Merci, that makes it a bit clearer.
However, the whole code still does not work.
The read out over the serial monitor still works, but as soon as the ESP32 tries to connect to WIFI, it quits working with the following prompt.
E (19179) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
E (19179) task_wdt: - async_tcp (CPU 1)
E (19179) task_wdt: Tasks currently running:
E (19179) task_wdt: CPU 0: tiT
E (19179) task_wdt: CPU 1: loopTask
E (19179) task_wdt: Aborting.
abort() was called at PC 0x400e635d on core 0
Backtrace: 0x40083a75:0x3ffbec0c |<-CORRUPTED
ELF file SHA256: d95b23bfeaa30abb
Rebooting...
Can't make heads or tales out of that. CORRUPTED certainly does not sound good.
This is the code I'm using.
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <ModbusMaster.h>
#include <SoftwareSerial.h>
const char* ssid = "...";
const char* password = "...";
float SDM_U=0;
float SDM_I=0;
float SDM_P=0;
float SDM_Eexp=0;
float SDM_F=0;
//SerialMod(Rx,Tx)
SoftwareSerial SerialMod(32,33);
ModbusMaster node;
#define modbusaddr 1
void preTransmission()
{
//delay(1);
}
void postTransmission()
{
//delay(1);
}
AsyncWebServer server(80);
AsyncEventSource events("/events");
// Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 3000;
void readSensor(){
SDM_U = getRTU(0x0000);
Serial.println("Spannung: " String(SDM_U,2));
delay(1000);
SDM_I = getRTU(0x0006);
Serial.println("Strom: " String(SDM_I,2));
delay(1000);
SDM_P = getRTU(0x000C);
Serial.println("Leistung: " String(SDM_P,2));
delay(1000);
SDM_Eexp = getRTU(0x004A);
Serial.println("Energie Export: " String(SDM_Eexp,2));
delay(1000);
SDM_F = getRTU(0x0046);
Serial.println("Frequenz: " String(SDM_F,2));
delay(1000);
Serial.println("==================================");
}
String processor(const String& var){
readSensor();
//Serial.println(var);
if(var == "SPANNUNG"){
return String(SDM_U);
}
else if(var == "FREQUENZ"){
return String(SDM_F);
}
else if(var == "EXPORT"){
return String(SDM_Eexp);
}
return String();
}
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ESP32 SDM230</title>
<style>
html {font-family: Arial; display: inline-block; text-align: center;}
p { font-size: 1.2rem;}
body { margin: 0;}
.topnav { overflow: hidden; background-color: #50B8B4; color: white; font-size: 1rem; }
.content { padding: 20px; }
.card { background-color: white; box-shadow: 2px 2px 12px 1px rgba(140,140,140,.5); }
.cards { max-width: 800px; margin: 0 auto; display: grid; grid-gap: 2rem; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.reading { font-size: 1.4rem; }
</style>
</head>
<body>
<div class="topnav">
<h2>ESP32 SDM230</h2>
</div>
<div class="content">
<div class="cards">
<div class="card">
<p>Spannung</p><p><span class="reading"><span id="Spannung">%SPANNUNG%</span> V</span></p>
</div>
<div class="card">
<p>Frequenz</p><p><span class="reading"><span id="Frequenz">%FREQUENZ%</span> Hz</span></p>
</div>
<div class="card">
<p>Energie Export</p><p><span class="reading"><span id="Energie_export">%EXPORT%</span> Wh</span></p>
</div>
</div>
</div>
<script>
if (!!window.EventSource) {
var source = new EventSource('/events');
source.addEventListener('open', function(e) {
console.log("Events Connected");
} , false);
source.addEventListener('error', function(e) {
if (e.target.readyState != EventSource.OPEN) {
console.log("Events Disconnected");
}
}, false);
source.addEventListener('message', function(e) {
console.log("message", e.data);
}, false);
source.addEventListener('SDM_U', function(e) {
console.log("SDM_U", e.data);
document.getElementById("Spannung").innerHTML = e.data;
}, false);
source.addEventListener('SDM_F', function(e) {
console.log("SDM_F", e.data);
document.getElementById("Frequenz").innerHTML = e.data;
}, false);
source.addEventListener('SDM_Eexp', function(e) {
console.log("SDM_Eexp", e.data);
document.getElementById("Energie_export").innerHTML = e.data;
}, false);
}
</script>
</body>
</html>
)rawliteral";
void setup(){
Serial.begin(9600);
SerialMod.begin(2400);
delay(2000);
Serial.println("Ready");
node.begin(modbusaddr, SerialMod);
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
Serial.println(WiFi.localIP());
// Handle Web Server
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html, processor);
});
// Handle Web Server Events
events.onConnect([](AsyncEventSourceClient *client){
if(client->lastId()){
Serial.printf("Client reconnected! Last message ID that it got is: %u\n", client->lastId());
}
// send event with message "hello!", id current millis
// and set reconnect delay to 1 second
client->send("hello!", NULL, millis(), 10000);
});
server.addHandler(&events);
server.begin();
}
float reform_uint16_2_float32(uint16_t u1, uint16_t u2)
{
uint32_t num = ((uint32_t)u1 & 0xFFFF) << 16 | ((uint32_t)u2 & 0xFFFF);
float numf;
memcpy(&numf, &num, 4);
return numf;
}
float getRTU(uint16_t m_startAddress){
uint8_t m_length =2;
uint16_t result;
float x;
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
result = node.readInputRegisters(m_startAddress, m_length);
if (result == node.ku8MBSuccess) {
return reform_uint16_2_float32(node.getResponseBuffer(0),node.getResponseBuffer(1));
}
}
void loop() {
if ((millis() - lastTime) > timerDelay) {
readSensor();
// Send Events to the Web Client with the Sensor Readings
events.send("ping",NULL,millis());
events.send(String(SDM_U).c_str(),"SDM_U",millis());
events.send(String(SDM_F).c_str(),"SDM_F",millis());
events.send(String(SDM_Eexp).c_str(),"SDM_Eexp",millis());
lastTime = millis();
}
}
Any ideas, where the problem might be, anyone?