Arduino + ESP8266 Recovery Mechanisms

The Plan

I have spent a lot of time trying to make my Arduino + ESP8266 mini web server more stable.  I still run into some issues that I can’t avoid or recover from.  To address this, I am going to try to implement some auto-recovery mechanisms.

The Problems

  1. The ESP8266 seems to randomly just reset.  Could be power issues supplied from supplying the ESP8266 from the USB port or my bad web server code.  Not sure, but want to be able to recover from this situation.
  2. ESP8266 serial port gets stuck with “busy s…”.  I am not 100% sure, but it seems to consistently happen if I abort a HTML request from my browser (which closes the connection) right when my server code is trying to send the HTTP header and HTML code back.  After then I get the “busy s…” for anything I try to send to the ESP8266.

The Implementation

Random Reset: My serial port parser looks for the occurrence of “System Ready”.  This is sent by the ESP8266 any time it resets.  If the “System Ready” string is found, send out all the AT commands to start up the ESP8266 as a server.

...
if (InLine.indexOf("System Ready") != -1) {
 Serial.println("The ESP8266 Reset for some reason");
 InitWifiModule();
 }
...

...
void InitWifiModule(){
 int CommandStep = 1;
 BlinkLED(REDLED,CommandStep,50); 
 SendCommand("AT+RST", "Ready", true);
 BlinkLED(GREENLED,CommandStep,50);
 CommandStep++;

 BlinkLED(REDLED,CommandStep,50); 
 SendCommand("AT+GMR", "OK", true);
 BlinkLED(GREENLED,CommandStep,50);
 CommandStep++;

 delay(3000);

 BlinkLED(REDLED,CommandStep,50); 
 SendCommand("AT+CIFSR", "OK", true);
 BlinkLED(GREENLED,CommandStep,50);
 CommandStep++;


 BlinkLED(REDLED,CommandStep,50); 
 SendCommand("AT+CIPMUX=1","OK",true);
 BlinkLED(GREENLED,CommandStep,50);
 CommandStep++;

 BlinkLED(REDLED,CommandStep,50); 
 SendCommand("AT+CIPSERVER=1,80","OK",true);
 BlinkLED(GREENLED,CommandStep,50);

 digitalWrite(GREENLED,HIGH);
}

“busy s…” Lockup: Once I see this, I have never been able to recover the ESP8266.  Even sending the AT+RST command doesn’t help.  The only way I can find around this is to HW Reset the ESP8266.  Luckily there is a HW Reset pin on the ESP8266.  I wired digital IO 22 (randomly picked on my Arduino Mega board) to the ESP8266 through my NTE4050B “one way level shifter”.  Now when my parser sees “busy s…” it will toggle digital IO 22.  My random reset monitor (see above) will see the ESP8266 reset and will send the server init command.

... 
if (InLine.indexOf("busy s...") != -1) {
 Serial.println("dead with busy s... HW Reset");
 QueOut=QueIn; //Clear out the command que
 digitalWrite(ESP_RESET,LOW);
 delay(500);
 digitalWrite(ESP_RESET,HIGH); 
 // Note: Parser should see the reset and start the InitWifiModule routine
}
...

It has been a few posts since I posted a diagram.  Here is the latest with the Uno replaced by the Mega and the new ESP8266 reset signal in place….

Update12_14_14

The Testing

To simulate the random reset, I turned off/on the power to the ESP8266.  My new code resent all AT command to start up the server again and I can refresh my web browsers with no problems.

Testing the “busy s…” was also pretty easy.  I hit the browser refresh… waited 1 second… hit the “x” on the browser to about the request….. wait 1 second… tried to request the page again… The HW reset was toggled and the AT commands started the server.  I can then request the page again from the browser and everything looks ok.

With that, I think I can recover from most problems I have been seeing.

The code

Here is my code with all the additions so far.  Tired of copy pasting the entire code.  If you want it, save to your local drive from here: https://drive.google.com/file/d/0B1a0nPfCQQvKX3d2OUFpSkRtQW8/view?usp=sharing

You can open it in a text editor if you don’t have the Arduino IDE.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s