#include "Weather.h"
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <ESP32Servo.h>
// Tanımlanacak
#include "Clock.h"
#include "Weather.h"
#define I2C_ADDR 0x27
#define Lcd_columns 16
#define Lcd_lines 4
uint8_t state;
unsigned long ClockTimer;
unsigned long weatherAPItimer;
unsigned long weatherDisplayTimer;
uint8_t valIndex;
uint8_t cursorPos;
const char* password = "";
const char* wid = "Wokwi-GUEST";
char entered_value [6];
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 21, 20, 19, 18 };
uint8_t rowPins[ROWS] = { 35, 34, 33, 26 };
LiquidCrystal_I2C lcd(I2C_ADDR, Lcd_columns, Lcd_lines);
Clock rtc(&lcd);
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
Servo servo;
void setup() {
Wire.begin(15,16);
lcd.init();
lcd.backlight();
WiFi.begin(wid,password);
servo.attach(14, 500, 2400);
while (WiFi.status() != WL_CONNECTED)
{
lcd.print(".");
delay(1000);
}
lcd.clear();
if(!rtc.setTimeFromAPI())
{
char t [] = __TIME__;
char compileTime [] = { t[0], t[1], t[3], t[4], t[6], t[7] };
rtc.setTime(compileTime);
getWeather(lcd);
printWeather(lcd);
}
}
void loop() {
if (state == 0)
{
unsigned long millisNow;
if (millisNow - ClockTimer >= 1000)
{
ClockTimer = millisNow;
rtc.updateClock();
}
if (millisNow - weatherDisplayTimer >= 10000)
{
weatherDisplayTimer = millisNow;
printWeather(lcd);
}
if (millisNow - weatherAPItimer >= 3600000)
{
weatherAPItimer = millisNow;
getWeather(lcd);
}
}
getInput();
}
void getInput()
{
switch (state)
{
case 0:
keyPadState0();
break;
case 1:
keyPadState1();
break;
case 2:
keyPadState2();
break;
}
}
void keyPadState0()
{
char key = keypad.getKey();
switch(key)
{
case 'A':
enterAlarm();
break;
case 'C':
enterTime();
break;
case '#':
rtc.silence();
break;
case '*':
rtc.addToSnooze();
break;
}
}
void keyPadState1()
{
char key = keypad.getKey();
switch(key)
{
case '#':
rtc.setTime(entered_value);
state = 0;
break;
case '*':
eraseChar();
break;
default:
if (isDigit(key))
{
nextChar(key);
}
break;
}
}
void keyPadState2()
{
char key = keypad.getKey();
switch(key)
{
case '#':
rtc.setAlarm(entered_value);
state = 0;
break;
case '*':
eraseChar();
break;
default:
if (isDigit(key))
{
nextChar(key);
}
break;
}
}
void enterAlarm()
{
state = 2;
memset(&entered_value[0],0,sizeof(entered_value));
cursorPos = 0;
valIndex = 0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Alarm Saatini Gir");
lcd.setCursor(0,1);
lcd.print("kayıt icin #");
delay(3000);
lcd.clear();
}
void enterTime()
{
state = 1;
memset(&entered_value[0], 0, sizeof(entered_value));
cursorPos = 0;
valIndex = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Saati Ayarla ");
lcd.setCursor(0, 1);
lcd.print("kayıt icin #");
delay(3000);
lcd.clear();
}
void nextChar(char key)
{
if (valIndex < 6)
{
entered_value[valIndex] = key;
lcd.setCursor(0, 0);
lcd.print(entered_value);
cursorPos++;
valIndex++;
}
}
void eraseChar()
{
if (valIndex > 0 )
{
valIndex--;
cursorPos--;
entered_value[valIndex] = '\0';
lcd.setCursor(cursorPos, 0);
lcd.print(' ');
lcd.setCursor(cursorPos, 0);
}
}
Loading
esp32-s2-devkitm-1
esp32-s2-devkitm-1