Web (HTTP) server for Arduino using a state machine

Most of the examples of making an Arduino-based web server have been a bit tedious to use, because you have to manually decode the header lines, the GET and POST parameters, and so on.

Thus I am please to present a library which does all the hard work for you, leaving you to just detect key/value pairs (eg. "light" -> "on")

Tiny web server for Arduino or similar

The library was written specifically to use minimal amounts of memory, thus it uses a state machine to work out what each incoming byte means.

It only stores the current key/value pair in memory, once assembled it calls a user-supplied callback function, which lets you handle that (eg. turn an LED on) and then discards it.

Example sketches show:

  • Handling GET parameters (eg. http://10.0.0.241/**?light5=on&light6=off**)
  • Handling POST parameters - so you can fill in a form with checkboxes
  • Handling cookies - so you can remember what the client last wanted

The library itself is available from GitHub - nickgammon/HTTPserver

Constructive comments and suggestions are welcome.

The library seems reasonably efficient in the use of program memory, RAM and time:

Binary sketch size: 13,662 bytes (of a 32,256 byte maximum)
...
Free memory = 1413 bytes
Time taken = 198 milliseconds.

I like the fact it is detached from the transport system. HTTP might have some usefulness on interfaces that do not implement IP. Or at least allow sensor nodes to each act as HTTP servers while only having the root node internet connected.

Yes, I didn't want to second-guess what sort of transports you might use.

To explain these comments, the library itself does not know or care what sort of Ethernet connection you have. You simply send it a byte at a time and that byte alters the state machine, which might or might not trigger a callback to your user-supplied functions.

For that matter it could be used for anything that works on similar lines (eg. the client wouldn't have to be a real client, it could be another Arduino pretending to be a client).

For that matter it could be used for anything that works on similar lines (eg. the client wouldn't have to be a real client, it could be another Arduino pretending to be a client).

It seems to me that it could be used to read and parse a configuration file from an SD card, as long as the file contains name=value pairs.

True, or in the case of the header part:

Name: Value

Very nice! Something you might consider adding, while you're at it:

When I did a web server for the Due a while back, I added a symbol table. This allows me to embed tokens in the HTML that automatically get replaced with data from variables when the page is served. For mine, I used #SYMBOLNAME# for the tokens embedded in the HTML. Each symbol had a type (boolean, int, char *, etc.), and could be either directly read (when the page is served) or written (when a GET ot POST is processed) either directly, or indirectly through set/get methods setup when the symbol table entry is created. This makes it really easy to have pages that display "live" data that is automatically updated whenever the page is refreshed. It makes for very, very little c/c++ code to create some pretty elaborate web pages. What I did was quick and dirt, but I'd bet you could do a really nice job of it.

Regards,
Ray L.

Thanks for the suggestion. :slight_smile: