Auto ESP8266 Server Init

The Plan

Now that I have a pretty stable HW setup, I want to start automating things and automate controls over Wifi.  The first step is to be able to auto init the ESP8266 whenever my system is power up or reset.


The ESP8266 is able to store the last Wifi network configuration and rejoins the last Wifi access point that it was on each time it power up or is reset.  I am going to assume that the ESP8266 is set up to attach to my Wifi router for now.  Will come back and implement a way to set it up if moving to a new area later.

So assuming the ESP8266 is already on the Wifi network, I need to give the following commands….

AT+RST                // Reset the module
AT+GMR                // Just a sanity check that the ESP8266 is responding
AT+CIPMUX=1           // Set up for multi connections
AT+CIPSERVER=1,8888   // Start up the server on port 8888

For each of these commands, I want to wait for the correct response from the ESP8266.  If I don’t get the correct response to one of the commands, I should halt and somehow indicate there is a failure.  Also would be nice to indicate as each step is successful.  I could use debug prints to the serial port on the PC, but eventually I want to have the system be a stand alone module so won’t have a PC connected to watch the serial port.

To add some sort of indication, I am going to use 2 LEDs and blinking codes to indicate what is going on.  One red LED and one green LED.  Here is what the circuit looks like…


BTW – If you have seen the previous post, you will noticed I add a few extra features to make my life easier… LED indicator for when the 3.3V regulator is on and a switch to turn on the power to the 3.3V regulator (so I don’t have to keep unplugging the 9V battery),

The blinking code that I want to implement is…

  • Each time a command is going to be sent, the red LED will blink
  • Each time a command is successful, the green LED will blink
  • If there is a failure, both green and red LED’s will be steady on.
  • If the entire setup is good, the red and green LED will blink alternately repeatedly.
  • To allow me to know what step the code is on, the LED’s will blink the number of times equivalent to the step it is on…. 1 blink for AT+RST, 2 blinks for AT+GMR, etc….

SW Implementation

I struggled for a while to write my own AT command parser, but found a really nice one that was shared by TM here:  It is ingenious!! I heavily borrowed his source code and did a little tinkering to fit my needs.  Added a blinking LED functions as I described above. Here is what my source code looks like…

Try to start the server autonomously
#include <SoftwareSerial.h>
#include <string.h>

#define TIMEOUT 5000 // mS
#define GREENLED 4
#define REDLED 5

SoftwareSerial mySerial(7, 6); // RX, TX

void setup() 

 int CommandStep = 1;
 //This initializes the Wifi Module as a server 
 SendCommand("AT+RST", "Ready", true);
 SendCommand("AT+GMR", "OK", true);


void loop() // run over and over


void BlinkLED(int LEDPin, int NumberOfBlinks, int OnDuration)
 for (int x=1; x <= NumberOfBlinks ; x ++){

boolean SendCommand(String cmd, String ack, boolean halt_on_fail)
 mySerial.println(cmd); // Send "AT+" command to module

 // Otherwise wait for ack.
 if (!echoFind(ack)) // timed out waiting for ack string
 if (halt_on_fail)
 errorHalt(cmd+" failed");// Critical failure halt.
 return false; // Let the caller handle it.
 return true; // ack blank or ack found

// Read characters from WiFi module and echo to serial until keyword occurs or timeout.
boolean echoFind(String keyword)
 byte current_char = 0;
 byte keyword_length = keyword.length();
 // Fail if the target string has not been sent by deadline.
 long deadline = millis() + TIMEOUT;
 while(millis() < deadline)
 if (mySerial.available())
 char ch =;
 if (ch == keyword[current_char])
 if (++current_char == keyword_length)
 return true;
 return false; // Timed out

// Print error message and loop stop.
void errorHalt(String msg)


Not much to say other than it worked and I could see the blinking lights.  I also purposely put in a string to look for that would never occur and saw both lights turn on.  Instead of keeping to describe in words, here is a video.


2 thoughts on “Auto ESP8266 Server Init

  1. Pingback: LED’s Control through TTY | PetesTechProjects

  2. Pingback: Mini Server on Arduino + ESP8266 | PetesTechProjects

Leave a Reply

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

You are commenting using your 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