Search code examples
arduinoesp8266

ESP8266 + aRest running out of memory and rebooting


I am using aREST and ESP8266 and I just wanted to create a personalized method which holds client's request until an action be given. However, a simple while(true) reboots the board immediately.

Any clue why such block inside a personalized function is rebooting my ESP8622? Any other approach do get it done?

int receiveCommand(String command){

  int timeStart = millis();

  while(true){

    //My code goes here

    //Waits for action during TIMEOUT seconds then breaks this loop and
    //Returns something suitable to the client 

    if((millis() - timeStart) > TIMEOUT){
        return 0;         
    }
  } 
  return 1;
}

Solution

  • The esp is a single threaded microcontroller. It mean that it can only do one thing at a time. And it has to run your program along with all the wifi/ip stack.

    Here your while loop is basicly spinning all the time, not giving control back for the microcontroller to perform other operations (like wifi handling for example).

    The esp is designed to reboot should the main program become unresponsible. That basicly what you are creating: the esp isn't responsible anymore because it's looping endlessy.

    The esp framework has an event approach to perform operation without blocking the normal operation. You should take a look at that and adapt your program to avoid blocking the esp in your code.