ESP8266 watch dog reset because of exception (29)

Hi guys,

Recently I am using ESP8266 to delivery some message to the Azure Cloud.

In my sketch, I am tyring to use AT+ command to let my esp switch the case and do what I want it to do.

I have attached my code, I really wish someone could give me a hand. I used the right power supply and I have more than 30% memory. I do not know what happens.

I suspect on several things:

1)is that because I used too many while loops
2) char *I used several times in the loop but with different name as you can see in the code

I defined four char *, two of them are at :


char* store_serial_data[100]={};
char AlertPayload[100]={};
myStringTwo.toCharArray(AlertPayload, sizeof(AlertPayload));
Serial.print(myStringTwo);
char delimiter[]=",";

char* save_alert = strtok(AlertPayload,delimiter);
store_serial_data[alert_index]=save_alert;
Serial.print(store_serial_data*);*
while(save_alert!=NULL)
{
alert_index++;
save_alert=strtok(NULL,delimiter);
store_serial_data[alert_index]=save_alert;
Serial.print(store_serial_data*);
Serial.print("\n");
_
}_
_
two of them are at :
_
char store_serial_data_normal[100]={};

char AlertPayload[100]={};
Serial.print(myStringTwo);
myStringTwo.toCharArray(AlertPayload, sizeof(AlertPayload));
Serial.write("\n debuger 1");
char delimiter[]=",";
char save_normal = strtok(AlertPayload,delimiter);

store_serial_data_normal[normal_index]=save_normal;
Serial.println(store_serial_data_normal[normal_index]);
while(save_normal!=NULL)
{
normal_index++;
Serial.write("\n debuger 5");
save_normal=strtok(NULL,delimiter);
store_serial_data_normal[normal_index]=save_normal;
Serial.println("\n");
Serial.println(store_serial_data_normal[normal_index]);
}
******************************************************
However, I have kept seeing the following message when I try to push the message to the clound,
someone also says might because ESPWebserver and HTTP request taking place at the same time but I doubt.
Exception (29):
epc1=0x4000e1b2 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000004 depc=0x00000000
ctx: cont
sp: 3fff3b40 end: 3fff4440 offset: 01a0
ets Jan 8 2013,rst cause:2, boot mode:(3,6)
load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v4024c5b0
~ld
[code][code][/code][/code]
i really hope some one can help me.
```
*void setup() {

delay(1000);
 
 Serial.begin(115200);

}

void loop()
{
if (Serial.available())
{
  String c = Serial.readString();

if (c=="AT+ALERT")
 {
    state =  Alert_st;
    system_rtc_mem_write(RTCMEMSTR, &state, 1);
     Serial.write("\n Transmission Notice Mode");
     Serial.flush();
 }

switch(case)
{

case Alert_st:
 
  message_check=0;
 
  Serial.write("AT+ACK");  
 
         while(AlertLoop==0)
       {
         delay(250);
         
if(Serial.available())
           {
int alert_index=0;
String myString= Serial.readStringUntil('\n');
//{911,3,1,0,22.5}
Serial.print(myString);
Serial.write("\n debuger 1");
int firstClosingBracket = myString.indexOf('{');
myString.remove(firstClosingBracket, 1);
String myStringTwo=myString;
int SecondClosingBracket = myString.indexOf('}');
myStringTwo.remove(SecondClosingBracket, 1);
char* store_serial_data[100]={};
char AlertPayload[100]={};
myStringTwo.toCharArray(AlertPayload, sizeof(AlertPayload));
Serial.print(myStringTwo);
char delimiter[]=",";
 
char* save_alert = strtok(AlertPayload,delimiter);
store_serial_data[alert_index]=save_alert;
Serial.print(store_serial_data[i]);
 while(save_alert!=NULL)
{
alert_index++;
save_alert=strtok(NULL,delimiter);
store_serial_data[alert_index]=save_alert;
Serial.print(store_serial_data[i]);
Serial.print("\n");

}
 
if (String(store_serial_data[0])=="911")
{
alert_temperature = String(store_serial_data[4]).toFloat();
my_alert_b1=String(store_serial_data[2]).toFloat();
my_alert_b0=String(store_serial_data[3]).toFloat();
Serial.print("alert_temperature:\n");
Serial.print(alert_temperature);
system_rtc_mem_write(77, &alert_temperature, 4);
system_rtc_mem_write(81, &my_alert_b1, 1);
system_rtc_mem_write(82, &my_alert_b0, 1);
Serial.write("AT+ACK");
}
else
{
 
 Serial.write("AT+NACK");
 state=init_st;
 system_rtc_mem_write(RTCMEMSTR, &state, 1);
 ESP.deepSleep(sleep_t);

}

Serial.write("\n debuger 2");
 while(AlertLoop==0)
{      
delay(250);
Serial.write("\n debuger 3");
delay(250);
 
if(Serial.available())
  {            
  int normal_index=0;
 String myString= Serial.readStringUntil('\n');
  Serial.print(myString);
 //{400,3,25.20,24.30,25.90,500,1,20}
 int firstClosingBracket = myString.indexOf('{');
 myString.remove(firstClosingBracket, 1);
  String myStringTwo=myString;
 int SecondClosingBracket = myStringTwo.indexOf('}');
 myStringTwo.remove(SecondClosingBracket, 1);
 char* store_serial_data_normal[100]={};
 char AlertPayload[100]={};
 Serial.print(myStringTwo);
 myStringTwo.toCharArray(AlertPayload, sizeof(AlertPayload));
 Serial.write("\n debuger 1");

char delimiter[]=",";
 char* save_normal = strtok(AlertPayload,delimiter);
 store_serial_data_normal[normal_index]=save_normal;
 Serial.println(store_serial_data_normal[normal_index]);
while(save_normal!=NULL)
 {
normal_index++;
Serial.write("\n debuger 5");
save_normal=strtok(NULL,delimiter);
store_serial_data_normal[normal_index]=save_normal;
Serial.println("\n");
Serial.println(store_serial_data_normal[normal_index]);

}

if (String(store_serial_data_normal[0])=="400")
 {
int dataLength = String(store_serial_data_normal[1]).toInt();
Serial.print(dataLength);
system_rtc_mem_write(80, &dataLength, 4);
for(j=0;j<dataLength;j++)
{
store_location=65+j;
data_s4= String(store_serial_data_normal[2+j]).toFloat();
system_rtc_mem_write(store_location, &data_s4, 4);
Serial.println("\n");
Serial.println(temp_data);
delay(250);
 
}

Serial.print("I am printing J\n");
Serial.print(j);
  battery_data=String(store_serial_data_normal[dataLength+4]).toInt();
  system_rtc_mem_write(78, &battery_data, 1);
Serial.write("AT+ACK");
start_t=millis();
while (AlertLoop==0)
 {
Serial.write("\n debuger 4");
delay(250);
 if (Serial.available())
{
 String myString = Serial.readString();
 
if(myString=="AT+WIFISTART")
 {
state=Wifi_init_st;
system_rtc_mem_write(RTCMEMSTR, &state, 1);
AlertLoop=1;
 }                    
 
}
 if (millis()-start_t>timesout)
{
 Serial.write("AT+NACK");  
 state=init_st;
 
 system_rtc_mem_write(RTCMEMSTR, &state, 1);
 ESP.deepSleep(sleep_t);
}

yield();
 }

}
else if (String(store_serial_data_normal[0])=="500")
 {
 
dataLength=0;
system_rtc_mem_write(80, &dataLength, 4);
  battery_data=String(store_serial_data_normal[2]).toInt();
  system_rtc_mem_write(78, &battery_data, 1);
  Serial.write("AT+ACK");
 while (AlertLoop==0)
 {
delay(250);
 if (Serial.available())
{
 String myString = Serial.readString();
 
if(myString=="AT+WIFISTART")
 {
state=Wifi_init_st;
system_rtc_mem_write(RTCMEMSTR, &state, 1);
AlertLoop=1;
 }                    
 
}
 if (millis()-start_t>timesout)
{
 Serial.write("AT+NACK");  
 state=init_st;
 
 system_rtc_mem_write(RTCMEMSTR, &state, 1);
 ESP.deepSleep(sleep_t);
}

yield();
 }
 
 }
else
 {
Serial.write("AT+NACK");
state=init_st;
system_rtc_mem_write(RTCMEMSTR, &state, 1);
ESP.deepSleep(sleep_t);
 }
 }
 
 
if (millis()-start_t>timesout)
 {
 state=init_st;
 ESP.deepSleep(sleep_t);
 }

yield();
 
}

}

if (millis()-start_t>timesout)
 {
 state=init_st;
 ESP.deepSleep(sleep_t);
 }

yield();

}
break;

case init_st:
           Serial.write("\n Initial State");      
       
           //sleep here;                                                                                          
           delay(5000);
          break;
 
      default:
       state = init_st;
       system_rtc_mem_write(RTCMEMSTR, &state, 1);
      break;

}
}
_
```_
_
Cao.h (9.4 KB)*_

   char* store_serial_data[100]={};

An array of 100 pointers means that you have 100 strings somewhere. That's going to use a lot of memory.

You need to post ALL of your code, in code tags!

Hi,

Sorry

I am new to this forum, I tried to post all my code here but seems it reaches the limit.
I attached the code it's called the Cao.h

Also, what is the length you would recommend for the pointer array and is that possible to use String array such as String mystg[100];

If it is too big what is the proper way to clear it up after I used it every time.

I tried to use memset() seems doesn't work out.

The message gives you a ton of usefull information. Just google for ESP8266 exception causes.

I suspect you're writing to memory you're not supposed to, this can be caused by invalid pointers.

Your code is a mess, please format it properly, and try not to use so much Strings, there's absolutely no need to, and they turn your heap into Swiss cheese.

Pieter

Hi Pieter,

Thanks for your response, I searched all the exception code seems there is no solid answer. Would you mind to give me a little bit more explanation about "invalid pointers" The only place I think I did not use the pointer properly would be shown below, I was trying to receive a string from serial port e.g.{11,22,33,44,55,66} I need to part this string and save each item by using the "," delimiter, that is why I declared a char* to save all the values.

char delimiter[]=",";
                  char* myptr = strtok(AlertPayload,delimiter);
                  store_serial_data[normal_index]=myptr;
                  Serial.println(store_serial_data[normal_index]);
                    while(myptr!=NULL)
                  {
                    normal_index++;
                    Serial.write("\n debuger 5");
                    myptr=strtok(NULL,delimiter);
                    store_serial_data[normal_index]=myptr;
                    Serial.println("\n");
                    Serial.println(store_serial_data[normal_index]);
                    
                  }

item(s) stored in store_serial_data[] may be expired when you exit loop()

                  char* myptr = strtok(AlertPayload,delimiter);
                  store_serial_data[normal_index]=myptr;

You have a pointer, with the dumb name myptr. It points to some memory location. You make an element of the array point to the same place, even if that is a NULL pointer.

                  Serial.println(store_serial_data[normal_index]);
                    while(myptr!=NULL)
                  {
                    normal_index++;
                    Serial.write("\n debuger 5");
                    myptr=strtok(NULL,delimiter);
                    store_serial_data[normal_index]=myptr;

You make myptr point somewhere else, and make another element of the array point to the same place. Now, you have 2 elements in the array and myptr all pointing to the SAME location. You repeat that until finally myptr points to nowhere.

At that point, what are ALL of the elements of the array pointing to? They ALL point to THE SAME PLACE! (Nowhere, that is.)

That is NOT even close to how to store the individual tokens in an array. You MUST copy the pointed to data, NOT just create another pointer to that memory.

Hi Paul,

Sorry I just saw your post.

I changed the code to the following, things seems getting worse:

I used strcpy(store_Serial_data[normal_index_noAlert_st],ptr);

              char* ptr;
              ptr = strtok(AlertPayload,delimiter);
              strcpy(store_Serial_data[normal_index_noAlert_st],ptr);
              mySerial.print("\n");
              mySerial.print(store_Serial_data[normal_index_noAlert_st]);
			
                while(ptr!=NULL)
              {
                normal_index_noAlert_st++;
                ptr=strtok(NULL,delimiter);
                strcpy(store_Serial_data[normal_index_noAlert_st],ptr);
                mySerial.print("\n");
                mySerial.print(store_Serial_data[normal_index_noAlert_st]);
                    delay(500);
                
              }

I suggest that you post your snippets at snippets-r-us.

How are you sure about proper constant voltage to esp? Did you check with oscilliscope?

What i suggest is soldering 1000uf and 0.1uf cap right on vcc and gnd pins in esp.

Also i did same circuit on breadboard sometimes was restarting the only thing that solved restarts was soldering all wires and components on board.

Spent a month then oscilliscope shows spikes even though multimeter wasnt reacting for that fast spikes.

Hi Paul,

Will do.

Recap:

  char* myptr = strtok(AlertPayload,delimiter);
  store_serial_data[normal_index]=myptr;

becomes:

 char* ptr;
 ptr = strtok(AlertPayload,delimiter);
 strcpy(store_Serial_data[normal_index_noAlert_st],ptr);

And also basically I used

strcpy(store_Serial_data[normal_index_noAlert_st],ptr);

to copy the value from pointer ptr to store_Serial_data

Then read the data from store_Serial_data but this time I got exception 28:

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

I am a little confused about what you said"You MUST copy the pointed to data, NOT just create another pointer to that memory."

In order for me to use the element in the pointer array char *ptr, what is the proper way to do it.

I also tried:

 String store_Serial_data[20]={};

make the store_Serial_data like a string array and assign String(ptr) to it but I get the same exception.

I checked the exception table it says :
28 LoadProhibitedCause A load referenced a page mapped with an attribute that does not permit loads
Region Protection or MMU

EVERY time you make code changes, you need to repost ALL of your code. I can't begin to follow what you are doing from the snippets.

Hi Paul,

Sorry about that.

Please see the whole code,for some reason it works now by doing the following

void setup() {

 
  delay(1000);
  
  Serial.begin(115200);

}
void loop() {
  int normalLoop=0;

  if (Serial.available())
{
   String c = Serial.readStringUntil('\n');

if (c=="AT+NOALERT")
  {
     state =  noAlert_st;
     system_rtc_mem_write(RTCMEMSTR, &state, 1);
        mySerial.print("\n NO ALERT MODE");
      ;
  }
}

switch(case)
{
case noAlert_st:
   mySerial.print("At noAlert_st state\n");
  
   
            while (normalLoop==0)
          {
            Serial.write("AT+ACK");
            Serial.write('\n');
            delay(1000);
           // Serial.write("noAlert_st Testp Point 2 \n");
              if(Serial.available())
            {    
                      
              String myString= Serial.readStringUntil('\n');
              Serial.flush();
              mySerial.print(myString);
               mySerial.print("\n");
              //{400,12,25.20,24.30,25.90,25.20,24.30,25.90,25.20,24.30,25.90,25.20,24.30,25.90,500,1,20}
              int firstClosingBracket = myString.indexOf('{');
              myString.remove(firstClosingBracket, 1);
              String myStringTwo=myString;
              int SecondClosingBracket = myStringTwo.indexOf('}');
              myStringTwo.remove(SecondClosingBracket, 1);
               
             String store_Serial_data[20]={};
              char AlertPayload[100]={};
              myStringTwo.toCharArray(AlertPayload, sizeof(AlertPayload));
               
              const char delimiter[]=",";
              char* ptr=NULL;
              ptr = strtok(AlertPayload,delimiter);
            store_Serial_data[0]=ptr;
        mySerial.print('\n');
        mySerial.print(store_Serial_data[0]);
              //strcpy(store_Serial_data[normal_index_noAlert_st],ptr);
              mySerial.print("\n");
           
                while(ptr!=NULL)
              {
                normal_index_noAlert_st++;
                ptr=strtok(NULL,delimiter);
                  store_Serial_data[normal_index_noAlert_st]=ptr;
                mySerial.print("\n");
               mySerial.print(store_Serial_data[normal_index_noAlert_st]);
                    delay(500);
                
              }
              
                if (store_Serial_data[0]=="400" and store_Serial_data[1].charAt(0)=='C')
              {
                  dataLength = 12;
                 
              
                  
                  for(int j=0;j<dataLength;j++)
                {
                  store_location=65+j;
                  data_s4= store_Serial_data[2+j].toFloat();
                  
                  system_rtc_mem_write(store_location, &data_s4, 4);
                     delay(250);
                   mySerial.print("\n");
                   mySerial.print(data_s4);
                     
                   mySerial.print("\nstore_location");
                  mySerial.print(store_location);
                  
                }
           
        
       
             battery_data=store_Serial_data[dataLength+4].toInt();
             system_rtc_mem_write(78, &battery_data, 1);
                message_check=1;
                 Serial.write("AT+ACK");
                 Serial.write('\n');
        
              }
             
            }
            
             
            yield();
          }
          
   normalLoop=0;
   
    break;
}

 default:
        state = init_st;
        system_rtc_mem_write(RTCMEMSTR, &state, 1);
       break;
}

surepic:
How are you sure about proper constant voltage to esp? Did you check with oscilliscope?

What i suggest is soldering 1000uf and 0.1uf cap right on vcc and gnd pins in esp.

Also i did same circuit on breadboard sometimes was restarting the only thing that solved restarts was soldering all wires and components on board.

Spent a month then oscilliscope shows spikes even though multimeter wasnt reacting for that fast spikes.

Hi,

I used battery to supply the ESP since I need use battery for my application instead of DC from the socket.

But I would definitely try to put them all on PCB so I can exclude the connection issue.

Your code doesn't even compile, there are many undeclared variables, you have missing brackets, and it is completely unreadable.