Internet of Things – Use Sensor Data In BlueMix

Today I will talk about how you can use sensor data published to BlueMix. I will create an app that will monitor the temperature and light sensors in my room, and during the day (when it’s not dark) it will let me know whether it’s too hot or too cold, or if the temperature is fine.

This video build on my previous video called “Internet of Things – Publish To BlueMix”, so please check that out for details on how to publish your sensor data to BlueMix. In this video, I will create an application in BlueMix that act in different ways depending on the sensor data. The application will be using the Node-RED runtime (for more details on Node-RED, please see http://nodered.org).

To create Node-RED application in BlueMix, please see https://developer.ibm.com/iot/recipes/node-red-quickstart-application, and instead of using the JSON on that page, use the below code instead (make sure to replace the twelve zeros with the MAC address of the Arduino network card)…

[{"id":"a01bd970.5fe428","type":"iot-app-in","name":"IoT App In","topic":"iot-2/type/sensors/id/000000000000/evt/status/fmt/json","x":275.5,"y":366,"z":"33e23879.cc1dc8","wires":[["9e53b0c6.61ac5"]]},{"id":"9e53b0c6.61ac5","type":"switch","name":"check light","property":"payload.d.light","rules":[{"t":"gte","v":300,"v2":null},{"t":"else"}],"checkall":"false","outputs":2,"x":460,"y":371,"z":"33e23879.cc1dc8","wires":[["c3c5f155.3c3a1"],["e208524c.1df7b"]]},{"id":"c3c5f155.3c3a1","type":"switch","name":"check temp","property":"payload.d.temp","rules":[{"t":"gte","v":"28"},{"t":"lte","v":"22"},{"t":"else"}],"checkall":"false","outputs":3,"x":633,"y":333,"z":"33e23879.cc1dc8","wires":[["53ab2e4a.ac54d"],["cfe52e9b.301ad"],["ad930b65.526cf8"]]},{"id":"53ab2e4a.ac54d","type":"template","name":"too hot","template":"Too hot! ({{payload.d.temp}})","x":804,"y":280,"z":"33e23879.cc1dc8","wires":[["5ed0b558.a12f4c"]]},{"id":"cfe52e9b.301ad","type":"template","name":"too cold","template":"Too cold! ({{payload.d.temp}})","x":802,"y":339,"z":"33e23879.cc1dc8","wires":[["5ed0b558.a12f4c"]]},{"id":"ad930b65.526cf8","type":"template","name":"fine","template":"Fine! ({{payload.d.temp}})","x":803,"y":390,"z":"33e23879.cc1dc8","wires":[["5ed0b558.a12f4c"]]},{"id":"e208524c.1df7b","type":"template","name":"night","template":"Night! ({{payload.d.light}})","x":801,"y":443,"z":"33e23879.cc1dc8","wires":[["5ed0b558.a12f4c"]]},{"id":"5ed0b558.a12f4c","type":"debug","name":"status msg","active":true,"console":"false","complete":"false","x":997,"y":340,"z":"33e23879.cc1dc8","wires":[]}]

…and you should see the above flow in the Node-RED flow editor. It starts to the left with a node that picks up the topic from your published sensors, and passes it on to a switch that determines whether it’s day or night. If it’s night (the sensor value is less than 300), a night message is shown in the debug tab. If it is day, another switch checks if the temperature is too hot (28 degrees Celcius or higher), too cold (22 degrees or lower), or if it’s fine. Each check results in a corresponding message that is shown in the debug tab.

When you deploy the application (by selecting the Deploy button on the top right), you should start seeing messages appear in the debug tab on the right in the flow editor. In this simple example, I’m just showing messages, but it’s fairly easy to take some more relevant action. For example, during the day, the hot and cold messages could be replaced with an HTTP request to send an email or SMS message.

That is how you can use sensor data published to BlueMix to take action through a cloud application.