3

I'm trying to upload this code on ESP8266, and when I do, it just resets continuously. This also happens when you have memory bugs, but obviously, this code has no memory bugs! Here is the code:

#include "Arduino.h"

void setup()
{
    Serial.begin(9600);
    foo();
}

void loop()
{

}

void foo(){
    for(unsigned int i = 1; i <= 10000; i++){
        Serial.println("FOOBAR");
    }
}

Also, in the serial monitor, I get this (per reset):

Soft WDT reset 

ctx: cont  
sp: 3ffef130 end: 3ffef390 offset: 01b0 

>>>stack>>> 
3ffef2e0:  feefeffe feefeffe feefeffe 00002580   
3ffef2f0:  0000001c 00000001 3ffe85b1 402023d4   
3ffef300:  feefeffe 00000005 3ffe842d 3ffee364   
3ffef310:  3fffdad0 00000006 3ffee340 40201fd1   
3ffef320:  3ffe85b0 00000000 3ffee340 40201fd1   
3ffef330:  3ffe8428 00000000 3ffee340 40201fec   
3ffef340:  00000001 feefeffe 3ffee340 40202010   
3ffef350:  feefeffe 00000000 00002580 40201c00   
3ffef360:  3fffdad0 00000000 3ffee35c 40201c2e   
3ffef370:  feefeffe feefeffe feefeffe 402021c8   
3ffef380:  feefeffe feefeffe 3ffee370 40100718   
<<<stack<<< 
Õ®(ŽL‰Ì==   I5

So, why is this happening?

5

It's happening because you're not giving the ESP8266 a chance to do its housekeeping activities and the watchdog (which is enabled by default) is timing out - as evidenced by the very first line of your output:

Soft WDT reset

You need to allow the MCU to do other things at the same time as you are printing to serial:

void foo(){
    for(unsigned int i = 1; i <= 10000; i++){
        Serial.println("FOOBAR");
        yield();
    }
}
  • What does yield() does? Curious to know that. – Rajendran Nadar Oct 3 '17 at 19:04
  • 2
    the ESP8266 uses the same cpu for maintaining wifi as running sketch code. if your sketch code runs too long, the wifi routine freaks out about not being able to maintain a connection and resets. yield() pauses sketch code while the wifi does its thing. – dandavis Oct 3 '17 at 19:18
  • Is there a way to modify time out? – Muhamed Cicak Oct 4 '17 at 11:27
  • @dandavis Very useful comment +1. – Muhamed Cicak Oct 15 '17 at 11:24
0

With Arduino/ESP8266 you have to break your tasks in little pieces so setup and loop takes as little time as possible.

Your elemental tasks is executing one Serial.println. We first reformulate foo to run a single elemental task in each invocation. Then, we move the call to loop, so setup ends quickly. In loop we use the variable iterations to controls calls to foo. When it reaches zero it signal the end of the "setup" part.

#include "Arduino.h"
int iterations = 10000;
void setup()
{
    Serial.begin(9600);
}
void loop()
{
  if (iterations > 0) {
    iterations--;
    foo();
  }
}
void foo(){
  Serial.println("FOOBAR");
}

It can be tricky to break some big code in small parts, but it's something you must learn to do, because there is no other way.

  • Comments are not for extended discussion; this conversation has been moved to chat. – Majenko Oct 6 '17 at 11:59
  • Majenko, did you see my comment on your answer? Or, rather, the question? – Muhamed Cicak Oct 8 '17 at 0:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.