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:
- For each line received from the ESP8266, look for a key word (ie: “GET / “) that I need to answer.
- Formulate an answer string when that key word is found on an incoming line.
- Stick the string in a fifo style command que to be sent later.
- When there are no characters coming in, send the oldest command in the que out (fifo style).
- 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.
- 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.
- 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.
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: http://www.jmarshall.com/easy/http/. 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.
Tired of copy pasting the entire code. If you want it, go 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.