Wednesday, July 13, 2011

Web server for Arduino

For my magnetometer project I've been using the Arduino micro-controller system to measure and log the magnetic field strength. For real-time data the Arduino sends a UDP packet to a remote server, using the ethernet shield. The shield also contains a microSD slot, allowing data to be logged in case of network outage. What I really want to do is fetch the archived data from the microSD card without disrupting data logging. I could use TFTP but HTTP clients are much more widespread. Using HTTP also gives the potential for a friendly interface to control the magnetometer, not just fetch the data.

There is already the webduino library but it doesn't serve files from the SD card, and it processes the HTTP request as one task. For my purpose I need the HTTP request to be split into many tasks so that web clients cannot disrupt data logging. For this reason I've created my own WwwServer library, which can be found on Github: https://github.com/stevemarple/WwwServer. At a convenient place in loop() make a call to processRequest() so that any pending or ongoing requests can be processed. You must pass a buffer for the web server code to use, but you are free to use the buffer for other purposes before or after the call to processRequest().

If you plan on using the code you'll need a beta of the upcoming 1.0 version of the Arduino IDE (the new-extension branch on Github). This is a big improvement over the 0022 release, DHCP is built into the Ethernet class and the SD library supports multiple open files. The beta release from January 2011 has a bug which breaks DHCP offers containing more than one DNS server. I've made some binaries available of my version at https://github.com/stevemarple/Arduino; it includes a fix for the DHCP bug.

How well does it work? Serving a 40KB file from the SD card takes several seconds, so its not very fast. The important thing is that worst case call to processRequest() is completed in under 30ms. That's over 10× faster than the blink of an eye, and fast enough that I only need add a small amount of 'guard time' to ensure that web requests don't affect data logging.

No comments:

Post a Comment