Internet of Things – Use Actuators In BlueMix

Today I will talk about how you can use actuators in BlueMix. I will build further on the app that monitor the temperature and light sensors in my room, and this time it will publish MQTT messages on the broker that is picked up by and viewed on an LCD display.

This video build on my previous video called “Internet of Things – Use Sensor Data In BlueMix”, so please check that out for details on how to use sensor data in BlueMix. In this video, I will modify the Node-RED application to send the output messages to another Arduino with an LCD display. To send messages to devices in BlueMix, you have to register the device, and to do that you need to sign up for the Internet of Things Foundation, register your devices, and also set up a real Internet of Things service (it doesn’t work with the quickstart, and for details please see https://developer.ibm.com/iot/recipes/node-red-registered-application).

So, to actually get this demo working as I’m showing it here, you need two Arduinos – one with the sensors and one with a display. The one with the sensors should use the code from my video “Internet of Things – Publish To BlueMix”, but note that you need to update the code to use the registered organization id, client id, and credentials (please see https://developer.ibm.com/iot/recipes/improvise-registered-devices). All the hardware that you need to perform this lab is the same as in my video “Internet of Things – Actuators”, so please check that out for details, and here is the updated code (make sure to replace the twelve nines with the MAC address of the network card, and also with the organization id, and the with the authorization token)…

…and after the initial includes, the MQTT client object is defined and created (on lines 5-6), and the LCD screen object is created with the pins specified (on line 7). In the setup, we define the LCD display size (on line 12), and initialize the bridge (on line 13). Then we connect the MQTT client to the server (on line 14), and set up the subscription (on line 15). The eternal loop simply makes sure that the MQTT object can handle the incoming messages (on line 20). The callback function convert the message payload to a string (on lines 25-26), extract the message (on line 27), and show it on the LCD display (on lines 28-29).

Now, open your Node-RED application in BlueMix (remove any nodes) and import the below code (make sure to replace the twelve zeros with the MAC address of the Arduino with sensors and the twelve nines with the MAC address of the Arduino with a display)…

[{"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","81a405bb.7e5bf8"]]},{"id":"9e53b0c6.61ac5","type":"switch","name":"check light","property":"payload.d.light","rules":[{"t":"gte","v":300,"v2":0},{"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,"v2":null},{"t":"lte","v":23,"v2":null},{"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":"{\"d\":{\"message\":\"Too hot ({{payload.d.temp}})\"}}","x":804,"y":280,"z":"33e23879.cc1dc8","wires":[["9bdaeaf5.642518"]]},{"id":"cfe52e9b.301ad","type":"template","name":"too cold","template":"{\"d\":{\"message\":\"Too cold ({{payload.d.temp}})\"}}","x":802,"y":339,"z":"33e23879.cc1dc8","wires":[["9bdaeaf5.642518"]]},{"id":"ad930b65.526cf8","type":"template","name":"fine","template":"{\"d\":{\"message\":\"Fine ({{payload.d.temp}})\"}}","x":803,"y":390,"z":"33e23879.cc1dc8","wires":[["9bdaeaf5.642518"]]},{"id":"e208524c.1df7b","type":"template","name":"night","template":"{\"d\":{\"message\":\"Night ({{payload.d.light}})\"}}","x":799,"y":452,"z":"33e23879.cc1dc8","wires":[["9bdaeaf5.642518"]]},{"id":"9bdaeaf5.642518","type":"json","name":"","x":992,"y":353,"z":"33e23879.cc1dc8","wires":[["5ed0b558.a12f4c","fda526d8.025ad8"]]},{"id":"fda526d8.025ad8","type":"iot-app-out","name":"IoT App Out","topic":"iot-2/type/display/id/999999999999/cmd/message/fmt/json","x":1195,"y":386,"z":"33e23879.cc1dc8","wires":[]}]

…and you should see the above flow in the Node-RED flow editor. The thing that has changed from the previous video is that there is now an output node that publishes the resulting message to the MQTT broker. When you deploy the application (by selecting the Deploy button on the top right), you should start seeing messages appear on the LCD display.

That’s how you can use actuators in BlueMix, and send commands to Internet Things.