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.
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.
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:
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.
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.
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.
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:
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.