ESP8266 Ardiuno cloud firmware with Soft Serial crashes on exception (0) randomly

Hi,

I am new to this forum as I thought to get some expert opinion being a newbie in this field.

I have following program running on my ESP8266 (ESP-12F) which uses Hardware & Software serials for flashing/ serial printing & communicating with an inverter, respectively.

#include "thingProperties.h"
#include <SoftwareSerial.h>

SoftwareSerial SUART(12, 13);  //SRX = DPin-4, STX = DPin-5

const byte numChars = 127;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing


boolean newData = false;

char *string, *found;

int gsValues[30] = {0};

unsigned long previousMillis = 0;

const long interval = 5000; //milliseconds

void setup() {
 // Initialize serial and wait for port to open:
 Serial.begin(2400);
 SUART.begin(2400);
 // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
 delay(1500); 

 // Defined in thingProperties.h
 initProperties();

 // Connect to Arduino IoT Cloud
 ArduinoCloud.begin(ArduinoIoTPreferredConnection);
 
 /*
    The following function allows you to obtain more information
    related to the state of network and IoT Cloud connection and errors
    the higher number the more granular information you’ll get.
    The default is 0 (only errors).
    Maximum is 4
*/
 setDebugMessageLevel(2);
 ArduinoCloud.printDebugInfo();
 WiFi.setPhyMode(WIFI_PHY_MODE_11G);
}

void loop() {
 ArduinoCloud.update();
 // Your code here
 if (ArduinoCloud.connected())
 {
   unsigned long currentMillis = millis();
   if (currentMillis - previousMillis >= interval) {

   previousMillis = currentMillis;
     
     SUART.print("^P005GS\r");
   }
   recvWithStartEndMarkers();
   if (newData == true) {
       strcpy(tempChars, receivedChars);
           // this temporary copy is necessary to protect the original data
           //   because strtok() used in parseData() replaces the commas with \0
       parseData();
       //showParsedData();
       newData = false;

     aco_apwr = gsValues[5];
     gd_volt = gsValues[0]/10;
     inv_temp = gsValues[13];
     pv1_pwr = gsValues[16];
     pv1_volt = (int)gsValues[18]/10.0;
     
     //delay(5000);
   }
 }

}
void recvWithStartEndMarkers() {
   static boolean recvInProgress = false;
   static byte ndx = 0;
   char startMarker = '^';
   char endMarker = '\r';
   char rc;

   while (SUART.available() > 0 && newData == false) {
       rc = SUART.read();
       //Serial.print(rc);
       if (recvInProgress == true) {
           if (rc != endMarker) {
               receivedChars[ndx] = rc;
               ndx++;
               if (ndx >= numChars) {
                   ndx = numChars - 1;
               }
           }
           else {
               receivedChars[ndx] = '\0'; // terminate the string
               recvInProgress = false;
               ndx = 0;
               newData = true;
               Serial.println("E_detected");
           }
       }

       else if (rc == startMarker) {
           recvInProgress = true;
           Serial.println("S_detected");
       }
   }
}

//============

void parseData() {      // split the data into its parts
   
   
   string = strdup(tempChars);
   char tmp[4];
   //printf("Original string: '%s'\n",string);
   found = strsep(&string,",");
   //printf("%s\n",found);
   int j = 0;
   for (int i = 0; i <= strlen(found); i++)
   {
       if (i >= 4)
       {
           tmp[j] = found[i];
           j++;
       }
   }
   
   gsValues[0] = atoi(tmp);
   
   unsigned int idx = 1;
   while( (found = strsep(&string,",")) != NULL )
   {
       gsValues[idx] = atoi(found);
       //Serial.println(found);
       //Serial.println(gsValues[idx]);
       idx++;
   }


}


void showParsedData() {
   //Serial.println(messageFromPC);
   for (int i = 0; i <=29; i++)
     Serial.println(gsValues[i]);     
}

The ESP connects and transmits perfectly but randomly throws Exception (0) and does not resets/ connects to cloud again. I have tried changing code, attaching device again and ssid keys in cloud etc. But, unable get rid of this issue.

Please, suggest what I may be doing wrong or try to resolve the said issue.

Regards

Can you share the error code

Thanks for the reply.
This is what I'm getting now and it keeps on retrying.

S_detected
E_detected
ArduinoIoTCloudTCP::handle_Disconnect MQTT client connection lost
Disconnected from Arduino IoT Cloud
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2
TimeServiceClass::getRemoteTime cannot get time from NTP, fallback on connection handler
TimeServiceClass::getRemoteTime cannot get time from connection handler
ArduinoIoTCloudTCP::handle_SyncTime could not get valid time. Retrying now.
TimeServiceClass::getRemoteTime cannot get time from NTP, fallback on connection handler
TimeServiceClass::getRemoteTime cannot get time from connection handler
ArduinoIoTCloudTCP::handle_SyncTime could not get valid time. Retrying now.
TimeServiceClass::getRemoteTime cannot get time from NTP, fallback on connection handler
TimeServiceClass::getRemoteTime cannot get time from connection handler
ArduinoIoTCloudTCP::handle_SyncTime could not get valid time. Retrying now.
TimeServiceClass::getRemoteTime cannot get time from NTP, fallback on connection handler
TimeServiceClass::getRemoteTime cannot get time from connection handler
ArduinoIoTCloudTCP::handle_SyncTime could not get valid time. Retrying now.

Can you verify the key and other details in this header file are same as in cloud

Yes, I have also tried adding new device and wifi device.

It disconnects so this may mean that settings are connect.
I was thinking to buy a plan but seems like an issue on cloud side may be.

Verify credentials: Make sure the device credentials (e.g., device ID, authentication tokens) are correct and haven't expired.

Check the network connectivity and ensure that there are no restrictions preventing access to cloud.

Try with different network

I have come to know that if I disconnect the device i.e. serial is not available then it is kept connected. But, when I connect back it randomly disconnects. So, there is some issue with my Serial functions handling text interpretation etc.

Hi @x4ce. I recommend focusing on your code that assigns to the arrays while debugging this problem:

If the program writes past the allocated bounds of the tmp or gsValues arrays, that could cause the board to crash.

Thanks for your response. I have tried with following codes to make sure arrays index don't go beyond.

string = strdup(tempChars);
    char tmp[4];
    //printf("Original string: '%s'\n",string);
    found = strsep(&string,",");
    //printf("%s\n",found);
    int j = 0;
    for (int i = 0; i <= strlen(found); i++)
    {
        if (i >= 4 && i <= 7)
        {
            tmp[j] = found[i];
            j++;
        }
    }
    
    gsValues[0] = atoi(tmp);
    
    unsigned int idx = 1;
    while( (found = strsep(&string,",")) != NULL )
    {
        gsValues[idx] = atoi(found);
        //Serial.println(found);
        //Serial.println(gsValues[idx]);
        idx++;
        if (idx >= 29)
          idx = 29;
    }

But the program still crashes.

I am getting this error and it never gets connected again...

S_detected
E_detected
ArduinoIoTCloudTCP::handle_Disconnect MQTT client connection lost
Disconnected from Arduino IoT Cloud
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -1
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2


I have got some other logs:

E_detected
S_detected

Panic /run/arduino/directories-data/packages/esp8266/hardware/esp8266/2.5.0/libraries/ESP8266WiFi/src/include/ClientContext.h:439 size_t ClientContext::_write: Assertion '_datasource == nullptr' failed.

>>>stack>>>

ctx: sys
sp: 3ffffcf0 end: 3fffffb0 offset: 01b0
3ffffea0:  40251264 00000000 00000010 40204aa1  
3ffffeb0:  00000000 00000000 3fff92cc 40204b75  
3ffffec0:  00000000 3fffc6fc 0000001e 00000001  
3ffffed0:  00000008 00000000 3ffef758 40205cc8  
3ffffee0:  0000002b 3ffeddac 00000012 3fff1214  
3ffffef0:  4023bc98 3ffeddac 00000000 4023ddcc  
3fffff00:  4023b6c5 3ffedf28 00000002 3ffeee98  
3fffff10:  3fffdc10 00000000 3ffef758 40205d67  
3fffff20:  0000003c 3fff2ec4 3ffef758 402045f5  
3fffff30:  0000001e 3fff2ec4 3ffef758 40205c33  
3fffff40:  3ffed280 3fff2ec4 3ffef758 40204e98  
3fffff50:  3ffed280 4010118f 3fff2ec4 4020400c  
3fffff60:  401011ca 00000000 00000002 3ffeee98  
3fffff70:  3fffdc10 00000000 3fff2ec4 4025127c  
3fffff80:  40000f49 3fffdab0 3fffdab0 40000f49  
3fffff90:  40000e19 40001878 00000002 00000000  
3fffffa0:  3fffff10 aa55aa55 000000db 40104d34  
<<<stack<<<

last failed alloc call: 40220048(644)
��***** Arduino IoT Cloud - 2.4.1 *****
Device ID: 9a188ee7-55b5-4185-803e-9e56627c6225
MQTT Broker: iot.arduino.cc:8884
WiFi.status(): 6
Connected to "UntrustedWifi20X"
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -1
S_detected
Connected to Arduino IoT Cloud

....

S_detected
E_detected

Exception (28):
epc1=0x4021153c epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

>>>stack>>>

ctx: cont
sp: 3fff0840 end: 3fff0a30 offset: 01a0
3fff09e0:  3ffe882c 00000000 3ffef9bc 4020c0d4  
3fff09f0:  00000000 3ffef4ac 00000000 3fff0a68  
3fff0a00:  3fffdad0 00000000 3ffef3ac 40206b65  
3fff0a10:  3fffdad0 00000000 3fff0a38 4020c978  
3fff0a20:  feefeffe feefeffe 3ffe8544 40100e89  
<<<stack<<<

last failed alloc call: 402114EB(127)
���***** Arduino IoT Cloud - 2.4.1 *****
Device ID: 9a188ee7-55b5-4185-803e-9e56627c6225
MQTT Broker: iot.arduino.cc:8884
WiFi.status(): 6
Connected to "UntrustedWifi20X"
S_detected

Exception (0):
epc1=0x4020c9c0 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

>>>stack>>>

ctx: sys
sp: 3ffffbf0 end: 3fffffb0 offset: 01a0
3ffffd90:  4107c9fd 00000000 000000dd 3ffffeb0  
3ffffda0:  02181000 3ffed460 3ffec2fa 00000020  
3ffffdb0:  ffffffff 0000000c 00000000 4010021b  
3ffffdc0:  4010047a 40100440 3fffc258 40100500  
3ffffdd0:  e0036035 00000030 00000010 ffffffff  
3ffffde0:  4023b174 3fff1088 00000010 00000022  
3ffffdf0:  3fffc200 40100440 3fffc258 4000050c  
3ffffe00:  4000437d 00000030 0000000b ffffffff  
3ffffe10:  60000200 00000008 ffffffff 80000000  
3ffffe20:  20000000 3fff9148 80000000 203fc180  
3ffffe30:  00000000 3fffc6fc 00000000 3fff914c  
3ffffe40:  000000f4 003fc180 60000600 00000030  
3ffffe50:  402386c5 3fff381e 3fff2eac 4021767d  
3ffffe60:  4010564c 004d4fa8 3fff122c 00000000  
3ffffe70:  3ffeece0 3fff122c 00000000 4023d0a7  
3ffffe80:  00000000 0030b370 4023d112 3fff122c  
3ffffe90:  3fff1214 ffffffc2 00000000 3fffdbc0  
3ffffea0:  00000000 00000000 40000001 7fffffff  
3ffffeb0:  00000000 00110101 00640104 0000004f  
3ffffec0:  3ffec224 000000d6 3ffec243 3ffec218  
3ffffed0:  00000000 3ffec224 3ffec236 3ffec24c  
3ffffee0:  00000000 3ffec2c8 3ffec252 3ffec2e0  
3ffffef0:  00000000 400042db 00000000 00000000  
3fffff00:  40004b31 3fff904c 000001f4 003fc080  
3fffff10:  401061d4 000001f4 3ffedc70 40100c60  
3fffff20:  40106571 3fff904c 00000000 009c4b25  
3fffff30:  3ffec200 000000fe 3ffedc70 40249cd6  
3fffff40:  40248cf1 40248cfa 3ffedae4 3ffeece0  
3fffff50:  4024d1dd 3fffdcc0 3ffe9ab8 3ffe9ab8  
3fffff60:  4024d222 3fffdab0 00000000 3fffdcb0  
3fffff70:  3ffeecf8 3fffdab0 00000000 4024ce1b  
3fffff80:  40000f49 40000f49 3fffdab0 40000f49  
3fffff90:  40000e19 40001878 00000002 3fff0a30  
3fffffa0:  3fffff10 aa55aa55 000000db 40104d34  
<<<stack<<<
��***** Arduino IoT Cloud - 2.4.1 *****
Device ID: 9a188ee7-55b5-4185-803e-9e56627c6225
MQTT Broker: iot.arduino.cc:8884
WiFi.status(): 6
Connected to "UntrustedWifi20X"
S_detected
Connected to Arduino IoT Cloud
Thing ID: 76c0c8d1-f1a0-48bd-814e-5cce2772c431
E_detected
S_detected

.....
S_detected
E_detected
ArduinoIoTCloudTCP::handle_Disconnect MQTT client connection lost
Disconnected from Arduino IoT Cloud
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2

Panic /run/arduino/directories-data/packages/esp8266/hardware/esp8266/2.5.0/libraries/ESP8266WiFi/src/include/ClientContext.h:439 size_t ClientContext::_write: Assertion '_datasource == nullptr' failed.

>>>stack>>>

ctx: sys
sp: 3ffffcf0 end: 3fffffb0 offset: 01b0
3ffffea0:  40000f49 00000000 00000010 40204aa1  
3ffffeb0:  00000000 00000000 3fff3864 40204b75  
3ffffec0:  00000000 3fffc6fc 0000001e 00000001  
3ffffed0:  00000008 00000000 3ffef758 40205cc8  
3ffffee0:  000000db 3ffeddac 00000012 3fff1214  
3ffffef0:  4023bc98 3ffeddac 00000000 4023ddcc  
3fffff00:  4023b6c5 3ffedf28 00000002 3ffeee98  
3fffff10:  3fffdc10 00000000 3ffef758 40205d67  
3fffff20:  0000003c 3fff2eac 3ffef758 402045f5  
3fffff30:  0000001e 3fff2eac 3ffef758 40205c33  
3fffff40:  3ffed280 3fff2eac 3ffef758 40204e98  
3fffff50:  3ffed280 4010118f 3fff2eac 4020400c  
3fffff60:  401011ca 00000000 00000002 3ffeee98  
3fffff70:  3fffdc10 00000000 3fff2eac 4025127c  
3fffff80:  40000f49 3fffdab0 3fffdab0 40000f49  
3fffff90:  40000e19 40001878 00000002 3fffffb0  
3fffffa0:  3fffff10 aa55aa55 000000db 40104d34  
<<<stack<<<

last failed alloc call: 40220048(644)
���***** Arduino IoT Cloud - 2.4.1 *****
Device ID: 9a188ee7-55b5-4185-803e-9e56627c6225
MQTT Broker: iot.arduino.cc:8884
WiFi.status(): 6
Connected to "UntrustedWifi20X"
S_detected

Exception (0):
epc1=0x4020c9c0 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

>>>stack>>>

ctx: sys
sp: 3ffffbf0 end: 3fffffb0 offset: 01a0
3ffffd90:  ea105630 40100440 3fffc258 4000050c  
3ffffda0:  40219035 00000030 00000010 00000020  
3ffffdb0:  ffffffff 0000000c 00000000 4010021b  
3ffffdc0:  4010047a 3fff0e48 3fff37a4 40100500  
3ffffdd0:  e0036035 0000e9e7 0000e900 00000020  
3ffffde0:  ffffffff 0000000c 00000000 00000022  
3ffffdf0:  3fffc200 40100440 3fffc258 4000050c  
3ffffe00:  4000438f 00000030 00000010 ffffffff  
3ffffe10:  60000200 00000001 28012800 80000000  
3ffffe20:  20000000 3fff9068 80000000 203fc0a0  
3ffffe30:  00000000 3fffc6fc 00000001 3fff906c  
3ffffe40:  000001d4 003fc0a0 60000600 00000030  
3ffffe50:  00000008 00000002 3ffedf2c 0000007f  
3ffffe60:  000000d0 00000000 3ffe99d0 3ffed3c0  
3ffffe70:  3fff1214 00000008 3ffee5f8 00000030  
3ffffe80:  3fff0bb4 0000103f 0000103f 401009d8  
3ffffe90:  00000000 00000000 3fff92d4 40100e20  
3ffffea0:  feefeffe 00000000 3fffd9d0 40220070  
3ffffeb0:  3fff0fc8 3fff1070 3fff92d4 4021acea  
3ffffec0:  3fff37a4 3fff0e48 00000000 00000001  
3ffffed0:  00000086 401006a4 00000014 40100c28  
3ffffee0:  3fff0fc8 00000000 00000000 401006a4  
3ffffef0:  00000000 400042db 000027cf 4024b07c  
3fffff00:  40004b31 3fff904c 000001f4 003fc080  
3fffff10:  401061d4 000001f4 3ffedc70 40100c60  
3fffff20:  40106571 3fff904c 00000000 009be3bc  
3fffff30:  3ffeb578 000000ac 3ffedc70 40249cd6  
3fffff40:  40248cf1 40248cfa 3ffedae4 3ffeece0  
3fffff50:  4024d1dd 3fffdcc0 3ffe9a40 3ffe9a40  
3fffff60:  4024d222 3fffdab0 00000000 3fffdcb0  
3fffff70:  3ffeed00 3fffdab0 00000000 4020c887  
3fffff80:  40000f49 40000f49 3fffdab0 40000f49  
3fffff90:  40000e19 40001878 00000002 3fffffb0  
3fffffa0:  3fffff10 aa55aa55 000000db 40104d34  
<<<stack<<<
���***** Arduino IoT Cloud - 2.4.1 *****
Device ID: 9a188ee7-55b5-4185-803e-9e56627c6225
MQTT Broker: iot.arduino.cc:8884
WiFi.status(): 6
Connected to "UntrustedWifi20X"
S_detected
Connected to Arduino IoT Cloud
Thing ID: 76c0c8d1-f1a0-48bd-814e-5cce2772c431
E_detected
S_detected
E_detected
S_detected

....
detected
E_detected

Exception (28):
epc1=0x4021153c epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

>>>stack>>>

ctx: cont
sp: 3fff0840 end: 3fff0a30 offset: 01a0
3fff09e0:  3ffe882c 00000000 3ffef9bc 4020c0d4  
3fff09f0:  00000000 3ffef4ac 00000000 3fff0a68  
3fff0a00:  3fffdad0 00000000 3ffef3ac 40206b65  
3fff0a10:  3fffdad0 00000000 3fff0a38 4020c978  
3fff0a20:  feefeffe feefeffe 3ffe8544 40100e89  
<<<stack<<<

last failed alloc call: 402114EB(110)
�***** Arduino IoT Cloud - 2.4.1 *****
Device ID: 9a188ee7-55b5-4185-803e-9e56627c6225
MQTT Broker: iot.arduino.cc:8884
WiFi.status(): 6
Connected to "UntrustedWifi20X"
S_detected
Connected to Arduino IoT Cloud
Thing ID: 76c0c8d1-f1a0-48bd-814e-5cce2772c431
E_detected
S_detected
E_detected
.....

S_detected
E_detected
ArduinoIoTCloudTCP::handle_Disconnect MQTT client connection lost
Disconnected from Arduino IoT Cloud
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2

Try ESP exception decoder to understand the error code

The ESP Exception Decoder requires access to the .elf file that is generated through the sketch compilation. That file is not available when the sketch is compiled via Arduino Cloud Editor, because the file is on the Arduino Cloud server. So unfortunately it is not possible to use the decoder on this stack trace.

It would be possible if you uploaded the Thing sketch to the board using Arduino IDE, then decoded the stack trace that is produced when the board is running the program uploaded via Arduino IDE. In this case, Arduino IDE saves the .elf file to the hard drive on your computer, so it is available for the exception decoder to find.

1 Like

I am pasting the stack trace correctly but don't know getting this error.

You must copy the entire content from Serial Monitor. You missed the end of the output so the decoder couldn't recognize it.

There is a bug in the Arduino IDE Serial Monitor that makes it so you can only select the text that is visible in Serial Monitor. If you scroll the panel while selecting, the text that is out of view is unselected. So make sure to resize the Serial Monitor panel to make all the content visible before selecting and copying it. You can resize the Serial Monitor panel by hovering the mouse pointer over the border between the Serial Monitor panel and the editor panel of the Arduino IDE window. You will see the mouse pointer change to the :up_down_arrow: resize pointer. Click and drag the border up to increase the size of the Serial Monitor panel.

Thanks for the detailed guidance. I have got this after decoding.

There a lots of decodings but they all refer to other header/ library files not main.c.

Regards

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.