Sunday, August 22, 2010

Master and Servant

Not exactly a cheesy homage to Depeche Mode's 1984 electropop single but more of a concept for connecting together distributed physical computing devices - honest!

Now that I have got the low cost Arduino compatible ethernet board running as described in the last post, it's time to get on with the main project and show that this cheap hardware can be used for home automation, energy monitoring and remote control applications.

The aim is to build up a network of interconnected "Arduinoids"* and have them controllable via the internet. A couple of weeks back I wrote my serial command interpreter, so that a laptop can send commands to an Arduino over it's serial interface, now it's time to extend this so that any device can command the Arduino - remotely via the internet.

If we think in terms of the Arduino as being a programmable element or smart node, designed to monitor sensors, switches or perform some processing function such as control a LCD, then by adding these nodes to a communications network we can distribute them around the environment to provide local monitoring and control, all controlled by a master which is connected to the internet.

The command string we will use is nothing more than comma separated variables (CSV). The Arduino receives this in its serial buffer and decodes it into a command plus one or more arguments. The proposed format of the command is as follows:

Command Letter, sub-address, arg1,arg2,arg3...

The command letter is chosen to be memorable to the human operator, for example B to represent the command that sets the brightness of a multi-colour LED array for example.

The sub-address is a way of directing the command to a specific Arduino board connected to the network.

Arg1,Arg2 and Arg3 are numerical arguments - which in this example might refer to the value of the 8-bit PWM signal sent to each of the red, green and blue LEDs in the multicolour array - thus giving a 24-bit control over the colour.

The plan is therefore to use Pachube as a simple message broker or mailbox server. A device wanting to send a control command to an other Arduino will PUT, or publish a short CSV string up to a Pachube feed. Arduinos needing to be controlled will regularly subscribe to this feed, using the GET method of the Pachube API and receive the command string in CSV format.

By decoding the command and the sub-address, the master device can then output the command string onto the serial bus such that the slave whose sub-address is given, can execute the command.

Typing serial commands into a web page application text box would create the corresponding CSV command string, and PUT it up to the Pachube feed. When the web-connected master subscribes to this feed the command string should ultimately control the remote slave hardware, with the master acting as a transparent serial to ethernet gateway. A simple serial command format would allow any serial device to be connected through the master and used for monitoring and control.

If each "slave" unit costs about a tenner, then is perfectly reasonable to have several in the household environment, each dedicated to specific tasks and comunicating back to the master unit. For example one could monitor domestic electricity and gas usage, another monitors individual room temperatures, outside temperture and controls the central heating, another controls the solar water heating - etc, etc.

Whilst low cost wireless is prolific amongst gadgets for data communications, it still does not solve the age-old problem of how do you power a remote device. So for this reason I am going to suggest a backbone network of wired devices, using wireless only where cables are not practical. A wireless shield could always be added - as the board will accept standart Arduino shields Borrowing from telephone extension cable techniques, a 4 wire telephone extension lead can provide power, ground and serial communication lines very cheaply for distances up to 50m. 0.5mm solid core telephone cable will carry about an amp - so plenty of current available for running a few sensor slaves.

The slaves will in fact be "Smart Sensor Nodes", consisting of a microcontroller and associated sensors etc. Using Arduino compatible hardware allows, for example a remote temperature sensing board to monitor between 6 and 8 thermistors and up to 11 digital inputs or outputs, which could be used for driving pump control relays, LCD displays or whatever. The slave hardware can easily be extended - using a 74HCT595 serial to parallel shift register will give another 8 output lines - controlled from the SPI bus. Adding a 4051 analogue switch will give an additional 8 analogue input channels. If many more I/O lines are needed then the Arduino Mega could be used - these are now very cost effective.

Using a common serial command format as discussed in an earlier post, means that we are not tied to Arduino hardware, but any microcontroller that can implement a UART in software or hardware. The advantage of remaining Arduino compatible, however, is that remote slaves could ultimately be programmed over the serial bus, so that updates can be made from a central location. By issuing a "reset" command to a specific slave, you could put it into bootloader mode and send a program update to it.

So the idea evolved of a number of serial slaves all connected to a serial bus. Various "multi-drop" addressable bus schemes are available such as RS485, but it was the two spare 74AHC125 tristate buffers left over on the ethernet board which gave me the idea of using them as tristate-able linedrivers - so that a slave could be polled by the master, and given access to the bus. In this way the slaves transmit pin would remain tristate, allowing the bus to be used by others, until allowed access.


* Arduinoid - something derived from the Arduino, augmented in some way with additional functionality - such as built in ethernet controller etc.

1 comment:

Anonymous said...

Very interesting stuff.

On the topic of using a common standard to communicate information from distributed, web-enabled sensors, the following research paper might be of interest:

Linked Data for a privacy-aware Smart Grid - PDF - 16 Aug

(by the way - do you remember I mumbled something about changing career a few months ago? Well it's now official: I'll be starting a Computer Science MSc at Imperial in October. My intention is to do geeky green things ;)