mirror of
https://github.com/bjoernellens1/ESP32-PowerGuard.git
synced 2024-11-23 09:35:05 +00:00
working on esp32s2 saola1
This commit is contained in:
parent
7d52f66f22
commit
d0084f5d99
@ -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
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -1,26 +1,46 @@
|
||||
// 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><head><style>";
|
||||
// CSS-Stil für die Schalter und Beschriftungen
|
||||
// Common styles
|
||||
html += "body {";
|
||||
html += " font-family: Arial, sans-serif;";
|
||||
html += " margin: 0;";
|
||||
html += " padding: 0;";
|
||||
html += "}";
|
||||
html += ".frame {";
|
||||
html += " border: 2px solid #ddd;";
|
||||
html += " padding: 20px;";
|
||||
html += " border-radius: 10px;";
|
||||
html += " max-width: 600px;"; // Set the maximum width for larger screens
|
||||
html += " margin: auto;"; // Center the frame
|
||||
html += "}";
|
||||
// Styles for larger screens
|
||||
html += "@media (min-width: 600px) {";
|
||||
html += " .toggle-container {";
|
||||
html += " display: flex;";
|
||||
html += " align-items: center;";
|
||||
@ -68,15 +88,27 @@ void WebServerModule::handleRoot(AsyncWebServerRequest *request) {
|
||||
html += " input:focus + .slider {";
|
||||
html += " box-shadow: 0 0 1px #2196F3;";
|
||||
html += " }";
|
||||
html += "}";
|
||||
// Styles for smaller screens (e.g., mobile devices)
|
||||
html += "@media (max-width: 599px) {";
|
||||
html += " .toggle-container {";
|
||||
html += " flex-direction: column;"; // Stack the toggle containers vertically
|
||||
html += " text-align: center;"; // Center the text in toggle containers
|
||||
html += " }";
|
||||
html += " .toggle-label {";
|
||||
html += " margin-bottom: 5px;";
|
||||
html += " }";
|
||||
html += "}";
|
||||
html += "</style></head><body>";
|
||||
html += "<div class='frame'>";
|
||||
html += "<h1>Energieverbrauchsmonitor</h1>";
|
||||
// Anzeige der Werte
|
||||
// Anzeige der Werte (unchanged)
|
||||
html += "<p>Strom: <span id='current_A'></span> A</p>";
|
||||
html += "<p>Spannung: <span id='busVoltage_V'></span> V</p>";
|
||||
html += "<p>Leistung: <span id='power_W'></span> W</p>";
|
||||
html += "<p>Durchschnittliche Leistung: <span id='avgPower_W'></span> W</p>";
|
||||
html += "<p>Gesamtenergie: <span id='totalEnergy'></span> Wh</p>";
|
||||
// JavaScript-Code für die Werteaktualisierung und Schaltersteuerung
|
||||
// JavaScript-Code für die Werteaktualisierung und Schaltersteuerung (unchanged)
|
||||
html += "<script>setInterval(updateValues, 5000);";
|
||||
html += "function updateValues() { fetch('/values').then(response => response.json()).then(data => {";
|
||||
html += "document.getElementById('current_A').textContent = data.current_A;";
|
||||
@ -85,38 +117,55 @@ void WebServerModule::handleRoot(AsyncWebServerRequest *request) {
|
||||
html += "document.getElementById('avgPower_W').textContent = data.avgPower_W;";
|
||||
html += "document.getElementById('totalEnergy').textContent = data.totalEnergy;";
|
||||
html += "}); }</script>";
|
||||
// Schalter 1 (K1)
|
||||
// Schalter 1 (K1) to Schalter 4 (K4) (unchanged)
|
||||
html += "<div class='toggle-container'>";
|
||||
html += "<label class='toggle-label'>Relais 1</label>"; // Beschriftung für den ersten Schalter
|
||||
html += "<label class='toggle-label'>Relais 1</label>";
|
||||
html += "<label class='toggle'>";
|
||||
html += "<input type='checkbox' id='relaySwitch1' onchange='toggleRelay(1, this.checked)'>";
|
||||
html += "<span class='slider'></span>";
|
||||
html += "<span class='inner-slider'></span>";
|
||||
html += "</label>";
|
||||
html += "</div>";
|
||||
// Schalter 2 (K2)
|
||||
html += "<div class='toggle-container'>";
|
||||
html += "<label class='toggle-label'>Relais 2</label>"; // Beschriftung für den zweiten Schalter
|
||||
html += "<label class='toggle-label'>Relais 2</label>";
|
||||
html += "<label class='toggle'>";
|
||||
html += "<input type='checkbox' id='relaySwitch2' onchange='toggleRelay(2, this.checked)'>";
|
||||
html += "<span class='slider'></span>";
|
||||
html += "<span class='inner-slider'></span>";
|
||||
html += "</label>";
|
||||
html += "</div>";
|
||||
// JavaScript-Code für die Schaltersteuerung
|
||||
html += "<div class='toggle-container'>";
|
||||
html += "<label class='toggle-label'>Relais 3</label>";
|
||||
html += "<label class='toggle'>";
|
||||
html += "<input type='checkbox' id='relaySwitch3' onchange='toggleRelay(3, this.checked)'>";
|
||||
html += "<span class='slider'></span>";
|
||||
html += "<span class='inner-slider'></span>";
|
||||
html += "</label>";
|
||||
html += "</div>";
|
||||
html += "<div class='toggle-container'>";
|
||||
html += "<label class='toggle-label'>Relais 4</label>";
|
||||
html += "<label class='toggle'>";
|
||||
html += "<input type='checkbox' id='relaySwitch4' onchange='toggleRelay(4, this.checked)'>";
|
||||
html += "<span class='slider'></span>";
|
||||
html += "<span class='inner-slider'></span>";
|
||||
html += "</label>";
|
||||
html += "</div>";
|
||||
// JavaScript-Code für die Schaltersteuerung (unchanged)
|
||||
html += "<script>";
|
||||
html += "function toggleRelay(relay, state) {";
|
||||
html += " fetch(`/toggle${relay}?state=${Number(state)}`).then(response => response.text()).then(data => {";
|
||||
html += " const innerSlider = document.querySelectorAll('.inner-slider')[relay - 1];"; // Das entsprechende Inner Slider
|
||||
html += " const translation = state ? '26px' : '0';"; // Verschiebungsberechnung
|
||||
html += " innerSlider.style.transform = `translateX(${translation})`;"; // Anwenden der Verschiebung
|
||||
html += " const innerSlider = document.querySelectorAll('.inner-slider')[relay - 1];";
|
||||
html += " const translation = state ? '26px' : '0';";
|
||||
html += " innerSlider.style.transform = `translateX(${translation})`;";
|
||||
html += " });";
|
||||
html += "}";
|
||||
html += "</script>";
|
||||
html += "</body></html>";
|
||||
html += "</div></body></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);
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user