Tuesday, June 10, 2014

IoT made easy

Wiring the Internet of Things has never been so easy. Here is a simple example how the classical chicken or the egg problem is solved.

Node-RED is a browser-based flow editor for graphical wiring together of hardware devices, APIs and online services in new and interesting ways. Node-RED is an open source project initiated by IBM. It can run in local hardware or in a cloud service. In this example I'm using Raspberry Pi as local the hardware and Bluemix as the cloud.

Adafruit has kindly provided detailed and easy to follow instructions how to install Node-RED in Raspberry Pi. There is also some more explanation what Node-RED is in general. I followed the instructions and quickly had my Node-RED up and running in the embedded target.

Program running in Raspberry Pi
Program running in RPi receives messages via serial port 'Coop'. As at the time of the development there is no external hardware in place, there is debug button 'TestEgg' for test purposes. The program does some tweaking to content of the message 'Egg', and then transmits it to Twitter 'Tweet' and to external 'MQTT' broker dev.mosquitto.org for "twitter of things".

Tweets send by RPi when the test button is pressed.
Bluemix provides ready made application templates for different purposes to speed up development. IoT is one of the boilerplates containing Node-RED, IoT and time series database readily installed.
Bluemix templates
Node-RED program running in the cloud receives subscribed 'MQTT' messages from the broker, and stores messages into a database 'StoreEgg'.  There is http-server 'HttpRequest' waiting for requests, and once such occurs, the content of the DB 'FetchEggs' is dumped to the browser 'HttpResponse'.

Node-RED program running in Bluemix.
As this exercise is not about how to make cool HTML5 web pages, the simple database dump is good enough for proof of concept here.

Database dump in browser.
Simple as that! By adding few boxes, wiring them together, and configuring them, I have now in place IoT connectivity, cloud integration, social media engagement, database access and web user interface. There is only a few lines of node.js code implemented in this exercise, in order to add time stamp to messages in the function 'Egg'.

var d= new Date();
msg.payload = "Chicken called "+msg.payload+" laid an egg at "+d.toString();
return msg;
var d= new Date();
msg.payload = "Chicken called "+msg.payload+" laid an egg at "+d.toString();
return msg;
var d = new Date();
msg.payload = "Chicken called "+msg.payload+" laid an egg at "+d.toString();
return msg;

That's all folks!

No comments:

Post a Comment