Exception (0) where is the problem?

Hey there,

I have a wind sensor connected to my ESP8266 in order to read the wind speed.
Unfortunately, after a short period of time, the ESP crashes and gives me the following output:

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

ctx: sys 
sp: 3ffffc30 end: 3fffffb0 offset: 01a0

>>>stack>>>
3ffffdd0:  4010706c 80af1999 00000020 40104aca  
3ffffde0:  ffffffff 00000020 40240000 ffffff1a  
3ffffdf0:  00000006 00000006 42d60000 00000022  
3ffffe00:  3fffc200 40107034 3fffc258 4000050c  
3ffffe10:  40004396 00000030 00000016 ffffffff  
3ffffe20:  60000200 00000003 02030702 80000000  
3ffffe30:  20000000 3fff0540 80000000 203fc0a0  
3ffffe40:  00000000 3fffc6fc 3ffe8d6c 3fff053c  
3ffffe50:  00000254 003fc0a0 60000600 00000030  
3ffffe60:  000000e2 3ffe93c5 3ffe93c0 40201c1d  
3ffffe70:  401070d0 00000002 00000000 401070ba  
3ffffe80:  ffffffff 00000020 3ffefd1c 00000000  
3ffffe90:  00000000 00000000 0000001f 4010175d  
3ffffea0:  4000050c 40107034 3fffc258 4000050c  
3ffffeb0:  40000f68 00000030 0000001c ffffffff  
3ffffec0:  40000f58 00000000 00000020 00000000  
3ffffed0:  00000013 4021ae9c 3ffe8d6c 00000001  
3ffffee0:  ffffffff 3ffefd08 3ffe8d6c 3fffdab0  
3ffffef0:  00000000 3fffdcb0 3ffe8928 00000030  
3fffff00:  00000000 400042db 00000064 60000600  
3fffff10:  40004b31 3fff0494 000002f4 003fc000  
3fffff20:  4010268e 3ffe8900 3ffe90a0 4010750c  
3fffff30:  4021a969 3ffe90a0 3ffe8900 01e20270  
3fffff40:  3fff0494 00001000 4021ae02 00000008  
3fffff50:  40102a80 00000000 4021aeaf 3ffe9154  
3fffff60:  3ffe8900 0049107c 3ffe8900 60000600  
3fffff70:  4020344d 3ffe9154 3ffe8900 01e1da36  
3fffff80:  40203492 3fffdab0 00000000 3fffdcb0  
3fffff90:  3ffe8910 00000000 40000f65 3fffdab0  
3fffffa0:  40000f49 000182d0 3fffdab0 40000f49  
<<<stack<<<

 ets Jan  8 2013,rst cause:2, boot mode:(1,6)


 ets Jan  8 2013,rst cause:4, boot mode:(1,6)

wdt reset

The crash only happens, when my wind sensor is spinning and the interrupt function is being called (void countwind()) . The faster the wind is (the more often often countwind() is called)) the sooner the crash happens (within 10-30 seconds).

Is it due to an overflow?

The Sketch:

int counterwind = 0;
const byte interruptPin = 13;
unsigned long  next_timestamp = 0;

float windgeschwindigkeitkmh = 0; 
volatile unsigned long last_micros;


void setup() {
   Serial.begin(115200);
   pinMode(13, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), countfunktion, RISING);
 
}

void loop() {
  
  delay(100);
if(millis() > next_timestamp + 5000){
  windfunktion();
  next_timestamp = millis();
  }
}

void countfunktion() {
  if((long)(micros() - last_micros) >= 5 * 1000) {
    counterwind++;
    last_micros = micros();
  }   
}

void windfunktion(){
  detachInterrupt(interruptPin);
  float rps = 0;
  if(counterwind != 0){
     rps = counterwind/20;
  }
  else{
    rps = 1;
  }
   
  
   windgeschwindigkeitkmh = 1.761 / (1 + rps) + 3.013 * rps;
  
    Serial.println(counterwind);
    Serial.print("Windgeschwindigkeit in km/h: ");
    Serial.println(windgeschwindigkeitkmh);

     counterwind = 0;
     yield();
     attachInterrupt(digitalPinToInterrupt(interruptPin), countfunktion, RISING);
  }

jktz90: wdt reset

wdt reset happens when some part of your code prevents the loop function from returning within the watchdog timer reset duration.

The only reason I can see that happening with this code is if the interrupt is just firing over and over and starving the main loop, which would match your findings. As a workaround, try adding a yield() statement to countfunktion().

if((long)(micros() - last_micros) >= 5 * 1000) What is the operator precedence here? Perhaps some more parentheses? And why the cast?

https://github.com/me-no-dev/EspExceptionDecoder

jktz90: if(millis() > next_timestamp + 5000){

Never add two time values, only subtract. It won't work if next_timestamp + 5000 overflows. If you move the 5000 to the LHS of the comparison, that won't be an issue.

Pieter

DKWatson: if((long)(micros() - last_micros) >= 5 * 1000) What is the operator precedence here? Perhaps some more parentheses? And why the cast?

In case the functions gets called too often in a short period of time

PieterP: https://github.com/me-no-dev/EspExceptionDecoder Never add two time values, only subtract. It won't work if next_timestamp + 5000 overflows. If you move the 5000 to the LHS of the comparison, that won't be an issue.

Pieter

Ill try that out, thanks

Is there any deliberate purpose of calling yield()?

void loop() {

  delay(100);
if(millis() > next_timestamp + 5000){
  windfunktion();
  next_timestamp = millis();
  }
}

Are you being billed for the number of times loop() gets called? That delay() is just plain stupid. Get rid of it.

Why are you calling yield() in windfunktion()? The function ends immediately after that, and loop() gets called again. yield() is accomplishing nothing.