Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

ESP8266 Panic crashing when updating HTML template though Serial communication with Arduino

I have basic knowledge of ESP and Arduino, I was writing a program to remotely start my 3 phase motor. In-circuit 4 sensors are connected to Arduino and Sending data to nodemcu ESP8266 by serial communication. I want to display the sensor readings to a web page (Localhost) with Button, My code:

// Import required libraries
#include "ESP8266WiFi.h"
#include "ESPAsyncWebServer.h"

#include <SoftwareSerial.h>
//D6 = Rx & D5 = Tx
SoftwareSerial nodemcu(D6, D5);

// include library to read and write from flash memory
#include <EEPROM.h>

// define the number of bytes you want to access
#define EEPROM_SIZE 1

// Set to true to define Relay as Normally Open (NO)
#define RELAY_NO    true

// Set number of relays
#define NUM_RELAYS  1

// Assign each GPIO to a relay
int relayGPIOs[NUM_RELAYS] = {5};

//Min Voltage
int minVol = 0;
int minCur = 0;
int livVol = 0;
int livCur = 0;
String values, sensor_data;
String vol1;
String vol2;
String vol3;
String cur1;


const char* ssid = "ESP";

const char* PARAM_INPUT_1 = "relay";
const char* PARAM_INPUT_2 = "state";
const char* PARAM_INPUT_3 = "led";

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);

const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    html {font-family: Arial; display: inline-block; text-align: center;}
    h2 {font-size: 3.0rem;}
    p {font-size: 3.0rem;}
    body {max-width: 600px; margin:0px auto; padding-bottom: 25px;}
    .switch {position: relative; display: inline-block; width: 120px; height: 68px} 
    .switch input {display: none}
    .slider {position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; border-radius: 34px}
    .slider:before {position: absolute; content: ""; height: 52px; width: 52px; left: 8px; bottom: 8px; background-color: #fff; -webkit-transition: .4s; transition: .4s; border-radius: 68px}
    input:checked+.slider {background-color: #2196F3}
    input:checked+.slider:before {-webkit-transform: translateX(52px); -ms-transform: translateX(52px); transform: translateX(52px)}
  </style>
</head>
<body>
  <h2>Switch</h2>
  %BUTTONPLACEHOLDER%
<script>function toggleCheckbox(element) {
  var xhr = new XMLHttpRequest();
  if(element.checked){ xhr.open("GET", "/update?relay="+element.id+"&state=1", true); }
  else { xhr.open("GET", "/update?relay="+element.id+"&state=0", true); }
  xhr.send();
}</script>
%PHASE%

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





String relayState(int numRelay){
  if(RELAY_NO){
    if(digitalRead(relayGPIOs[numRelay-1])){
      return "";
    }
    else {
      return "checked";
    }
  }
  else {
    if(digitalRead(relayGPIOs[numRelay-1])){
      return "checked";
    }
    else {
      return "";
    }
  }
  return "";
}

void setup(){
  // Serial port for debugging purposes
  Serial.begin(9600);

  //Arduino Serial communication
  nodemcu.begin(9600);
  while (!Serial) continue;

  // initialize EEPROM with predefined size
  EEPROM.begin(EEPROM_SIZE);

 // Set all relays to off when the program starts - if set to Normally Open (NO), the relay is off when you set the relay to HIGH
  for(int i=1; i<=NUM_RELAYS; i++){
    pinMode(relayGPIOs[i-1], OUTPUT);
    if(RELAY_NO){
      digitalWrite(relayGPIOs[i-1], HIGH);
    }
    else{
      digitalWrite(relayGPIOs[i-1], LOW);
    }
  }

  digitalWrite(relayGPIOs[0], EEPROM.read(0));
  digitalWrite(relayGPIOs[1], EEPROM.read(0));
  digitalWrite(relayGPIOs[2], EEPROM.read(0));
  digitalWrite(relayGPIOs[3], EEPROM.read(0));


  WiFi.softAP(ssid);

  // Print ESP8266 Local IP Address
  Serial.println(WiFi.localIP());

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

  // Send a GET request to <ESP_IP>/update?relay=<inputMessage>&state=<inputMessage2>
  server.on("/update", HTTP_GET, [] (AsyncWebServerRequest *request) {
    String inputMessage;
    String inputParam;
    String inputMessage2;
    String inputParam2;
    // GET input1 value on <ESP_IP>/update?relay=<inputMessage>
    if (request->hasParam(PARAM_INPUT_1) & request->hasParam(PARAM_INPUT_2)) {
      inputMessage = request->getParam(PARAM_INPUT_1)->value();
      inputParam = PARAM_INPUT_1;
      inputMessage2 = request->getParam(PARAM_INPUT_2)->value();
      inputParam2 = PARAM_INPUT_2;
      if(RELAY_NO){
        Serial.print("NO ");
        digitalWrite(relayGPIOs[inputMessage.toInt()-1], !inputMessage2.toInt());
        digitalWrite(relayGPIOs[0], !inputMessage2.toInt());
        digitalWrite(relayGPIOs[1], !inputMessage2.toInt());
        digitalWrite(relayGPIOs[2], !inputMessage2.toInt());
        digitalWrite(relayGPIOs[3], !inputMessage2.toInt());
        EEPROM.write(0, !inputMessage2.toInt());
        EEPROM.commit();
      }
      else{
        Serial.print("NC ");
        digitalWrite(relayGPIOs[inputMessage.toInt()-1], inputMessage2.toInt());
        digitalWrite(relayGPIOs[0], !inputMessage2.toInt());
        digitalWrite(relayGPIOs[1], !inputMessage2.toInt());
        digitalWrite(relayGPIOs[2], !inputMessage2.toInt());
        digitalWrite(relayGPIOs[3], !inputMessage2.toInt());
        EEPROM.write(0, !inputMessage2.toInt());
        EEPROM.commit();
      }
    }
    else {
      inputMessage = "No message sent";
      inputParam = "none";
    }
    Serial.println(inputMessage + inputMessage2);
    request->send(200, "text/plain", "OK");
  });

  // Send a GET request to <ESP_IP>/Led?led=1
  server.on("/Led", HTTP_GET, [] (AsyncWebServerRequest *request) {
    
    request->send(200, "text/plain", String(digitalRead(relayGPIOs[0])));
  });
  // Start server
  server.begin();
}
  
void loop() {

  if(livCur<<minCur & livVol<<minVol & digitalRead(relayGPIOs[0])==0){
        Serial.print("Off");
        digitalWrite(relayGPIOs[0], 1);
        EEPROM.write(0, 0);
        EEPROM.commit();
  }

  bool Sr =false;

  if(nodemcu.available()){
    
        //get sensor data from serial put in sensor_data
        sensor_data=nodemcu.readString(); 
        Sr=true;    
        delay(1000);
        
    }
  
delay(1000);

  if(Sr==true){  
    
  values=sensor_data;
  
  //get comma indexes from values variable
  int fristCommaIndex = values.indexOf(',');
  int secondCommaIndex = values.indexOf(',', fristCommaIndex+1);
  int thirdCommaIndex = values.indexOf(',', secondCommaIndex + 1);
  int fourthCommaIndex = values.indexOf(',', thirdCommaIndex + 1);
  
  //get sensors data from values variable by  spliting by commas and put in to variables  
  String vol1 = values.substring(0, fristCommaIndex);
  String vol2 = values.substring(fristCommaIndex+1, secondCommaIndex);
  String vol3 = values.substring(secondCommaIndex+1, thirdCommaIndex);
  String cur1 = values.substring(thirdCommaIndex+1);

  }

  

}

// Replaces placeholder with button section in your web page
String processor(const String& var){
  //Serial.println(var);
  if(var == "BUTTONPLACEHOLDER"){
    String buttons ="";
    for(int i=1; i<=NUM_RELAYS; i++){
      String relayStateValue = relayState(i);
      buttons+= "<h4>Motor</h4><label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\"" + String(i) + "\" "+ relayStateValue +"><span class=\"slider\"></span></label>";
    }
    return buttons;
  }

  if(var == "PHASE"){
    String phaseread ="";

    if(nodemcu.available()){
  String values="";
  String sensor_data="";
  sensor_data=nodemcu.readString(); 
  delay(1000);

  values=sensor_data;
  
  //get comma indexes from values variable
  int fristCommaIndex = values.indexOf(',');
  int secondCommaIndex = values.indexOf(',', fristCommaIndex+1);
  int thirdCommaIndex = values.indexOf(',', secondCommaIndex + 1);
  int fourthCommaIndex = values.indexOf(',', thirdCommaIndex + 1);
  
  //get sensors data from values variable by  spliting by commas and put in to variables  
  String vol1 = values.substring(0, fristCommaIndex);
  String vol2 = values.substring(fristCommaIndex+1, secondCommaIndex);
  String vol3 = values.substring(secondCommaIndex+1, thirdCommaIndex);
  String cur1 = values.substring(thirdCommaIndex+1);

}
    
    phaseread+= "<h3>Phase 1:  " + String(vol1) + " </h3><br><h3>Phase 2:  " + String(vol2) + " </h3><br><h3>Phase 3:  " + String(vol3) + " </h3><br><h3>Current:  " + String(cur1) + " </h3><br>";
    
    return phaseread;
  }
  
  return String();
}

Everything works normally till I connected ESP8266 and Arduino and refreshed the Web page to check sensor values, it got crashed

Panic C:\Users\Aman\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266\core_esp8266_main.cpp:103 __yield

>>>stack>>>

ctx: sys
sp: 3fffead0 end: 3fffffb0 offset: 01b0
3fffec80:  3ffeec04 3ffeec04 3fffecc0 40209f01  
3fffec90:  3ffeec04 3ffeec04 3ffeec04 4020935d  
3fffeca0:  3ffe88aa 3fffed84 3fffecc0 402093e8  
3fffecb0:  3fffed90 3fffed84 3ffe88aa 40202dce  
3fffecc0:  3fff144c 0000001f 00000016 40100b8d  
3fffecd0:  00000000 402094cf 00000000 00000000  
3fffece0:  00000000 00000000 00000000 00000000  
3fffecf0:  3fff1434 0000000f 00000000 3fff141c  
3fffed00:  0000000f 00000000 3fff1b04 0000000f  
3fffed10:  00000000 000004d2 000004d2 401008ac  
3fffed20:  3fff1d58 3fffed65 3fffed84 40100cf0  
3fffed30:  3fff1d58 3fffed65 3fffed84 000001c5  
3fffed40:  3fff1d57 3fffed65 3fffed84 40209fac  
3fffed50:  3fff1ba4 00000000 3fff1a8c 40206ffc  
3fffed60:  53414850 4c500045 48454341 45444c4f  
3fffed70:  3ffe0052 00000000 00000000 3fff15f4  
3fffed80:  00007fff 00000000 00000000 00000000  
3fffed90:  3fff0c44 0000000f 00000005 00000001  
3fffeda0:  000001c6 3fff1d57 3fff1d5d 3fff1baa  
3fffedb0:  00000428 3fff1d70 3fff1fd1 0000008e  
3fffedc0:  3fffc278 4010297c 3fffc200 3fff1b1c  
3fffedd0:  00000430 40207264 00000430 3fff1b1c  
3fffede0:  3fff1ba4 00000000 3fff1a8c 40207295  
3fffedf0:  00000000 4bc6a7f0 b2b020c4 00000000  
3fffee00:  00000000 00000000 4bc6a7f0 00000000  
3fffee10:  3fff0ebc 00000001 00000430 00015c4f  
3fffee20:  00000000 3fff09be 3fff24ec 3fff0ebc  
3fffee30:  00000430 0000001c 3fff0ebc 4020a0ac  
3fffee40:  00000430 3fffee80 3fff0dd4 40203fb9  
3fffee50:  00000430 3fffee80 3fff0dd4 40207ed9  
3fffee60:  3ffef1d0 3fff0944 3ffef1c9 3ffef1e4  
3fffee70:  3fff0944 00000218 3fff0dd4 40208486  
3fffee80:  3fff0094 3fff0c2c 3fff13cc 3ffef1e4  
3fffee90:  3fff0944 3ffef1c8 3ffef1c9 40215c38  
3fffeea0:  4021f120 3fffdcc0 3ffe92a0 00000038  
3fffeeb0:  3ffef3ac 0104a8c0 00000048 40100afc  
3fffeec0:  00000000 00000036 3fffef30 3ffef3ac  
3fffeed0:  3ffef458 3fff13cc 3fff13ea 4021a8e5  
3fffeee0:  00000014 00000000 3ffef3ac 401008ac  
3fffeef0:  3fffdc90 3ffefddc 3fff1b04 3ffefe44  
3fffef00:  00000008 3ffef3ac 3fff13cc 402128e1  
3fffef10:  3fffdc90 3ffefddc 3fff1b04 40212717  
3fffef20:  40231c56 3ffefddc 3fff1b04 40231c67  
3fffef30:  3fff13dc 3fff13cc 00000000 3fffdcc0  
3fffef40:  4022c94b 00000000 3fff1b04 40233a17  
3fffef50:  40000f49 3fffdab0 3fffdab0 40000f49  
3fffef60:  40000e19 40001878 00000002 00000000  
3fffef70:  3fffff10 aa55aa55 00000011 40104dc4  
3fffef80:  40104dca 00000002 00000000 2a20712f  
3fffef90:  4010000d 8d009cd0 d6f0a078 ef40cead  
3fffefa0:  40100dc0 3fffef3c 40100d6d 3fffff18  
3fffefb0:  3fffffc0 00000000 00000000 feefeffe  
3fffefc0:  feefeffe feefeffe feefeffe feefeffe  
3fffefd0:  feefeffe feefeffe feefeffe feefeffe  
3fffefe0:  feefeffe feefeffe feefeffe feefeffe  
3fffeff0:  feefeffe feefeffe feefeffe feefeffe  
3ffff000:  feefeffe feefeffe feefeffe feefeffe  
3ffff010:  feefeffe feefeffe feefeffe feefeffe  
3ffff020:  feefeffe feefeffe feefeffe feefeffe  
3ffff030:  feefeffe feefeffe feefeffe feefeffe  
3ffff040:  feefeffe feefeffe feefeffe feefeffe  
3ffff050:  feefeffe feefeffe feefeffe feefeffe  
3ffff060:  feefeffe feefeffe feefeffe feefeffe  
3ffff070:  feefeffe feefeffe feefeffe feefeffe  
3ffff080:  feefeffe feefeffe feefeffe feefeffe  
3ffff090:  feefeffe feefeffe feefeffe feefeffe  
3ffff0a0:  feefeffe feefeffe feefeffe feefeffe  
3ffff0b0:  feefeffe feefeffe feefeffe feefeffe  
3ffff0c0:  feefeffe feefeffe feefeffe feefeffe  
3ffff0d0:  feefeffe feefeffe feefeffe feefeffe  
3ffff0e0:  feefeffe feefeffe feefeffe feefeffe  
3ffff0f0:  feefeffe feefeffe feefeffe feefeffe  
3ffff100:  feefeffe feefeffe feefeffe feefeffe  
3ffff110:  feefeffe feefeffe feefeffe feefeffe  
3ffff120:  feefeffe feefeffe feefeffe feefeffe  
3ffff130:  feefeffe feefeffe feefeffe feefeffe  
3ffff140:  feefeffe feefeffe feefeffe feefeffe  
3ffff150:  feefeffe feefeffe feefeffe feefeffe  
3ffff160:  feefeffe feefeffe feefeffe feefeffe  
3ffff170:  feefeffe feefeffe feefeffe feefeffe  
3ffff180:  feefeffe feefeffe feefeffe feefeffe  
3ffff190:  feefeffe feefeffe feefeffe feefeffe  
3ffff1a0:  feefeffe feefeffe feefeffe feefeffe  
3ffff1b0:  feefeffe feefeffe feefeffe feefeffe  
3ffff1c0:  feefeffe feefeffe feefeffe feefeffe  
3ffff1d0:  feefeffe feefeffe feefeffe feefeffe  
3ffff1e0:  feefeffe feefeffe feefeffe feefeffe  
3ffff1f0:  feefeffe feefeffe feefeffe feefeffe  
3ffff200:  feefeffe feefeffe feefeffe feefeffe  
3ffff210:  feefeffe feefeffe feefeffe feefeffe  
3ffff220:  feefeffe feefeffe feefeffe feefeffe  
3ffff230:  feefeffe feefeffe feefeffe feefeffe  
3ffff240:  feefeffe feefeffe feefeffe feefeffe  
3ffff250:  feefeffe feefeffe feefeffe feefeffe  
3ffff260:  feefeffe feefeffe feefeffe feefeffe  
3ffff270:  feefeffe feefeffe feefeffe feefeffe  
3ffff280:  feefeffe feefeffe feefeffe feefeffe

Can anybody please help? It all started after I have added the below lines in my code (For updating sensor values on the web Page)

 if(var == "PHASE"){
    String phaseread ="";

    if(nodemcu.available()){
  String values="";
  String sensor_data="";
  sensor_data=nodemcu.readString(); 
  delay(1000);

  values=sensor_data;
  
  //get comma indexes from values variable
  int fristCommaIndex = values.indexOf(',');
  int secondCommaIndex = values.indexOf(',', fristCommaIndex+1);
  int thirdCommaIndex = values.indexOf(',', secondCommaIndex + 1);
  int fourthCommaIndex = values.indexOf(',', thirdCommaIndex + 1);
  
  //get sensors data from values variable by  spliting by commas and put in to variables  
  String vol1 = values.substring(0, fristCommaIndex);
  String vol2 = values.substring(fristCommaIndex+1, secondCommaIndex);
  String vol3 = values.substring(secondCommaIndex+1, thirdCommaIndex);
  String cur1 = values.substring(thirdCommaIndex+1);

}
    
    phaseread+= "<h3>Phase 1:  " + String(vol1) + " </h3><br><h3>Phase 2:  " + String(vol2) + " </h3><br><h3>Phase 3:  " + String(vol3) + " </h3><br><h3>Current:  " + String(cur1) + " </h3><br>";
    
    return phaseread;
  }

EDIT: Decoded result

Decoding stack results
0x40209f01: __yield() at C:\Users\Aman\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266\core_esp8266_main.cpp line 105
0x4020935d: Stream::timedRead() at C:\Users\Aman\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266\Stream.cpp line 37
0x402093e8: Stream::readString() at C:\Users\Aman\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266\Stream.cpp line 243
0x40202dce: processor(String const&) at C:\Users\Aman\Documents\Arduino\sketch_apr19a/sketch_apr19a.ino line 254
0x40100b8d: realloc at C:\Users\Aman\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266\umm_malloc\umm_malloc.c line 1504
0x402094cf: String::changeBuffer(unsigned int) at C:\Users\Aman\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266\WString.cpp line 156
0x401008ac: _umm_free at C:\Users\Aman\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266\umm_malloc\umm_malloc.c line 1300
0x40100cf0: free at C:\Users\Aman\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266\umm_malloc\umm_malloc.c line 1760
0x40209fac: std::_Function_handler ::_M_invoke(std::_Any_data const&, String const&) at c:\users\Aman\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\2.5.0-3-20ed2b9\xtensa-lx106-elf\include\c++\4.8.2/functional line 2058
0x40206ffc: AsyncAbstractResponse::_fillBufferAndProcessTemplates(unsigned char*, unsigned int) at C:\Users\Aman\Documents\Arduino\libraries\ESPAsyncWebServer\src\WebResponses.cpp line 442
0x40100b8d: realloc at C:\Users\Aman\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266\umm_malloc\umm_malloc.c line 1504
0x40207264: AsyncAbstractResponse::_ack(AsyncWebServerRequest*, unsigned int, unsigned int) at C:\Users\Aman\Documents\Arduino\libraries\ESPAsyncWebServer\src\WebResponses.cpp line 300
0x40207295: AsyncAbstractResponse::_ack(AsyncWebServerRequest*, unsigned int, unsigned int) at C:\Users\Aman\Documents\Arduino\libraries\ESPAsyncWebServer\src\WebResponses.cpp line 315
0x4020a0ac: AsyncWebServerRequest::_onAck(unsigned int, unsigned int) at C:\Users\Aman\Documents\Arduino\libraries\ESPAsyncWebServer\src\WebRequest.cpp line 201
0x40203fb9: std::_Function_handler ::_M_invoke(const std::_Any_data &, void *, AsyncClient *, unsigned int, unsigned int) at c:\users\Aman\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\2.5.0-3-20ed2b9\xtensa-lx106-elf\include\c++\4.8.2/functional line 2073
0x40207ed9: AsyncClient::_sent(std::shared_ptr &, tcp_pcb*, unsigned short) at C:\Users\Aman\Documents\Arduino\libraries\ESPAsyncTCP\src\ESPAsyncTCP.cpp line 557
0x40208486: AsyncClient::_s_sent(void*, tcp_pcb*, unsigned short) at C:\Users\Aman\Documents\Arduino\libraries\ESPAsyncTCP\src\ESPAsyncTCP.cpp line 753
0x40215c38: tcp_input at core/tcp_in.c line 469
0x40100afc: malloc at C:\Users\Aman\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266\umm_malloc\umm_malloc.c line 1673
0x4021a8e5: ip4_input at core/ipv4/ip4.c line 710
0x401008ac: _umm_free at C:\Users\Aman\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266\umm_malloc\umm_malloc.c line 1300
0x402128e1: ethernet_input_LWIP2 at netif/ethernet.c line 188
0x40212717: esp2glue_ethernet_input at glue-lwip/lwip-git.c line 430
0x40231c56: ethernet_input at glue-esp/lwip-esp.c line 363
0x40231c67: ethernet_input at glue-esp/lwip-esp.c line 371

 

-------------------------------

Code was taken from c vs c++.

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Off-topic

Don't F&*% the planet.

Over the weekend, Atlassian ran a New York Times ad calling on business leaders to join us in committing to a net zero future through the Science-Based Targets Initiative. As part of our gl...

291 views 0 13
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you