Tuesday, July 13, 2010

More Messaging

Below are some jottings and thoughts I wrote down on 13th July. Just about 5 weeks later (starting new job etc) - most of these ideas are coming to fruition. The idea of communicating between ethernet enabled microcontrollers using Pachube as a message broker is currently being pursued.

Getting small microcontroller systems to talk to the web is an area of increasing interest. With many homeowners now possessing "always-on" broadband routers, the task of getting home automation, sensing and security systems online has never been easier or cheaper.

In this interesting talk/slide show, "The House That Twitters", Andy Stanford-Clark, describes how he has arranged devices around his house to publish data using MQTT to a Really Small Message Broker (RSMB) and then make that data to other subscribing devices. As Andy invented and subsequently developed MQTT, it is fitting that he choses to use his own house as an example of what can be done. He also explained that once he had established the basic system, then new devices and applications could be added relatively easily without having to start from scratch each time.

My aspirations are somewhat different, and perhaps much cruder. My aim is to get stand-alone Arduinos to exchange data with each other using Pachube as the message broker.

With a little help from others, I now have analogue readings from my Arduino appearing as a Pachube feed. The next task is to retrieve the data from that feed using another Arduino and make use of it, for example display it on a local LCD, or have the second Arduino parse through the data, and make control decisions based on the numeric values of the data. Here Pachube would be acting solely as a message broker - receiving data from the first Arduino which is the publisher, and making it available to any other Arduino which wishes to subscribe to it. It is this Publisher/Subscriber relationship (PubSub) which is key to any message brokerage system.

For this to work, the subscribing Arduino(s) must have a means of accessing the raw csv data on Pachube, using the GET method. I'm still reading up on this and am yet to find an example which explains it in simple terms. I'm hoping to reach the point where the whole PubSub functionality is handled in underlying code, and all the Arduino user needs to do is read from or write to a suitably sized buffer.

Messaging between Arduinos, or other small micros, should be made as simple as texting your mates.

All you need is a feed number on Pachube to send the data to, and the subscribing Arduinos regularly look for updates. The subscribers might just display the data on an LCD for example, or use the various fields for specific control purposes. One field might be a bit pattern that you want to send to an output port and control a bunch of relays, another field might be the unique address of a specific Arduino that you want to send a command to. If the message length was kept to 128 bytes then this should be small enough to allow easy manipulation on the Arduino, be compatible with Twitter, and provide sufficient scope for several fields to reflect analogue values, bit maps for port setting and various other control structures.

Hardware Thoughts.

Hardware needs to be cheap and cheerful with much of the elegance of the application provided in the firmware.

What is needed in terms of hardware to give a microcontroller access to web connectivity? What are the opportunities for a low cost, open source hardware solution?

First what is needed is a network interface controller - a NIC chip. Microchip offer the low cost ENC28J60 which is available for about £2. With a RJ45 jack, with integrated magnetics - a mag-jack, a 25MHz crystal and a few other components you can put together a simple 10Mbit ethernet controller.

Alternatively, if you are working with the popular Arduino microcontroller, then you can buy a ready built and tested ethernet shield which uses the ENC28J60, for as little as £12.50. This little board comes with example sketches, and others have developed and improved upon these to make connection to Twitter and Pachube relatively straight forward.

Now one of the problems of ethernet and web access is the hosting of the TCP/IP stack, which tends to put a large burden on the resource limited microcontroller, in terms of RAM and program code size. This leaves little available for the application - which may be a problem. Indeed the ethernet shield with the ENC28J60 is limited in application when compared to the Wiznet W5100 based "Official" Ethernet Shield- but that costs twice the price!

However with the availability of the 32K ATmega328 microcontroller, now fitted to most new Arduinos, the problem is somewhat reduced.

Other options include a combination of ATmega328 and ENC28J60 on one simple pcb which have recently appeared - such as this one from Tuxgraphics who also provide a handy tutorial on their cut down TCP/IP stack specifically for AT mega microcontrollers. Tuxgraphics have been developing AVR/ENC28J60 applications for several years, and are a useful source of information - should you wish to follow this particular hardware combination.

No comments:

Post a Comment