Author Topic: ESP32 send data to async webserver  (Read 5666 times)

0 Members and 1 Guest are viewing this topic.

Offline PSR B1257Topic starter

  • Frequent Contributor
  • **
  • Posts: 330
  • Country: de
ESP32 send data to async webserver
« on: April 04, 2023, 01:44:59 pm »
Hello all.
I would like to discuss my present problem with a ESP32 code.
The ESP32 reads out a SDM230 power meter via MODBUS. This already works.
Now I just (famous last words) want to display certain values in a web server.

In order to also work in other scripts, I want to keep the ESPAsyncWebServer.h library
Is there a simple line of code like this one for the ESP8266WebServer.h library?
Code: [Select]
server.send(200, "text/html", String(V,1));No fancy formatting required (jet). Plain text as a proof of concept would be enough for starters.

Here is the whole code
Code: [Select]
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <ModbusMaster.h>
#include <SoftwareSerial.h>

const char* ssid = "...";
const char* password = "...";


float V=0;
float A=0;
float W=0;
float Wh=0;
float PF=0;
float F=0;
//order of ports(Rx,Tx)
SoftwareSerial SerialMod(32,33);
ModbusMaster node;
#define modbusaddr 1

void preTransmission()
{
  //delay(1);
}

void postTransmission()
{
  //delay(1);
}

AsyncWebServer server(80);

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: Times New Roman; display: inline-block; text-align: center;}
    h2 {font-size: 2.3rem;}
    p {font-size: 1.9rem;}
    body {max-width: 400px; margin:0px auto; padding-bottom: 25px;}

  </style>
</head>
<body>
  <h2>ESP32 SDM230</h2>

</body>
</html>
)rawliteral";


void setup(){
  //serial monitor
  Serial.begin(9600);

  //baud rate SDM230
  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());
 
  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() {
  V = getRTU(0x0000);
  Serial.println("Voltage : " + String(V,2));
  delay(1000);
  A = getRTU(0x0006);
  Serial.println("Current : " + String(A,2));
  delay(1000);
  W = getRTU(0x000C);
  Serial.println("Active Power : " + String(W,2));
  delay(1000);
  Wh = getRTU(0x0156);
  Serial.println("Total Active Energy : " + String(Wh,2));
  delay(1000);
  PF = getRTU(0x001E);
  Serial.println("Power Factor : " + String(PF,2));
  delay(1000);
  F = getRTU(0x0046);
  Serial.println("Frequency : " + String(F,2));
  delay(1000);
  Serial.println("==================================");

  //here lies the problem
  server.send(200, "text/html", String(V,1));
 
}
In theory, there is no difference between theory and practice. But, in practice, there is.
 

Offline voltsandjolts

  • Supporter
  • ****
  • Posts: 2301
  • Country: gb
 

Offline PSR B1257Topic starter

  • Frequent Contributor
  • **
  • Posts: 330
  • Country: de
Re: ESP32 send data to async webserver
« Reply #2 on: April 05, 2023, 08:08:51 am »
But there is no such example for the ESPAsyncWebServer, or am I blind?

In theory, there is no difference between theory and practice. But, in practice, there is.
 

Offline PSR B1257Topic starter

  • Frequent Contributor
  • **
  • Posts: 330
  • Country: de
Re: ESP32 send data to async webserver
« Reply #3 on: April 10, 2023, 03:16:09 pm »
I am still not able to send data to a web page, even though there are countless examples 'out there'.
Take this one:
https://randomnerdtutorials.com/esp32-web-server-sent-events-sse/
Especially the part
Code: [Select]
String processor(const String& var){
  getSensorReadings();
  //Serial.println(var);
  if(var == "TEMPERATURE"){
    return String(temperature);
  }
  else if(var == "HUMIDITY"){
    return String(humidity);
  }
  else if(var == "PRESSURE"){
    return String(pressure);
  }
  return String();
}
is a mystery to me.
Where does "TEMPERATURE" in capital letters come from?
There is only a declaration for temperature in lower case letters.

So if I have a different code, which pulls the sensor data, I have no idea how to substitute this part of the code.

In theory, there is no difference between theory and practice. But, in practice, there is.
 

Offline Gixy

  • Regular Contributor
  • *
  • Posts: 232
  • Country: fr
Re: ESP32 send data to async webserver
« Reply #4 on: April 10, 2023, 08:44:34 pm »
TEMPERATURE, HUMIDITY and PRESSURE are patterns included in the string index_html which contains the whole web page. The processor function returns the value of the corresponding parameter reading which is then inserted in the page at the same location, allowing to display a real value when the page is loaded for the first time (instead of an empty field). Later, each field is accessed by its id (temp, hum or pres) and updated.
 

Offline PSR B1257Topic starter

  • Frequent Contributor
  • **
  • Posts: 330
  • Country: de
Re: ESP32 send data to async webserver
« Reply #5 on: April 11, 2023, 05:18:53 pm »
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.
Code: [Select]
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.
Code: [Select]
#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?
« Last Edit: April 11, 2023, 05:21:11 pm by PSR B1257 »
In theory, there is no difference between theory and practice. But, in practice, there is.
 

Offline floobydust

  • Super Contributor
  • ***
  • Posts: 7014
  • Country: ca
Re: ESP32 send data to async webserver
« Reply #6 on: April 11, 2023, 05:54:25 pm »
As far as I know, you have to use strcmp() to do string compares. Maybe this is not what you think: if (var == "TEMPERATURE")
 

Offline sokoloff

  • Super Contributor
  • ***
  • Posts: 1799
  • Country: us
Re: ESP32 send data to async webserver
« Reply #7 on: April 11, 2023, 06:21:38 pm »
Arduino is using c++. The c++ string class has an operator overload for == that does the sensible thing.
 

Offline porter

  • Contributor
  • Posts: 46
  • Country: us
Re: ESP32 send data to async webserver
« Reply #8 on: April 11, 2023, 06:31:29 pm »
Maybe check with expressif regarding this term:

Quote
Task watchdog got triggered. The following tasks did not reset the watchdog in time:

 

Offline PSR B1257Topic starter

  • Frequent Contributor
  • **
  • Posts: 330
  • Country: de
Re: ESP32 send data to async webserver
« Reply #9 on: April 12, 2023, 09:36:28 am »
Thank you guys, but honestly I don't know how to apply your suggestions.
Code: [Select]
if (var == "TEMPERATURE") comes from the example I'm using and should be correct.

But I found the mistake.
Code: [Select]
readSensors() was called two times. In the
Code: [Select]
String processor(....) and the
Code: [Select]
void loop() subroutine. The code compiled fine, but the ESP32 obviously did not like that at all.


In theory, there is no difference between theory and practice. But, in practice, there is.
 

Offline Gixy

  • Regular Contributor
  • *
  • Posts: 232
  • Country: fr
Re: ESP32 send data to async webserver
« Reply #10 on: April 12, 2023, 03:40:56 pm »
Seems to be a known issue with modbus framework on ESP, watchdog task triggering during Wifi init. Try to search for a workaround/update on Google and/or GitHub.
 

Online bookaboo

  • Frequent Contributor
  • **
  • Posts: 729
  • Country: ie
Re: ESP32 send data to async webserver
« Reply #11 on: April 12, 2023, 03:48:25 pm »
Sounds like a conflict between the WiFi and Modbus tasks, there are a load of limitations to what you can do concurrently with WiFi enabled on ESP32. Try running the webpage only with some dummy data, if that works then it's a conflict. Which exact ESP32 variant do you have?
 

Offline PSR B1257Topic starter

  • Frequent Contributor
  • **
  • Posts: 330
  • Country: de
Re: ESP32 send data to async webserver
« Reply #12 on: April 15, 2023, 03:20:54 pm »
Quote
Sounds like a conflict between the WiFi and Modbus tasks
Not only that, as it seems.
There should be three functions: power-meter readout via MODBUS, PWM generator with slider-interface and three buttons

Independently every script works fine. But trying to combining them in one 'soup' gives various problems.

The automagic update (SSE) of the power-meter readout and also the graphical representation of the switches does not work anymore. Interestingly, the magic trick 'invisible switch' still works. Meaning you cant see the switch, but knowing where it should be on the screen, you can toggle it.

I am using a ESP32 DevKitC4 with an WROOM32U modul.

Here comes the soup, for those who want to take a peak, and maybe have suggestions how to improve this mess.

Code: [Select]
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <ModbusMaster.h>
#include <SoftwareSerial.h>

const char* ssid = "...";
const char* password = "...";

const int pwm_pin = 14;

String slider_value = "0";

const int frequency = 1000;
const int pwm_channel = 0;
const int resolution = 8;

const char* input_parameter = "value";
const char* PARAM_INPUT_1 = "output";
const char* PARAM_INPUT_2 = "state";

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("==================================");
}

const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>ESP32 Wallbox</title>
  <style>
 
    html {font-family: Times New Roman; display: inline-block; text-align: center;}
    h2 {font-size: 2.3rem;}
    p {font-size: 1.9rem;}
    body {max-width: 400px; margin:0px auto; padding-bottom: 25px;}
   
    .slider { -webkit-appearance: none; margin: 14px; width: 360px; height: 25px; background: #38c0ff  ;
    outline: none; -webkit-transition: .2s; transition: opacity .2s;}
    .slider::-webkit-slider-thumb {-webkit-appearance: none; appearance: none; width: 35px; height: 35px; background:#01070a; cursor: pointer;}
    .slider::-moz-range-thumb { width: 35px; height: 35px; background: #01070a; cursor: pointer; }
   
    .switch {position: relative; display: inline; width: 120px; height: 68px}
    .switch input {display: none}
    .sliderSwitch {position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; border-radius: 6px}
    .sliderSwitch:before {position: absolute; content: ""; height: 52px; width: 52px; left: 8px; bottom: 8px; background-color: #fff; -webkit-transition: .4s; transition: .4s; border-radius: 3px}
    input:checked .sliderSwitch {background-color: #b30000}
    input:checked .sliderSwitch:before {-webkit-transform: translateX(52px); -ms-transform: translateX(52px); transform: translateX(52px)}
   
  </style>
</head>
<body>
  <h2>ESP32 Wallbox</h2>
  <p><span id="Export">%EXPORT%</span> kWh</p>
  <p><span id="textslider_value">%SLIDERVALUE%</span></p>
  <p><input type="range" onchange="updateSliderPWM(this)" id="pwmSlider" min="25" max="65" value="%SLIDERVALUE%" step="1" class="slider"></p>
  %BUTTONPLACEHOLDER%
<script>
function updateSliderPWM(element) {
  var slider_value = document.getElementById("pwmSlider").value;
  document.getElementById("textslider_value").innerHTML = slider_value*0.24;
  console.log(slider_value);
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "/slider?value=" slider_value, true);
  xhr.send();
}
function toggleCheckbox(element) {
  var xhr = new XMLHttpRequest();
  if(element.checked){ xhr.open("GET", "/update?output=" element.id "&state=1", true); }
  else { xhr.open("GET", "/update?output=" element.id "&state=0", true); }
  xhr.send();

    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("Export").innerHTML = e.data;
 }, false);

 }

}
</script>
</body>
</html>
)rawliteral";

String processor(const String& var){
  if (var == "SLIDERVALUE"){
    return slider_value;
  }

    if(var == "BUTTONPLACEHOLDER"){
    String buttons = "";
     
    buttons  = "<h4>L1</h4><label class="switch"><input type="checkbox" onchange="toggleCheckbox(this)" id="25" "   outputState(25)   "><span class="sliderSwitch"></span></label>";
    buttons  = "<h4>L2</h4><label class="switch"><input type="checkbox" onchange="toggleCheckbox(this)" id="26" "   outputState(26)   "><span class="sliderSwitch"></span></label>";
    buttons  = "<h4>L3</h4><label class="switch"><input type="checkbox" onchange="toggleCheckbox(this)" id="27" "   outputState(27)   "><span class="sliderSwitch"></span></label>";

    return buttons;
  }
  if(var == "EXPORT"){
    //only for testing purposes, I'm using the voltage readout
    return String(SDM_U);
  }

  return String();
}
String outputState(int output){
  if(digitalRead(output)){
    return "checked";
  }
  else {
    return "";
  }
}

void setup(){
  Serial.begin(115200);
  SerialMod.begin(2400);

  ledcSetup(pwm_channel, frequency, resolution);
  ledcAttachPin(pwm_pin, pwm_channel);
  ledcWrite(pwm_channel, slider_value.toInt());

  node.begin(modbusaddr, SerialMod);
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);

  pinMode(25, OUTPUT);
  digitalWrite(25, LOW);
  pinMode(26, OUTPUT);
  digitalWrite(26, LOW);
  pinMode(27, OUTPUT);
  digitalWrite(27, LOW);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting...");
  }

  Serial.println(WiFi.localIP());

  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/html", index_html, processor);
  });

  server.on("/slider", HTTP_GET, [] (AsyncWebServerRequest *request) {
    String message;

    if (request->hasParam(input_parameter)) {
      message = request->getParam(input_parameter)->value();
      slider_value = message;
      ledcWrite(pwm_channel, slider_value.toInt());
    }
    else {
      message = "No message sent";
    }
    Serial.println(message);
    });

  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/html", index_html, processor);
    });
     server.on("/update", HTTP_GET, [] (AsyncWebServerRequest *request) {
    String inputMessage1;
    String inputMessage2;
    if (request->hasParam(PARAM_INPUT_1) && request->hasParam(PARAM_INPUT_2)) {
      inputMessage1 = request->getParam(PARAM_INPUT_1)->value();
      inputMessage2 = request->getParam(PARAM_INPUT_2)->value();
      digitalWrite(inputMessage1.toInt(), inputMessage2.toInt());
    }
    else {
      inputMessage1 = "No message sent";
      inputMessage2 = "No message sent";
    }
   
    Serial.print("GPIO: ");
    Serial.print(inputMessage1);
    Serial.print(" - Set to: ");
    Serial.println(inputMessage2);
    request->send(200, "text/plain", "OK");
  });
 
  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());
 
  lastTime = millis();
  }
 
}
« Last Edit: April 15, 2023, 03:28:42 pm by PSR B1257 »
In theory, there is no difference between theory and practice. But, in practice, there is.
 

Offline sokoloff

  • Super Contributor
  • ***
  • Posts: 1799
  • Country: us
Re: ESP32 send data to async webserver
« Reply #13 on: April 15, 2023, 04:52:09 pm »
Get rid of the Delay statements and prints as one step. Or make them 1 ms instead of a series of 1 second delays.
 

Offline PSR B1257Topic starter

  • Frequent Contributor
  • **
  • Posts: 330
  • Country: de
Re: ESP32 send data to async webserver
« Reply #14 on: April 16, 2023, 08:16:00 am »
Since I don't need the serial output anyway for the final installation, I deleted all such lines of code. I also deleted the delay-statements. Even the SSE-statements were deleted.
All of that has not changed the result what so ever. The switches still are invisible.

By combining multiple codes, I got in the part
Code: [Select]
<body>
...
<script>
function updateSliderPWM(element) {
  var slider_value = document.getElementById("pwmSlider").value;
  document.getElementById("textslider_value").innerHTML = slider_value*0.24;
  console.log(slider_value);
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "/slider?value=" slider_value, true);
  xhr.send();
}
function toggleCheckbox(element) {
  var xhr = new XMLHttpRequest();
  if(element.checked){ xhr.open("GET", "/update?output=" element.id "&state=1", true); }
  else { xhr.open("GET", "/update?output=" element.id "&state=0", true); }
  xhr.send();

...
</script>
</body>
two lines of this code
Code: [Select]
var xhr = new XMLHttpRequest();Seem suspicious, so I changed it to xhr1 and xhr2

You might guessed it by now, no change...
In theory, there is no difference between theory and practice. But, in practice, there is.
 

Online bookaboo

  • Frequent Contributor
  • **
  • Posts: 729
  • Country: ie
Re: ESP32 send data to async webserver
« Reply #15 on: April 17, 2023, 06:15:13 am »
Try different pins for the sensor and modbus (possible conflict may be on the pins selected)
Beef up input capacitance, when WiFi is on it might pull the Vdd down, causing brownout symptoms. Experiment for best results, a few hundred µF should make a big difference.
If Arduino has RTOS give that a try.µF
If you into ESP32 for the long haul or professionally , give the espidf a try, it's steeper learning curve but easier to solve issues like this. For hobby, educational or one off projects sticking with Arduino for now is probably best.
 

Offline PSR B1257Topic starter

  • Frequent Contributor
  • **
  • Posts: 330
  • Country: de
Re: ESP32 send data to async webserver
« Reply #16 on: April 17, 2023, 08:38:35 am »
The supply voltage is no issue. The ESP32 works 'fine', there is 'just' the display issue. Somehow the code is not interpreted, as it should be. Apparently it is to do with MODBUS/software-serial since the switches are working without the MOSBUS-readout being part of the whole code.

I also tried different pins for the MODBUS-serial-interface (GPIO 16,17 which are labeled as TXD2 and RXD2). Makes also no difference, not that I expected that with using software-serial.
In theory, there is no difference between theory and practice. But, in practice, there is.
 

Offline Gixy

  • Regular Contributor
  • *
  • Posts: 232
  • Country: fr
Re: ESP32 send data to async webserver
« Reply #17 on: April 17, 2023, 11:55:26 am »
Again, if you google something like "ESP32 modbus wifi watchdog", you'll find a lot of solutions.
 

Offline PSR B1257Topic starter

  • Frequent Contributor
  • **
  • Posts: 330
  • Country: de
Re: ESP32 send data to async webserver
« Reply #18 on: April 18, 2023, 05:29:08 am »
The watchdog-problem is already solved (see post from 12 of April).
Or at least I can't see how this could still be a problem, if the switches are working but being invisible on the browser page.

I use the latest Arduino IDE and also the latest libraries (and every code is working independently, as mentioned).
In theory, there is no difference between theory and practice. But, in practice, there is.
 

Offline Gixy

  • Regular Contributor
  • *
  • Posts: 232
  • Country: fr
Re: ESP32 send data to async webserver
« Reply #19 on: April 18, 2023, 11:54:59 am »
You say using the Arduino framework but I don't see "#include Arduino.h" in your code. Is it included elsewhere?
 

Offline PSR B1257Topic starter

  • Frequent Contributor
  • **
  • Posts: 330
  • Country: de
In theory, there is no difference between theory and practice. But, in practice, there is.
 

Offline PSR B1257Topic starter

  • Frequent Contributor
  • **
  • Posts: 330
  • Country: de
Re: ESP32 send data to async webserver
« Reply #21 on: April 19, 2023, 11:24:30 am »
O deer, the codes are not the same  :palm:

I was messing around with the placement of the switches (would like to have them side by side) and changed
Code: [Select]
.switch {position: relative; display: inline-block; width: 120px; height: 68px}to
Code: [Select]
.switch {position: relative; display: inline; width: 120px; height: 68px}
Why this is causing the colors to vanish... :-// Further changing the "3-digit color-codes" to "6-digit color-codes" makes at least the sliding part of the switch visible  :o
In theory, there is no difference between theory and practice. But, in practice, there is.
 

Offline ab9nq

  • Newbie
  • Posts: 1
  • Country: us
    • ESPAsyncWebserver --datalogger
Re: ESP32 send data to async webserver
« Reply #22 on: August 30, 2023, 10:21:18 am »
Sharing ESPAsyncWebServer method of sending data to web page:

Gist of method

Method is used for my web site

Complete project code


William
« Last Edit: August 30, 2023, 10:30:31 am by ab9nq »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf