IBM Bluemix
Using Node-RED to build the internet of things
Ever had one of those days Where the Application works! And then Can we also get some data from the this whatchamacallit? And send the logs off to this other server And add some additional REST endpoints
The Problem We need tools that make it easier for developers at all levels to bring together the different streams of events, both Physical and Digital that make up the Internet of Things
Why Node-RED? The internet does not have a one-size-fits-all solution Every new thing has a new API that must be understood Solutions often require pulling together several different device API s and online services in new and interesting ways Time spent pondering how to access a serial port, or complete an OAuth flow to Twitter is not time spent on creating the real time of a solution
Node-RED is An application composition tool experience A lightweight proof of concept runtime Easy to use for simple tasks Simple to extend to add new capabilities and types of integration Capable of creating the back-end glue between social applications A great way to try can I just get this data from here to there? and maybe change it just slightly along the way
Node-RED is not A fully-scalable, high-performance, enterprise-capable application runtime A dashboard with widgets A mobile application builder The answer to life, the universe, and everything
On the other hand Node-RED is deployed in a manufacturing production line Required extra sensors --quickly--for a proof of concept Built on Raspberry Pi s + Arduinos + Sensors Sensor readings and initial processing coordinated via Node-RED Able to adapt and change quickly redeploying during support phone calls!
Architecture of Node-RED Node.js v8-engine driven; so it s fast Event-driven, asynchronous io; it s all about the events Single-threaded eventqueue; built for fairness Javascript front and back; only one language runtime to deal with Built using express, d3, jquery and ws
Basic Node types Inject node Allows manual triggering of flows Can inject events at scheduled intervals Debug node Show message content; either payload or entire object Template Node Modifies the output based on a Mustache Template
Node-RED Hello World When you click on the Inject Node, it sends and event through the flow triggering the template node and sending the result to the Debug node
Other Input Nodes HTTP Act as an HTTP endpoint; great for building RESTful services IBMIOT Receive messages from an attached IOT Foundation account Also can receive from Websockets, MQTT (pick your own broker), TCP and MQ Light
Other Output Nodes HTTP Response; required as the final node when the input comes from an HTTP Request IBMIOT send events out to the attached IOT Foundation account Twilio send SMS messages via the Twilio service IBM Push Send Push notifications to mobile devices Also can send requests through TCP, UDP, MQLight, WebSockets.
Function Node Types Function node Run user-defined node.js code on the messages going by Uses vm.createscript under the covers to sandbox execution Console, util, Buffer included for convenience Switch Change flow to different options based on a comparison
Creating your own nodes http://nodered.org/docs/creating-nodes/first-node.html Easy to wrap any npm module into a palette node Each node is defined in a pair of files.js: server-side behavior.html: appearance in editor and help Can be shared and installed via npm npm install node-red-node-xmpp
Online flow library Contributors add flows through Github
Node-RED on your device Works anywhere you have node.js and npm (Raspberry Pi, Galileo, etc.)
Node-RED on Bluemix
Temperature Sensor Example Use DHT11 Temperature Sensor and Arduino Yun to send events to Node-RED Send commands to the Arduino Yun from Node-RED to change AdaFruit NeoPixel Use Nick O Leary s MQTT library for Arduino to connect to IOT Foundation For the example
IBM Internet of Things Foundation Real Time MQTT REST Simple APIs Build Manage Assemble Collect Connect
Setting up Node-RED in Bluemix Start with the Internet of Things Boilerplate in Bluemix (available from the Bluemix Catalog) Installs a node.js runtime with Node-RED and a Cloudant NoSQL Database for configuration
Connecting Node-RED to the Internet of Things Foundation To gain access to device data coming from the IBM IOT Foundation, you need to connect your Node-RED runtime to an IBM IOT Service register for the IOT Foundation Trial from within Bluemix
Registering your device You will need to: Register your device s MAC Address (obtaining an auth token to place on the device) Add yourself to the organization Obtain an API Key to register Node-RED
Binding a database Bind a MongoDB service to your application to store your temperature data Will need to note username, password, host and port to configure your Node-RED flow
Storing IOT Data IOT App In connects to the IOT foundation Check for Ardunio is a simple switch based on a property of the payload Get and append time is a function node that gets the current time and adds it to the payload MongoStore takes the final payload and places it in the MongoDB
Building a RESTful service The Data Web Service GET is an HTTP input endpoint responding to a GET on /data Update Payload is a function node that changes the format of the payload to the MongoDB query format (avoiding Mongo injection attacks) MongoDB Query simply returns the result of evaluating what was in the payload (in Mongo DB query form, e.g. "payload.d.time": {$gt: x, $lt: y}) DataOutput is an HTTP Response node returning the payload as its result to the caller
Making actions happen sending events The three inject nodes simply add a predetermined payload into the IBM IOT Output node Two approaches; different commands in the topic name, or different commands in the payload (using payload more common)
Sources for Hardware and Examples Arduino Yun available from Amazon or Radio Shack DHT11 and NeoPixel available from Adafruit (www.adafruit.com) Nick O Leary s MQTT Library for Arduino - http://knolleary.net/arduino-client-for-mqtt/ IBM IOT Foundation - https://internetofthings.ibmcloud.com/#/ IBM Bluemix http://www.bluemix.net JazzHub link to download examples - https://hub.jazz.net/git/brownkyl/arduinoyun Original four-part DeveloperWorks series - http://www.ibm.com/developerworks/cloud/library/cl-bluemixarduino-iot1/index.html