ESP8266 keeps resetting (most likely due to interrupt routine)

Hello everyone,

I’m trying to put a simple cycle time sensor together which will be used in conjuction with a cnc machine (thats the plan).
The code worked for a while until I tried to incorporate a circular buffer for a running average. I then went back to the previously working version, but I get constant reboots with the following exception:

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

ctx: cont
sp: 3ffefd90 end: 3ffeff80 offset: 01a0

stack>>>
3ffeff30: 00000000 3ffeef54 3ffeef4c 40203c04
3ffeff40: 402010d2 000003e8 000003e8 40203dd8
3ffeff50: 3fffdad0 00000000 3ffeef4c 402030c5
3ffeff60: 3fffdad0 00000000 3ffeef4c 402045a0
3ffeff70: feefeffe feefeffe 3ffeef60 40100718
<<<stack<<<

I’m pretty new to the coding practices of Blynk, so I might be doing something very stupid, but I think I need to periodically send the “sensor” data (every second or every few seconds). That is why I need the interrupt, I need to measure the time when a signal is detected (this signal will range from 0 to 4000 pulses per hour max which should be slow enough for the code to run in the interrupt routine).

ESP8266 (Wemos D1 mini)
Android version 2.17.2
Blynk server
v0.5.0

  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************
  This example runs directly on ESP8266 chip.

  Note: This requires ESP8266 support package:
    https://github.com/esp8266/Arduino

  Please be sure to select the right ESP8266 module
  in the Tools -> Board menu!

  Change WiFi ssid, pass, and Blynk auth token to run :)
  Feel free to apply it to any other example. It's simple!
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxx";

char ssid[] = "xxxx";
char pass[] = "xxxx";

#define cyclePin  15
volatile int elapsedTime = 0;
volatile int previousTime = 0;
unsigned long averageCycle = 0;

BlynkTimer timer;

void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  
  pinMode(LED_BUILTIN,OUTPUT);
  digitalWrite(LED_BUILTIN,HIGH);
  
  pinMode(cyclePin, INPUT);
  attachInterrupt(digitalPinToInterrupt(15), cycle_ISR, RISING);
  
  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
  delay(1000);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

void myTimerEvent()
{
  Blynk.virtualWrite(V0, 3600000/elapsedTime);
  digitalWrite(LED_BUILTIN,LOW);
  delay(10);
  digitalWrite(LED_BUILTIN,HIGH);
}

void cycle_ISR()
{
  unsigned long currentTime = millis();
  elapsedTime = currentTime - previousTime;
  previousTime = currentTime;
}

for me the code seems fine, it shouldn’t cause resets.

what does it mean? every second, every minute, on random intervals, etc?

maybe, just for piece of mind, if the interrupt won’t trigger for a long time, the elapsedtime and previoustime should be declared long (this can’t affect the reset cause of course).

  • maybe the interrupt signal triggers more often than you think, and this can cause system hang

  • try to use a different interrupt pin, and see if the problem still happens

  • it would be interesting to know what is hooked up to the esp on what pins

  • you should pour the cryptic reset message into mighty google, and see if you can find out more about that message. example, the cause of the reset

  • please set the serial baud to the “standard” 115200 rate, and see if there’s any useful debug message. or try to find out if the reset is somehow related to some actions done by blynk

I found the problem…

As usually it is something very simple, I commented different sections of code and I assume that the BlynkTimer starts with an execution and crashed on a division by zero.

void myTimerEvent()
{
  if (elapsedTime>0){
    Blynk.virtualWrite(V0, 3600000/elapsedTime);
    digitalWrite(LED_BUILTIN,LOW);
    delay(10);
    digitalWrite(LED_BUILTIN,HIGH);
  }
}

Checking if elapsedTime is greater then zero fixed it.

@jbowser do you know how to use the stack error decoder? Not easy even for experienced coders.

ISR’s can be problematic on ESP’s due to the WiFi stack.

You have a division by zero error.

Mod your code to the extract below and you will be good to go:

  if(elapsedTime > 0){
    Blynk.virtualWrite(V0, 3600000UL/elapsedTime);
  }

do you mind sharing a link to a good tutorial? i’m interested too…

Hardly a tutorial but the GitHub is at https://github.com/me-no-dev/EspExceptionDecoder

1 Like

see also:
http://arduino-esp8266.readthedocs.io/en/latest/Troubleshooting/stack_dump.html

2 Likes