#include <LiquidCrystal.h>
#include <WiFi.h>
#include <HTTPClient.h>
//#include <SPIFFS.h>
//#include <WiFiSettings.h>
#include "MyButton.h"
#include "myTimer.h"
#include "EEPROM.h"
#define relay 25
#define tempPin 35
LiquidCrystal lcd(21, 19, 18, 4, 2, 15);
Button ctrin, enter, next, prev;
MyTimer update;
uint32_t counter = 0;
int timeout = 10;
int temperature = -1, threshold1 = 0, threshold2 = 150;
int otp[4] = { 0, 0, 0, 0 };
int otpindex = 0;
int menu = 0, _menu;
String enteredOtp;
bool eepromOk;
void setup() {
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("WELCOME");
lcd.setCursor(0, 1);
lcd.print("Connecting wifi..");
delay(300);
Serial.begin(115200);
// SPIFFS.begin(true);
// WiFiSettings.onSuccess = []() {
// lcd.clear();
// lcd.print("Connected");
// delay(300);
// };
// WiFiSettings.connect();
WiFi.begin("Wokwi-GUEST","");
while(!WiFi.isConnected()){
Serial.print(".");
delay(1000);
}
eepromOk = EEPROM.begin(512);
if (!eepromOk) Serial.println("EEPROM ERROR");
else {
threshold1 = EEPROM.read(0);
threshold2 = EEPROM.read(1);
if (threshold1 > 150) threshold1 = 0;
if (threshold2 > 150) threshold2 = 150;
}
Serial.begin(9600);
ctrin.init(27, INPUT);
enter.init(14, INPUT_PULLUP);
next.init(12, INPUT_PULLUP);
prev.init(13, INPUT_PULLUP);
pinMode(relay, OUTPUT);
pinMode(tempPin, INPUT);
delay(1000);
lcd.clear();
updatecounter();
updateTemp();
update.setCallback(updateTemp);
update.setTimeout(1000);
}
void loop() {
if (menu != _menu) {
_menu = menu;
lcd.clear();
}
if (menu == 0) {
updatecounter();
update.run();
printTemp();
if (temperature < threshold1) {
lcd.setCursor(3, 0);
lcd.print("HEATER OFF");
}
if (temperature > threshold2) {
lcd.setCursor(3, 0);
lcd.print("HEATER ON ");
}
enter.longPress([]() {
menu = 1;
lcd.clear();
},
1);
next.longPress([]() {
menu = 2;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temperature");
lcd.setCursor(0, 1);
lcd.print("settings");
delay(1000);
lcd.clear();
},
1);
} else if (menu == 1) {
next.ShortPress(nextCb);
prev.ShortPress(prevCb);
enter.ShortPress(enterCb);
otpMenu();
enter.longPress([]() {
menu = 0;
lcd.clear();
},
1);
} else if (menu == 2) {
tempMenu();
}
}
void updatecounter() {
printCounter();
ctrin.ShortPress([]() {
Serial.println("Counter++");
if (counter < 50000) {
counter++;
}
});
}
void counterResetBlue(char val) {
if (val == 'r') {
counter = 0;
timeout = 10;
menu = 0;
otpindex = 0;
lcd.clear();
lcd.print("OTP Verified");
delay(300);
lcd.setCursor(0, 0);
lcd.print("Counter reset!");
delay(1000);
lcd.clear();
} else if (val == 'n') {
lcd.clear();
lcd.print("OTP Invalid");
delay(1000);
}
}
void updateTemp() {
//bt.println("hello");
if (readTemperature() != temperature) {
temperature = readTemperature();
if (temperature < threshold1) {
digitalWrite(relay, 1);
}
if (temperature > threshold2) {
digitalWrite(relay, 0);
}
}
update.reset();
}
void printCounter() {
lcd.setCursor(8, 1);
lcd.print("C:");
lcd.print(counter);
}
void printTemp() {
lcd.setCursor(0, 1);
lcd.print("T:");
lcd.print(temperature);
lcd.print("C ");
}
void nextCb() {
otp[otpindex]++;
if (otp[otpindex] > 9) otp[otpindex] = 0;
}
void prevCb() {
otp[otpindex]--;
if (otp[otpindex] < 1) otp[otpindex] = 0;
}
void enterCb() {
otpindex++;
if (otpindex > 3) {
otpindex = 0;
otpVerify();
}
}
void otpMenu() {
lcd.setCursor(0, 0);
lcd.print("Enter OTP:");
enteredOtp = String(otp[0]) + String(otp[1]) + String(otp[2]) + String(otp[3]);
lcd.print(enteredOtp);
lcd.setCursor(otpindex + 9, 1);
lcd.print(" ^");
}
void otpVerify() {
lcd.clear();
lcd.print("OTP verifying...");
delay(300);
if (WiFi.isConnected()) {
verifyOtp(enteredOtp);
}
}
//=============================Temperature set===========
bool threIndex = 0;
void tempMenu() {
lcd.setCursor(0, 0);
lcd.print("min temp <");
lcd.print(threshold1);
lcd.print("C> ");
lcd.setCursor(0, 1);
lcd.print("max temp <");
lcd.print(threshold2);
lcd.print("C> ");
lcd.setCursor(15, !threIndex);
lcd.print("<");
next.ShortPress([]() {
if (!threIndex)
if (threshold2 < 150) threshold2 += 2;
if (threIndex)
if (threshold1 < 150) threshold1 += 2;
});
prev.ShortPress([]() {
if (!threIndex)
if (threshold2 > 50) threshold2 -= 2;
if (threIndex)
if (threshold1 > 50) threshold1 -= 2;
});
enter.ShortPress([]() {
threIndex = !threIndex;
});
enter.longPress([]() {
if (threshold1 == threshold2) {
lcd.clear();
lcd.print("Threshold Error");
delay(1000);
lcd.clear();
return;
}
menu = 0;
if (eepromOk) {
EEPROM.write(0, threshold1);
EEPROM.write(1, threshold2);
EEPROM.commit();
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp threshold");
lcd.setCursor(0, 1);
lcd.print("Saved");
delay(1000);
lcd.clear();
},
1);
}
float readTemperature() {
int analogValue = analogRead(tempPin);
float voltage = analogValue * 3.3 / 4095.0;
float temperatureC = voltage * 100.0;
return temperatureC;
}
bool verifyOtp(String otp) {
HTTPClient http;
String url = "https://espotpverify-ht3zdfvb.b4a.run/verify/" + otp;
http.begin(url);
Serial.println(url);
int httpResponseCode = http.GET();
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
if (httpResponseCode == 200) {
counterResetBlue('r');
return false;
} else {
counterResetBlue('n');
return false;
}
http.end();
return true;
}