Arduino + ESP8266 Mini Server with Command Que

The Problem

Up to this point, my mini web server running on my Arduino + ESP8266 is ok if things move very slow.  Once I start hitting it with several page requests and aborts, the system just crashes.  After looking through the serial logs, it became clear that I was missing a lot of incoming characters.  This was because there were several places in my old code that was blocking incoming characters while sitting and waiting for the “OK” response.  While I was waiting for the “OK”, some other client would request something and I would totally miss the request.

The (Attempted) Solution:

So I spent a lot of time (…more than I should have….) entirely re-writing my serial port handling code.  The major re-write was to add a command que that would then write the command to the serial port after some delay (points A & B under step 4 below).  The high level concept is:

  1. For each line received from the ESP8266, look for a key word (ie: “GET / “) that I need to answer.
  2. Formulate an answer string when that key word is found on an incoming line.
  3. Stick the string in a fifo style command que to be sent later.
  4. When there are no characters coming in, send the oldest command in the que out (fifo style).
    1. I had to add a time stamp check to add a delay between 2 consecutive commands being sent.  I had to do this because the ESP8266 sometimes would respond with some sort of “busy” response if I tried to send 2 commands too quickly together.
    2. I also added a time stamp check to add a delay between when I received a request to when I sent out the next response.  I just wait a while and assume that my command was successful and move on.  I am not making a commercial grade server and spent too much time here and want to move on.
  5. If a character is received from the ESP8266, go to step 1.  If no characters came from the ESP8266, keep sending commands in the que until all gone.

Best I can do in words.  You can just read the attached code below for more details.

Disclaimer: There are likely many server experts out there cringing at what I am doing.  I am no expert here and really just wanted to play around and got carried away with trying to actually serve up a web page.  Should have stopped long ago and made a custom daemon and custom client to do what I wanted.  This ended up being a long distraction… although it was fun to learn more about how servers work.

Extra Fix

HTTP header: A very helpful visitor commented in my earlier post “Mini Server on Arduino + ESP8266” that I was not sending the HTTP header.  I found very nice and simple tutorial here:  I added a VERY simple HTTP header to my response for HTML page and don’t have to send the connection close command any more.  The client automatically closes the port now.  It took some tricky coding but the clients seem much happier.

The Code

Tired of copy pasting the entire code.  If you want it, go here:

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


One thought on “Arduino + ESP8266 Mini Server with Command Que

  1. Pingback: Arduino + ESP8266 remote control LED using an Android App with Camera Live View | 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 )

Google+ photo

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


Connecting to %s