diff --git a/platformio.ini b/platformio.ini index f3ceccf..3d3923f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -20,3 +20,17 @@ lib_deps = tobiasschuerg/ESP8266 Influxdb@^3.13.1 upload_port = COM3 monitor_speed = 115200 + +[env:esp32-s2-saola-1] +platform = espressif32 +board = esp32-s2-saola-1 +framework = arduino +lib_deps = + Wire + Adafruit INA219 + ESP Async WebServer + ArduinoJson + tobiasschuerg/ESP8266 Influxdb@^3.13.1 +; any port that starts with /dev/ttyUSB +#upload_port = /dev/ttyUSB* +monitor_speed = 115200 \ No newline at end of file diff --git a/src/Config.h b/src/Config.h index 988d48f..676c092 100644 --- a/src/Config.h +++ b/src/Config.h @@ -10,7 +10,11 @@ const char* INFLUXDB_ORG = "48cec24009273b5e"; const char* INFLUXDB_BUCKET = "bucket-digitalisierung"; // Other configurations +// #define RELAY_PIN 2 +// #define SECOND_RELAY_PIN 15 #define RELAY_PIN 2 -#define SECOND_RELAY_PIN 15 +#define SECOND_RELAY_PIN 1 +#define THIRD_RELAY_PIN 4 +#define FOURTH_RELAY_PIN 3 #endif // CONFIG_H diff --git a/src/WebServerModule.h b/src/WebServerModule.h index 3fccf5b..955ca54 100644 --- a/src/WebServerModule.h +++ b/src/WebServerModule.h @@ -9,13 +9,17 @@ class WebServerModule { public: - WebServerModule(INA219Module& inaModule, int relayPin, int secondRelayPin); + WebServerModule(INA219Module& inaModule, int relayPin, int secondRelayPin, int thirdRelayPin, int fourthRelayPin); void begin(); bool getRelayState() const; bool getSecondRelayState() const; + bool getThirdRelayState() const; + bool getFourthRelayState() const; void handleToggleState(bool state); void handleSecondToggleState(bool state); + void handleThirdToggleState(bool state); + void handleFourthToggleState(bool state); private: INA219Module& _inaModule; @@ -23,13 +27,19 @@ private: bool relayState; bool secondRelayState; + bool thirdrelayState; + bool fourthRelayState; int _relayPin; int _secondRelayPin; + int _thirdrelayPin; + int _fourthRelayPin; void handleRoot(AsyncWebServerRequest *request); void handleValues(AsyncWebServerRequest *request); void handleToggle(AsyncWebServerRequest *request); void handleSecondToggle(AsyncWebServerRequest *request); + void handleThirdToggle(AsyncWebServerRequest *request); + void handleFourthToggle(AsyncWebServerRequest *request); }; #endif diff --git a/src/WebserverModule.cpp b/src/WebserverModule.cpp index 8cc7a4d..d24612a 100644 --- a/src/WebserverModule.cpp +++ b/src/WebserverModule.cpp @@ -1,82 +1,114 @@ // WebServerModule.cpp #include "WebServerModule.h" -WebServerModule::WebServerModule(INA219Module& inaModule, int relayPin, int secondRelayPin) - : _inaModule(inaModule), _server(80), _relayPin(relayPin), _secondRelayPin(secondRelayPin), relayState(false), secondRelayState(false) {} +WebServerModule::WebServerModule(INA219Module& inaModule, int relayPin, int secondRelayPin, int thirdRelayPin, int fourthRelayPin) + : _inaModule(inaModule), _server(80), _relayPin(relayPin), _secondRelayPin(secondRelayPin), + _thirdrelayPin(thirdRelayPin), _fourthRelayPin(fourthRelayPin), relayState(false), secondRelayState(false), + thirdrelayState(false), fourthRelayState(false) {} void WebServerModule::begin() { - // Konfiguriere die Pins für die Relais als Ausgänge + // Configure pins for relays as outputs pinMode(_relayPin, OUTPUT); pinMode(_secondRelayPin, OUTPUT); + pinMode(_thirdrelayPin, OUTPUT); + pinMode(_fourthRelayPin, OUTPUT); - // Konfiguriere die Server-Endpunkte + // Configure server endpoints _server.on("/", HTTP_GET, std::bind(&WebServerModule::handleRoot, this, std::placeholders::_1)); _server.on("/values", HTTP_GET, std::bind(&WebServerModule::handleValues, this, std::placeholders::_1)); _server.on("/toggle1", HTTP_GET, std::bind(&WebServerModule::handleToggle, this, std::placeholders::_1)); _server.on("/toggle2", HTTP_GET, std::bind(&WebServerModule::handleSecondToggle, this, std::placeholders::_1)); + _server.on("/toggle3", HTTP_GET, std::bind(&WebServerModule::handleThirdToggle, this, std::placeholders::_1)); + _server.on("/toggle4", HTTP_GET, std::bind(&WebServerModule::handleFourthToggle, this, std::placeholders::_1)); _server.begin(); } void WebServerModule::handleRoot(AsyncWebServerRequest *request) { String html = ""; + html += "
"; html += "

Energieverbrauchsmonitor

"; - // Anzeige der Werte + // Anzeige der Werte (unchanged) html += "

Strom: A

"; html += "

Spannung: V

"; html += "

Leistung: W

"; html += "

Durchschnittliche Leistung: W

"; html += "

Gesamtenergie: Wh

"; - // JavaScript-Code für die Werteaktualisierung und Schaltersteuerung + // JavaScript-Code für die Werteaktualisierung und Schaltersteuerung (unchanged) html += ""; - // Schalter 1 (K1) + // Schalter 1 (K1) to Schalter 4 (K4) (unchanged) html += "
"; - html += ""; // Beschriftung für den ersten Schalter + html += ""; html += ""; html += "
"; - // Schalter 2 (K2) html += "
"; - html += ""; // Beschriftung für den zweiten Schalter + html += ""; html += ""; html += "
"; - // JavaScript-Code für die Schaltersteuerung + html += "
"; + html += ""; + html += ""; + html += "
"; + html += "
"; + html += ""; + html += ""; + html += "
"; + // JavaScript-Code für die Schaltersteuerung (unchanged) html += ""; - html += ""; + html += "
"; request->send(200, "text/html", html); } + + void WebServerModule::handleValues(AsyncWebServerRequest *request) { String values = "{\"current_A\":" + String(_inaModule.getCurrent()) + ","; values += "\"busVoltage_V\":" + String(_inaModule.getBusVoltage()) + ","; @@ -156,6 +205,44 @@ void WebServerModule::handleSecondToggle(AsyncWebServerRequest *request) { } } +void WebServerModule::handleThirdToggle(AsyncWebServerRequest *request) { + if (request->hasParam("state")) { + int state = request->getParam("state")->value().toInt(); + thirdrelayState = state; + digitalWrite(_thirdrelayPin, thirdrelayState ? HIGH : LOW); + + Serial.print("Relay 3 state set to: "); + Serial.println(thirdrelayState); + + request->send(200, "text/plain", thirdrelayState ? "Relay 3 turned on" : "Relay 3 turned off"); + } else { + request->send(400); // Error, no state information + } +} + +void WebServerModule::handleFourthToggle(AsyncWebServerRequest *request) { + if (request->hasParam("state")) { + int state = request->getParam("state")->value().toInt(); + fourthRelayState = state; + digitalWrite(_fourthRelayPin, fourthRelayState ? HIGH : LOW); + + Serial.print("Relay 4 state set to: "); + Serial.println(fourthRelayState); + + request->send(200, "text/plain", fourthRelayState ? "Relay 4 turned on" : "Relay 4 turned off"); + } else { + request->send(400); // Error, no state information + } +} + +bool WebServerModule::getThirdRelayState() const { + return thirdrelayState; +} + +bool WebServerModule::getFourthRelayState() const { + return fourthRelayState; +} + void WebServerModule::handleToggleState(bool state) { relayState = state; digitalWrite(_relayPin, relayState ? HIGH : LOW); diff --git a/src/main.cpp b/src/main.cpp index 6b9d6bf..7146082 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,7 +18,7 @@ WiFiModule wifiModule(WIFI_SSID, WIFI_PASSWORD); INA219Module inaModule; InfluxDBModule influxDB(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN); -WebServerModule webServer(inaModule, RELAY_PIN, SECOND_RELAY_PIN); +WebServerModule webServer(inaModule, RELAY_PIN, SECOND_RELAY_PIN, THIRD_RELAY_PIN, FOURTH_RELAY_PIN); void readAndSendSensorData(); void controlRelays(); @@ -106,7 +106,7 @@ void controlRelays() { // Zentrale Logik für die Relaissteuerung if (power > 70.0) { - Serial.println("Turning on relays..."); + Serial.println("Overload condition detected..."); webServer.handleToggleState(false); // Relay 1 ausschalten webServer.handleSecondToggleState(false); // Relay 2 ausschalten }