working on esp32s2 saola1

This commit is contained in:
Björn Ellensohn 2024-02-21 18:15:28 +01:00
parent 7d52f66f22
commit d0084f5d99
5 changed files with 178 additions and 63 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -1,46 +1,66 @@
// 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
html += ".toggle-container {";
// 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;";
html += " margin-bottom: 10px;";
html += "}";
html += ".toggle-label {";
html += " }";
html += " .toggle-label {";
html += " margin-right: 10px;";
html += "}";
html += ".toggle {";
html += " }";
html += " .toggle {";
html += " position: relative;";
html += " display: inline-block;";
html += " width: 60px;";
html += " height: 34px;";
html += "}";
html += ".toggle input {";
html += " }";
html += " .toggle input {";
html += " opacity: 0;";
html += " width: 0;";
html += " height: 0;";
html += "}";
html += ".slider {";
html += " }";
html += " .slider {";
html += " position: absolute;";
html += " cursor: pointer;";
html += " top: 0;";
@ -50,8 +70,8 @@ void WebServerModule::handleRoot(AsyncWebServerRequest *request) {
html += " background-color: #ccc;";
html += " border-radius: 34px;";
html += " transition: .4s;";
html += "}";
html += ".inner-slider {";
html += " }";
html += " .inner-slider {";
html += " position: absolute;";
html += " content: \"\";";
html += " height: 26px;";
@ -61,22 +81,34 @@ void WebServerModule::handleRoot(AsyncWebServerRequest *request) {
html += " background-color: white;";
html += " border-radius: 50%;";
html += " transition: .4s;";
html += "}";
html += "input:checked + .slider {";
html += " }";
html += " input:checked + .slider {";
html += " background-color: #2196F3;";
html += "}";
html += "input:focus + .slider {";
html += " }";
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);

View File

@ -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
}