#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
//#include <ESPAsyncWebServer.h>
#include "ButtonHandler.h"
#define RELAY1 16
#define RELAY2 17
#define RELAY3 18
#define VOLTAGE_PIN 34
#define BTN_UP 25
#define BTN_DOWN 26
#define BTN_SELECT 27
#define BTN_RESET 14
LiquidCrystal_I2C lcd(0x27, 20, 4);
//AsyncWebServer server(80);
ButtonUp Up(BTN_UP);
ButtonDown Down(BTN_DOWN);
ButtonSelect Select(BTN_SELECT);
ButtonCancel Cancel(BTN_RESET);
bool relay1_state = false;
bool relay2_state = false;
bool relay3_state = false;
void setup()
{
Serial.begin(115200);
lcd.init();
lcd.backlight();
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
pinMode(BTN_UP, INPUT_PULLUP);
pinMode(BTN_DOWN, INPUT_PULLUP);
pinMode(BTN_SELECT, INPUT_PULLUP);
pinMode(BTN_RESET, INPUT_PULLUP);
/*
WiFi.softAP("ESP32_Battery", "12345678");
server.on("/status", HTTP_GET, [](AsyncWebServerRequest *request)
{
String json = "{";
json += "\"voltage\":" + String(voltage, 2) + ",";
json += "\"thresholds\": [" + String(thresholds[0],2) + "," + String(thresholds[1],2) + "," + String(thresholds[2],2) + "],";
json += "\"hysteresis\": " + String(hysteresis, 2);
json += "}";
request->send(200, "application/json", json);
});
server.begin();
*/
}
void loop()
{
voltage = analogRead(VOLTAGE_PIN) * (3.3 / 4095.0) * 5.0;
// Relay handler
if(voltage > thresholds[0] + hysteresis) relay1_state = true;
if(voltage < thresholds[0] - hysteresis) relay1_state = false;
digitalWrite(RELAY1, relay1_state ? HIGH : LOW);
if(voltage > thresholds[1] + hysteresis) relay2_state = true;
if(voltage < thresholds[1] - hysteresis) relay2_state = false;
digitalWrite(RELAY2, relay2_state ? HIGH : LOW);
if(voltage > thresholds[2] + hysteresis) relay3_state = true;
if(voltage < thresholds[2] - hysteresis) relay3_state = false;
digitalWrite(RELAY3, relay3_state ? HIGH : LOW);
/*
if (digitalRead(BTN_SELECT) == LOW)
{
editing = !editing;
if(editing)
{
tempThreshold = thresholds[selectedIndex];
}
else
{
thresholds[selectedIndex] = tempThreshold;
}
delay(200);
}
if (digitalRead(BTN_RESET) == LOW)
{
editing = false;
delay(200);
}
*/
Select.SelectSave();
Cancel.Cancel();
if(!editing)
{
Up.Scroll();
Down.Scroll();
}
else
{
Up.ModifyThreshold();
Down.ModifyThreshold();
/*
static unsigned long buttonPressTime = 0;
static bool buttonHeld = false;
if (digitalRead(BTN_UP) == LOW)
{
if (!buttonHeld)
{
buttonPressTime = millis();
buttonHeld = true;
}
if (millis() - buttonPressTime > 500)
{
tempThreshold += 0.1;
}
else
{
tempThreshold += 0.01;
}
delay(200);
}
else if (digitalRead(BTN_DOWN) == LOW)
{
if (!buttonHeld)
{
buttonPressTime = millis();
buttonHeld = true;
}
if (millis() - buttonPressTime > 500)
{
tempThreshold -= 0.1;
}
else
{
tempThreshold -= 0.01;
}
delay(200);
}
else
{
buttonHeld = false;
}
*/
}
lcd.setCursor(0, 0);
lcd.print("N:");
lcd.print(voltage);
lcd.print("V");
lcd.print(" G:");
lcd.print(hysteresis);
lcd.print("V");
lcd.setCursor(0, 1);
lcd.print((selectedIndex == 0 ? ">" : " ")); lcd.print("R1:");
if(editing && selectedIndex == 0 )
{
lcd.print(tempThreshold); lcd.print("<");
}
else
{
lcd.print(thresholds[0]); lcd.print(" ");
}
lcd.setCursor(0, 2);
lcd.print((selectedIndex == 1 ? ">" : " ")); lcd.print("R2:");
if(editing && selectedIndex == 1 )
{
lcd.print(tempThreshold); lcd.print("<");
}
else
{
lcd.print(thresholds[1]); lcd.print(" ");
}
lcd.setCursor(0, 3);
lcd.print((selectedIndex == 2 ? ">" : " ")); lcd.print("R3:");
if(editing && selectedIndex == 2)
{
lcd.print(tempThreshold); lcd.print("<");
}
else
{
lcd.print(thresholds[2]); lcd.print(" ");
}
//Serial.print("Editing: "); Serial.println(editing);
//Serial.print("Threshold 1: "); Serial.println(thresholds[0]);
//Serial.print("Editing Threshold: "); Serial.println(tempThreshold);
//delay(500);
}
[BATTERY]