ESP8266 Exception (0)

I could do

for(int i=0;i<(sampletime_ms/inst_sampletime_ms);i++){

Taking off the '='. Or just replace all of the (sampletime_ms/inst_sampletime_ms) by '60', which is the nautral result of the operation. I was doing like this to standarise and leave everything dependant of the sampletimes, as they might vary from one project to another.

Why do you think that the '-1' could break the system, when it is working fine (and thus doing well the operation) for many hours? Thank you!

Using the constant instead of direct typed number is a right way, but in that case you should use the constant (sampletime_ms/inst_sampletime_ms) also everywhere instead of magic number 60 in definitions of the related arrays like:

The most correct thing would be to define the constant (sampletime_ms/inst_sampletime_ms) as a macro or, preferably, as a constexpr and then use it in array definitions and expressions in code:

constexpr unsigned long inst_sampletime_ms = 5000;
constexpr unsigned long sampletime_ms = 300000;
constexpr unsigned long N_sampletimes =  sampletime_ms /  inst_sampletime_ms;
....
float historic_pm1[N_sampletimes];
...
for(int i=0; i < N_sampletimes ;i++){
        
    avg_pm1 = avg_pm1 + historic_pm1[i];
    historic_pm1[i] = 0;

in the processor's view, calling something "constexpr" doesn't mean the memory in which it lives
cannot be written by software at runtime. It means the compiler won't let you use it as a destination.

in C at least.

How does your comment relate to the discussion? Or are you just sharing what you know?

The exception code for divide by zero is 6, so it is not divide by zero. Exception code 0 is an invalid instruction. Usually caused by an invalid branch target or a bad pointer. Overwriting the return address on the call stack could cause an invalid branch target.

are you sure you never get more than 20 commas ?

      String strs[20];
      int StringCount = 0;
      while (payload.length() > 0) {

        int index = payload.indexOf(',');
        if (index == -1) {

          strs[StringCount++] = payload;
          break;

        } else {

          strs[StringCount++] = payload.substring(0, index);
          payload = payload.substring(index + 1);

        }

      }

you never check

If that was the case the sketch would crash instantly.

Oh man i didn't even see that part of the code.

First of all it really is not a good idea to create an Array of Strings, (though i think the sep8266webserver does as well) but as with any array you need to make sure you don't write beyond the bounds of the array.

Hmm ah well in the end you have double buffered, and all will be destroyed, (local variables) but the bounds of the array, you need to check, or even better, Scan payload for the amount of commas, create an array of the size you need etc. Still this can also be done with char* more efficiently without the double buffer. Count the commas, create an array of pointers , move payload to a char array and add a null terminator, scan for the comma, replace the comma with a 'null' and assign the next pointer to be the character after the null.

You should be able to process in place. But of course the bounds of the array might be you illegal instruction.

I am running two ESP with all above recommendations, except the payload ones (I am just printing the value of payload at each iteration).

@Deva_Rishi, @J-M-L:
I think this payload function is the issue. The payload mainly consists of two different values separated with a coma. Then, those two values are filled in into two variables: caibration and max_co2. There is always only one coma and two values. Example of payload: "1,400".

I am thinking that the last part of the funcion is specifically makign the system crash at some point:

calibration = strs[0].toInt();
max_co2 = strs[1].toInt();

Imagine that httpResponseCode == HTTP_CODE_OK, but for some reason https.getString(); fails to receive the payload correctly (can this happen??). Then, strs[0].toInt() and/or strs[1].toInt() would not exist and would make the system crash. What do you think?

Also, what would be the proper way to assign those two values of payload (example, "1,400") to two different int variables?

If https.getString() fails to receive the payload correctly, the result will be an empty string. Operation toInt() from the empty String will returns zero, but not to make the system crash.

Add a printout for this while loop to see what’s going on and if it is incriminated

After leaving both ESP running all night, I found out the following:

  1. The reset reason is: 2 Fatal exception.

  2. The payload value just before the reset is "0,800".

I am doing a test now without the following piece of code, to make sure the crash comes or not from it:

if (httpResponseCode == HTTP_CODE_OK) {
     
      String payload = https.getString();

      Serial.println  (payload);
      
      String strs[20];
      int StringCount = 0;
      while (payload.length() > 0){
        
        int index = payload.indexOf(',');
        if (index == -1){
          
          strs[StringCount++] = payload;
          break;
    
        }else{
        
          strs[StringCount++] = payload.substring(0, index);
          payload = payload.substring(index+1);
    
        }
  
      }

      calibration = strs[0].toInt();
      max_co2 = strs[1].toInt();

Any other suggestions?

if you know you are awaiting only for 2 coma separated integers in the payload, then just extract those. Don't complicate things

if (httpResponseCode == HTTP_CODE_OK) {
  String payload = https.getString();
  Serial.println  (payload);
  int commaIndex = payload.indexOf(',');
  if (commaIndex != -1) {
      // Found a comma in the payload
      String intNumber1_str = payload.substring(0, commaIndex);
      String intNumber2_str = payload.substring(commaIndex + 1);
      calibration = intNumber1_str.toInt();
      max_co2 = intNumber2_str.toInt();
      •••
  } else {
    Serial.println("No comma found in the payload");
  }
}

The ESP crashed again with the above code after running 30-40 minuts

OK so it's likely not part of the issue

can you post the new code that crashed and when you compile the summary of memory information?

Sure, Topic Starter is updated with the new code and summary of memory, which is pretty much like the previous one.

I have read that one possible cause is that the watchdog timer firing and reseting my ESP CPU. The Exception Decoder is showing an issue in readSensorData() , which can be due to large delays in the function between the readings of the sensors (1 second delay, 4 times in the function). I will test now again with 0.1 seconds delay between different sensor readings.

"OP" means "Opener of the Post" (or something like) - that is, it's you

Personally I prefer "TS" = "Topic Starter" , but for some reason this is not used on this forum.

for me ➜ OP = Original Poster

Reducing the delay to 0.1 seconds resulted in nothing different. Given that I have SD_ZH03B::readData() in the Exception Decoder, I will eliminate all references to that sensor in the code and try again.

Any more suggestions are more than welcome :slight_smile:

So I spotted the error: ZH03B sensor was producing the exception as, when I eliminated all references to its readings, the sensor has been working uninterruptedly for more than 24 hours. This is the piece of code I eliminated:

char printbuf1[80];

if ( ZH03B.readData() ) {
    
    pm1 = ZH03B.getPM1_0();
    historic_pm1[measurements_counter] = ZH03B.getPM1_0();

    pm25 = ZH03B.getPM2_5();
    historic_pm25[measurements_counter] = ZH03B.getPM2_5();

    pm10 = ZH03B.getPM10_0();
    historic_pm10[measurements_counter] = ZH03B.getPM10_0();
    
  } else {
    
    Serial.println(F("ZH03B Error reading stream or Check Sum Error"));
    
  }

However, I have another ESP working fine with this sensor with a slightly different code. This code for the sensor is working fine during months:

char printbuf1[80];
  if( ZH03B.readData() ) {
    sprintf(printbuf1, "PM1.0, PM2.5, PM10=[%d %d %d]", ZH03B.getPM1_0(), ZH03B.getPM2_5(), ZH03B.getPM10_0() );
    Serial.println(printbuf1);
  } else {   
    Serial.println( "ZH03B Error reading stream or Check Sum Error" );
  } 
  pm1=ZH03B.getPM1_0();

I am thinking: in the first piece of code, if ZH03B fails randomly to read one time, there could be an issue with historic_pm1[measurements_counter], historic_pm25[measurements_counter] and historic_pm10[measurements_counter]. Also, this would explain that the ESP is crashing at different and random times from the start.

Please, let me know what do you think