update: working correctly now. Chnaged for four channel active low relay module.

This commit is contained in:
Björn Ellensohn 2024-02-21 19:43:57 +01:00
parent d0084f5d99
commit d90e8d99b7
5 changed files with 123 additions and 46 deletions

View File

@ -8,7 +8,7 @@ bool InfluxDBModule::connectToInfluxDB() {
return client.validateConnection(); return client.validateConnection();
} }
bool InfluxDBModule::sendData(float current, float busVoltage, float power, float avgPower, float totalEnergy, int relay1State, int relay2State) { bool InfluxDBModule::sendData(float current, float busVoltage, float power, float avgPower, float totalEnergy, int relay1State, int relay2State, int relay3State, int relay4State) {
Point sensorData("sensor_data"); Point sensorData("sensor_data");
sensorData.addTag("device", "ESP32"); sensorData.addTag("device", "ESP32");
sensorData.addField("current", current); sensorData.addField("current", current);
@ -18,6 +18,8 @@ bool InfluxDBModule::sendData(float current, float busVoltage, float power, floa
sensorData.addField("totalEnergy", totalEnergy); sensorData.addField("totalEnergy", totalEnergy);
sensorData.addField("relay1StateINT", relay1State); sensorData.addField("relay1StateINT", relay1State);
sensorData.addField("relay2StateINT", relay2State); sensorData.addField("relay2StateINT", relay2State);
sensorData.addField("relay3StateINT", relay3State);
sensorData.addField("relay4StateINT", relay4State);
return client.writePoint(sensorData); return client.writePoint(sensorData);
} }

View File

@ -9,7 +9,7 @@ class InfluxDBModule {
public: public:
InfluxDBModule(String url, String org, String bucket, String token); InfluxDBModule(String url, String org, String bucket, String token);
bool connectToInfluxDB(); bool connectToInfluxDB();
bool sendData(float current, float busVoltage, float power, float avgPower, float totalEnergy, int relay1State, int relay2State); bool sendData(float current, float busVoltage, float power, float avgPower, float totalEnergy, int relay1State, int relay2State, int relay3State, int relay4State);
private: private:
InfluxDBClient client; InfluxDBClient client;
}; };

View File

@ -31,7 +31,7 @@ private:
bool fourthRelayState; bool fourthRelayState;
int _relayPin; int _relayPin;
int _secondRelayPin; int _secondRelayPin;
int _thirdrelayPin; int _thirdRelayPin;
int _fourthRelayPin; int _fourthRelayPin;
void handleRoot(AsyncWebServerRequest *request); void handleRoot(AsyncWebServerRequest *request);

View File

@ -3,14 +3,14 @@
WebServerModule::WebServerModule(INA219Module& inaModule, int relayPin, int secondRelayPin, int thirdRelayPin, int fourthRelayPin) WebServerModule::WebServerModule(INA219Module& inaModule, int relayPin, int secondRelayPin, int thirdRelayPin, int fourthRelayPin)
: _inaModule(inaModule), _server(80), _relayPin(relayPin), _secondRelayPin(secondRelayPin), : _inaModule(inaModule), _server(80), _relayPin(relayPin), _secondRelayPin(secondRelayPin),
_thirdrelayPin(thirdRelayPin), _fourthRelayPin(fourthRelayPin), relayState(false), secondRelayState(false), _thirdRelayPin(thirdRelayPin), _fourthRelayPin(fourthRelayPin), relayState(false), secondRelayState(false),
thirdrelayState(false), fourthRelayState(false) {} thirdrelayState(false), fourthRelayState(false) {}
void WebServerModule::begin() { void WebServerModule::begin() {
// Configure pins for relays as outputs // Configure pins for relays as outputs
pinMode(_relayPin, OUTPUT); pinMode(_relayPin, OUTPUT);
pinMode(_secondRelayPin, OUTPUT); pinMode(_secondRelayPin, OUTPUT);
pinMode(_thirdrelayPin, OUTPUT); pinMode(_thirdRelayPin, OUTPUT);
pinMode(_fourthRelayPin, OUTPUT); pinMode(_fourthRelayPin, OUTPUT);
// Configure server endpoints // Configure server endpoints
@ -108,48 +108,74 @@ void WebServerModule::handleRoot(AsyncWebServerRequest *request) {
html += "<p>Leistung: <span id='power_W'></span> W</p>"; html += "<p>Leistung: <span id='power_W'></span> W</p>";
html += "<p>Durchschnittliche Leistung: <span id='avgPower_W'></span> W</p>"; html += "<p>Durchschnittliche Leistung: <span id='avgPower_W'></span> W</p>";
html += "<p>Gesamtenergie: <span id='totalEnergy'></span> Wh</p>"; html += "<p>Gesamtenergie: <span id='totalEnergy'></span> Wh</p>";
// JavaScript-Code für die Werteaktualisierung und Schaltersteuerung (unchanged) // // JavaScript-Code für die Werteaktualisierung und Schaltersteuerung (unchanged)
html += "<script>setInterval(updateValues, 5000);"; // 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;";
// html += "document.getElementById('busVoltage_V').textContent = data.busVoltage_V;";
// html += "document.getElementById('power_W').textContent = data.power_W;";
// html += "document.getElementById('avgPower_W').textContent = data.avgPower_W;";
// html += "document.getElementById('totalEnergy').textContent = data.totalEnergy;";
// html += "}); }</script>";
// JavaScript-Code für die Werteaktualisierung und Schaltersteuerung
html += "<script>setInterval(updateValues, 1000);"; // Update values every second
html += "function updateValues() { fetch('/values').then(response => response.json()).then(data => {"; html += "function updateValues() { fetch('/values').then(response => response.json()).then(data => {";
html += "document.getElementById('current_A').textContent = data.current_A;"; html += "document.getElementById('current_A').textContent = data.current_A;";
html += "document.getElementById('busVoltage_V').textContent = data.busVoltage_V;"; html += "document.getElementById('busVoltage_V').textContent = data.busVoltage_V;";
html += "document.getElementById('power_W').textContent = data.power_W;"; html += "document.getElementById('power_W').textContent = data.power_W;";
html += "document.getElementById('avgPower_W').textContent = data.avgPower_W;"; html += "document.getElementById('avgPower_W').textContent = data.avgPower_W;";
html += "document.getElementById('totalEnergy').textContent = data.totalEnergy;"; html += "document.getElementById('totalEnergy').textContent = data.totalEnergy;";
// Update toggle states
html += "document.getElementById('relaySwitch1').checked = data.relay1State;";
html += "document.getElementById('relaySwitch2').checked = data.relay2State;";
html += "document.getElementById('relaySwitch3').checked = data.relay3State;";
html += "document.getElementById('relaySwitch4').checked = data.relay4State;";
html += "}); }</script>"; html += "}); }</script>";
// Schalter 1 (K1) to Schalter 4 (K4) (unchanged)
// Schalter 1 (K1)
html += "<div class='toggle-container'>"; html += "<div class='toggle-container'>";
html += "<label class='toggle-label'>Relais 1</label>"; html += "<label class='toggle-label'>Relais 1</label>";
html += "<label class='toggle'>"; html += "<label class='toggle'>";
html += "<input type='checkbox' id='relaySwitch1' onchange='toggleRelay(1, this.checked)'>"; html += "<input type='checkbox' id='relaySwitch1' onchange='toggleRelay(1, this.checked)' " + String(getRelayState() ? "checked" : "") + ">";
html += "<span class='slider'></span>"; html += "<span class='slider'></span>";
html += "<span class='inner-slider'></span>"; html += "<span class='inner-slider'></span>";
html += "</label>"; html += "</label>";
html += "</div>"; html += "</div>";
// Schalter 2 (K2)
html += "<div class='toggle-container'>"; html += "<div class='toggle-container'>";
html += "<label class='toggle-label'>Relais 2</label>"; html += "<label class='toggle-label'>Relais 2</label>";
html += "<label class='toggle'>"; html += "<label class='toggle'>";
html += "<input type='checkbox' id='relaySwitch2' onchange='toggleRelay(2, this.checked)'>"; html += "<input type='checkbox' id='relaySwitch2' onchange='toggleRelay(2, this.checked)' " + String(getSecondRelayState() ? "checked" : "") + ">";
html += "<span class='slider'></span>"; html += "<span class='slider'></span>";
html += "<span class='inner-slider'></span>"; html += "<span class='inner-slider'></span>";
html += "</label>"; html += "</label>";
html += "</div>"; html += "</div>";
// Schalter 3 (K3)
html += "<div class='toggle-container'>"; html += "<div class='toggle-container'>";
html += "<label class='toggle-label'>Relais 3</label>"; html += "<label class='toggle-label'>Relais 3</label>";
html += "<label class='toggle'>"; html += "<label class='toggle'>";
html += "<input type='checkbox' id='relaySwitch3' onchange='toggleRelay(3, this.checked)'>"; html += "<input type='checkbox' id='relaySwitch3' onchange='toggleRelay(3, this.checked)' " + String(getThirdRelayState() ? "checked" : "") + ">";
html += "<span class='slider'></span>"; html += "<span class='slider'></span>";
html += "<span class='inner-slider'></span>"; html += "<span class='inner-slider'></span>";
html += "</label>"; html += "</label>";
html += "</div>"; html += "</div>";
// Schalter 4 (K4)
html += "<div class='toggle-container'>"; html += "<div class='toggle-container'>";
html += "<label class='toggle-label'>Relais 4</label>"; html += "<label class='toggle-label'>Relais 4</label>";
html += "<label class='toggle'>"; html += "<label class='toggle'>";
html += "<input type='checkbox' id='relaySwitch4' onchange='toggleRelay(4, this.checked)'>"; html += "<input type='checkbox' id='relaySwitch4' onchange='toggleRelay(4, this.checked)' " + String(getFourthRelayState() ? "checked" : "") + ">";
html += "<span class='slider'></span>"; html += "<span class='slider'></span>";
html += "<span class='inner-slider'></span>"; html += "<span class='inner-slider'></span>";
html += "</label>"; html += "</label>";
html += "</div>"; html += "</div>";
// JavaScript-Code für die Schaltersteuerung (unchanged) // JavaScript-Code für die Schaltersteuerung (unchanged)
html += "<script>"; html += "<script>";
html += "function toggleRelay(relay, state) {"; html += "function toggleRelay(relay, state) {";
@ -164,14 +190,22 @@ void WebServerModule::handleRoot(AsyncWebServerRequest *request) {
request->send(200, "text/html", html); request->send(200, "text/html", html);
} }
void WebServerModule::handleValues(AsyncWebServerRequest *request) { void WebServerModule::handleValues(AsyncWebServerRequest *request) {
// String values = "{\"current_A\":" + String(_inaModule.getCurrent()) + ",";
// values += "\"busVoltage_V\":" + String(_inaModule.getBusVoltage()) + ",";
// values += "\"power_W\":" + String(_inaModule.getPower()) + ",";
// values += "\"avgPower_W\":" + String(_inaModule.calculateAveragePower()) + ",";
// values += "\"totalEnergy\":" + String(_inaModule.getTotalEnergy()) + "}";
String values = "{\"current_A\":" + String(_inaModule.getCurrent()) + ","; String values = "{\"current_A\":" + String(_inaModule.getCurrent()) + ",";
values += "\"busVoltage_V\":" + String(_inaModule.getBusVoltage()) + ","; values += "\"busVoltage_V\":" + String(_inaModule.getBusVoltage()) + ",";
values += "\"power_W\":" + String(_inaModule.getPower()) + ","; values += "\"power_W\":" + String(_inaModule.getPower()) + ",";
values += "\"avgPower_W\":" + String(_inaModule.calculateAveragePower()) + ","; values += "\"avgPower_W\":" + String(_inaModule.calculateAveragePower()) + ",";
values += "\"totalEnergy\":" + String(_inaModule.getTotalEnergy()) + "}"; values += "\"totalEnergy\":" + String(_inaModule.getTotalEnergy()) + ",";
values += "\"relay1State\":" + String(getRelayState() ? 1 : 0) + ",";
values += "\"relay2State\":" + String(getSecondRelayState() ? 1 : 0) + ",";
values += "\"relay3State\":" + String(getThirdRelayState() ? 1 : 0) + ",";
values += "\"relay4State\":" + String(getFourthRelayState() ? 1 : 0) + "}";
request->send(200, "application/json", values); request->send(200, "application/json", values);
} }
@ -179,7 +213,7 @@ void WebServerModule::handleToggle(AsyncWebServerRequest *request) {
if (request->hasParam("state")) { if (request->hasParam("state")) {
int state = request->getParam("state")->value().toInt(); int state = request->getParam("state")->value().toInt();
relayState = state; relayState = state;
digitalWrite(_relayPin, relayState ? HIGH : LOW); digitalWrite(_relayPin, relayState ? LOW : HIGH);
Serial.print("Relay 1 state set to: "); Serial.print("Relay 1 state set to: ");
Serial.println(relayState); Serial.println(relayState);
@ -194,7 +228,7 @@ void WebServerModule::handleSecondToggle(AsyncWebServerRequest *request) {
if (request->hasParam("state")) { if (request->hasParam("state")) {
int state = request->getParam("state")->value().toInt(); int state = request->getParam("state")->value().toInt();
secondRelayState = state; secondRelayState = state;
digitalWrite(_secondRelayPin, secondRelayState ? HIGH : LOW); digitalWrite(_secondRelayPin, secondRelayState ? LOW : HIGH);
Serial.print("Relay 2 state set to: "); Serial.print("Relay 2 state set to: ");
Serial.println(secondRelayState); Serial.println(secondRelayState);
@ -209,7 +243,7 @@ void WebServerModule::handleThirdToggle(AsyncWebServerRequest *request) {
if (request->hasParam("state")) { if (request->hasParam("state")) {
int state = request->getParam("state")->value().toInt(); int state = request->getParam("state")->value().toInt();
thirdrelayState = state; thirdrelayState = state;
digitalWrite(_thirdrelayPin, thirdrelayState ? HIGH : LOW); digitalWrite(_thirdRelayPin, thirdrelayState ? LOW : HIGH);
Serial.print("Relay 3 state set to: "); Serial.print("Relay 3 state set to: ");
Serial.println(thirdrelayState); Serial.println(thirdrelayState);
@ -224,7 +258,7 @@ void WebServerModule::handleFourthToggle(AsyncWebServerRequest *request) {
if (request->hasParam("state")) { if (request->hasParam("state")) {
int state = request->getParam("state")->value().toInt(); int state = request->getParam("state")->value().toInt();
fourthRelayState = state; fourthRelayState = state;
digitalWrite(_fourthRelayPin, fourthRelayState ? HIGH : LOW); digitalWrite(_fourthRelayPin, fourthRelayState ? LOW : HIGH);
Serial.print("Relay 4 state set to: "); Serial.print("Relay 4 state set to: ");
Serial.println(fourthRelayState); Serial.println(fourthRelayState);
@ -235,22 +269,24 @@ void WebServerModule::handleFourthToggle(AsyncWebServerRequest *request) {
} }
} }
bool WebServerModule::getThirdRelayState() const {
return thirdrelayState;
}
bool WebServerModule::getFourthRelayState() const {
return fourthRelayState;
}
void WebServerModule::handleToggleState(bool state) { void WebServerModule::handleToggleState(bool state) {
relayState = state; relayState = state;
digitalWrite(_relayPin, relayState ? HIGH : LOW); digitalWrite(_relayPin, relayState ? LOW : HIGH); // Inverted logic for active LOW relay
} }
void WebServerModule::handleSecondToggleState(bool state) { void WebServerModule::handleSecondToggleState(bool state) {
secondRelayState = state; secondRelayState = state;
digitalWrite(_secondRelayPin, secondRelayState ? HIGH : LOW); digitalWrite(_secondRelayPin, secondRelayState ? LOW : HIGH); // Inverted logic for active LOW relay
}
void WebServerModule::handleThirdToggleState(bool state) {
thirdrelayState = state;
digitalWrite(_thirdRelayPin, thirdrelayState ? LOW : HIGH); // Inverted logic for active LOW relay
}
void WebServerModule::handleFourthToggleState(bool state) {
fourthRelayState = state;
digitalWrite(_fourthRelayPin, fourthRelayState ? LOW : HIGH); // Inverted logic for active LOW relay
} }
bool WebServerModule::getRelayState() const { bool WebServerModule::getRelayState() const {
@ -260,3 +296,11 @@ bool WebServerModule::getRelayState() const {
bool WebServerModule::getSecondRelayState() const { bool WebServerModule::getSecondRelayState() const {
return secondRelayState; return secondRelayState;
} }
bool WebServerModule::getThirdRelayState() const {
return thirdrelayState;
}
bool WebServerModule::getFourthRelayState() const {
return fourthRelayState;
}

View File

@ -24,6 +24,12 @@ void readAndSendSensorData();
void controlRelays(); void controlRelays();
void setup() { void setup() {
// setting relay pins first - needed for proper initialization of active low relays
digitalWrite(RELAY_PIN, HIGH);
digitalWrite(SECOND_RELAY_PIN, HIGH);
digitalWrite(THIRD_RELAY_PIN, HIGH);
digitalWrite(FOURTH_RELAY_PIN, HIGH);
Serial.begin(115200); Serial.begin(115200);
Serial.println("... init ..."); Serial.println("... init ...");
Wire.begin(); Wire.begin();
@ -49,6 +55,15 @@ void setup() {
webServer.begin(); webServer.begin();
Serial.println("Web server started"); Serial.println("Web server started");
float busVoltage = inaModule.getBusVoltage();
// Init: Relais einschalten, wenn die Spannung zwischen 11 und 13V liegt
if (busVoltage > 11.0 && busVoltage < 13.0)
{
Serial.println("Turning on relays due to good voltage condition...");
webServer.handleToggleState(true); // Relay 1 einschalten
//webServer.handleSecondToggleState(true); // Relay 2 einschalten
}
Serial.println("... init done");
Serial.println(); Serial.println();
Serial.println("......"); Serial.println("......");
@ -67,14 +82,23 @@ void readAndSendSensorData() {
float power = inaModule.getPower(); float power = inaModule.getPower();
float avgPower = inaModule.calculateAveragePower(); float avgPower = inaModule.calculateAveragePower();
float totalEnergy = inaModule.getTotalEnergy(); float totalEnergy = inaModule.getTotalEnergy();
// Get relay states
bool relay1State = webServer.getRelayState(); bool relay1State = webServer.getRelayState();
bool relay2State = webServer.getSecondRelayState(); bool relay2State = webServer.getSecondRelayState();
// Ändere die bool's zu Float-Werten bool relay3State = webServer.getThirdRelayState(); // Assuming this method exists
bool relay4State = webServer.getFourthRelayState(); // Assuming this method exists
// Convert bool relay states to int
int relay1StateInt = relay1State ? 1 : 0; int relay1StateInt = relay1State ? 1 : 0;
int relay2StateInt = relay2State ? 1 : 0; int relay2StateInt = relay2State ? 1 : 0;
int relay3StateInt = relay3State ? 1 : 0;
int relay4StateInt = relay4State ? 1 : 0;
influxDB.sendData(current, busVoltage, power, avgPower, totalEnergy, relay1StateInt , relay2StateInt ); // Send data to InfluxDB
influxDB.sendData(current, busVoltage, power, avgPower, totalEnergy, relay1StateInt, relay2StateInt, relay3StateInt, relay4StateInt);
// Print data to Serial for debugging
Serial.print("current: "); Serial.print("current: ");
Serial.print(current); Serial.print(current);
Serial.println(" A"); Serial.println(" A");
@ -100,21 +124,28 @@ void readAndSendSensorData() {
Serial.println(); Serial.println();
} }
void controlRelays() { void controlRelays()
{
float power = inaModule.getPower(); float power = inaModule.getPower();
float busVoltage = inaModule.getBusVoltage(); float busVoltage = inaModule.getBusVoltage();
// Zentrale Logik für die Relaissteuerung // Zentrale Logik für die Relaissteuerung
if (power > 70.0) { if (power > 50.0)
{
Serial.println("Overload condition detected..."); Serial.println("Overload condition detected...");
webServer.handleToggleState(false); // Relay 1 ausschalten webServer.handleToggleState(false); // Relay 1 ausschalten
webServer.handleSecondToggleState(false); // Relay 2 ausschalten webServer.handleSecondToggleState(false); // Relay 2 ausschalten
webServer.handleThirdToggleState(false); // Relay 3 ausschalten
webServer.handleFourthToggleState(false); // Relay 4 ausschalten
} }
// Zusatz: Relais einschalten, wenn die Spannung zwischen 0 und 14V liegt // Zusatz: Relais ausschalten, wenn die Spannung nicht zwischen 0 und 14V liegt
if (busVoltage > 0.0 && busVoltage < 14.0) { if (!(busVoltage > 2.0 && busVoltage < 14.0))
Serial.println("Turning on relays due to voltage condition..."); {
webServer.handleToggleState(true); // Relay 1 einschalten Serial.println("Turning off relays due to bad voltage condition...");
webServer.handleSecondToggleState(true); // Relay 2 einschalten webServer.handleToggleState(false); // Relay 1 ausschalten
webServer.handleSecondToggleState(false); // Relay 2 ausschalten
webServer.handleThirdToggleState(false); // Relay 3 ausschalten
webServer.handleFourthToggleState(false); // Relay 4 ausschalten
} }
} }