Monday, 28 March 2016

Controlling Arduino with a web page

Here's a little project I've been working on lately.
It's using a Raspberry Pi as a web server and a wifi access point, as well as talking to an Arduino over serial. It means we can use a web-based interface to change settings and parameters on the Arduino without exposing the user to nasty strings of hexadecimal.

It uses a right old clumsy mix of a LAMP stack on the Pi (well, an Apache webserver and PHP although there are plans to do a little bit of database logging too) and Python.

There's no nice way to get the web server talking to Python (or vice versa) so everything is done through exchanging temporary files on the server.

When a request comes in from the web-based app, our phone (or tablet or whatever) is talking to PHP. The PHP script creates a temporary file on the Pi (we created a small ram-disk of about 5Mb to put these temporary files on, so we're not continually writing to and deleting content on the actual SD card).


Then we have a Python script running as a daemon service.
It constantly looks for temporary files and if it finds one, parses the contents to see what has been requested. The script then takes action - where needed - and, if necessary, writes a response to another temporary file. The PHP script can then read these files and report the results back the web interface.

It's clunky and nasty.
But it works.
And is pretty robust.

So now we don't even need to build "apps" for controlling our connected microcontrollers (of course in the fullness of time, we'll be talking to a PIC over serial - we were asked to use Arduino as proof of concept for someone else to follow). We can just throw a Raspberry Pi onto a UART and use that for all manner of fancy graphical front-ends.,,,,