Using NodeRed and IBM Bluemix with Evok over websockets

Home Tutorials Using NodeRed and IBM Bluemix with Evok over websockets Back

Simple example of using NodeRed via websocket

 IBM has been developing some great tools for IoT. One of them is called Node-RED which is a great tool for wiring hardware together. It allows users to easily write behavior of the hardware devices in javascript. They even went further and developed a cloud platform for IoT called Bluemix which is a basically virtual server where you can run and link together multiple applications, services, databases and virtual machines. We will only use a small part of this great platform for this tutorial.


Prepare SW

Before you can start, you need a UniPi with our opensource Evok API configured and running. We recommend default Raspbian but we are also working on our own modified Minibian distribution.

Next you need to install and run local Node-RED instance according to this tutorial.

You will also need a Bluemix account if you want to try to get the UniPi on the cloud.

Node-RED on UniPi

We will use the advantages of both evok and Node-RED that they support communication over websocket. The principle is described on this picture.

Evok acts as a websocket server where clients can connect to. Once they are connected they can receive and also send messages to the UniPi over the socket.

Local control of UniPi

We have prepared a simple Node-RED example which allows you to easily control the UniPi with Node-RED. It consists of a only websocket node which connects to the UniPi and a few inject nodes to send messages to the UniPi ws. See the content of the inject nodes for example of message payload. Description of all possible commands can be found on the evok github repository.

Try clicking to the blue inject nodes and see the response of UniPi.

[{"id":"a3c2ed71.5c3d1","type":"websocket-client","path":"ws://","wholemsg":"false"},{"id":"c7a9d1f1.38563","type":"websocket out","name":"","server":"","client":"a3c2ed71.5c3d1","x":643,"y":272,"z":"2b1b35ba.d4e4ca","wires":[]},{"id":"80afc33f.7f504","type":"inject","name":"R1 ON","topic":"","payload":"{\"dev\":\"relay\", \"circuit\":\"1\", \"value\":\"1\"}","payloadType":"string","repeat":"","crontab":"","once":false,"x":363,"y":199,"z":"2b1b35ba.d4e4ca","wires":[["c7a9d1f1.38563"]]},{"id":"3964a515.c69b5a","type":"inject","name":"R1 OFF","topic":"","payload":"{\"dev\":\"relay\", \"circuit\":\"1\", \"value\":\"0\"}","payloadType":"string","repeat":"","crontab":"","once":false,"x":357,"y":273,"z":"2b1b35ba.d4e4ca","wires":[["c7a9d1f1.38563"]]},{"id":"aa9c56df.5563a8","type":"inject","name":"AO set 5V a","topic":"","payload":"{\"dev\":\"ao\", \"circuit\":\"1\", \"value\":\"5\"}","payloadType":"string","repeat":"","crontab":"","once":false,"x":363,"y":114,"z":"2b1b35ba.d4e4ca","wires":[["c7a9d1f1.38563"]]}]

This example only shows a basic messaging commands but you can use the function codes to describe your own behavior into a complex function blocks.

UniPi on the Cloud

We will extend the previous example by giving the UniPi the possibility to talk talk to the Bluemix cloud service via MQTT queue at which provides a free sandbox for testing IoT projects.

Once you have registered your free trial at Bluemix, go to the console and in the Catalog find Node-RED starter in Boilerplates category, name your application and fill out the hostname. Once you hit the create button, wait for your app to start and visit your Node-RED flow editor on Bluemix.

You do not need to create any account at just create a unique Topic (eg. UniPi_to_Bluemix or Bluemix_to_UniPi) so you wont mix your messages with others using the sandbox.

Node-RED - UniPi

Are you ready? Lets get coding! Or perhaps... flowing?

Next you need to tell UniPi to send all messages to the MQTT server and also to receive messages. For that you need to create a MQTT input node and also mqtt output node. Make sure not to name topics of both of them same so you don't mix messages send to the UniPi and to the Bluemix. Below is an extended version of the previous example also with a debug node so you can see what messages are being passed.

[{"id":"a3c2ed71.5c3d1","type":"websocket-client","path":"ws://","wholemsg":"false"},{"id":"b826f19f.47d91","type":"mqtt-broker","broker":"","port":"1883","clientid":""},{"id":"821258ce.7deda8","type":"mqtt in","name":"","topic":"Bluemix -> Unipi Topic","broker":"b826f19f.47d91","x":341,"y":422,"z":"2b1b35ba.d4e4ca","wires":[["c7a9d1f1.38563","f950058b.06aff8"]]},{"id":"c7a9d1f1.38563","type":"websocket out","name":"","server":"","client":"a3c2ed71.5c3d1","x":643,"y":272,"z":"2b1b35ba.d4e4ca","wires":[]},{"id":"80afc33f.7f504","type":"inject","name":"R1 ON","topic":"","payload":"{\"dev\":\"relay\", \"circuit\":\"1\", \"value\":\"1\"}","payloadType":"string","repeat":"","crontab":"","once":false,"x":363,"y":199,"z":"2b1b35ba.d4e4ca","wires":[["c7a9d1f1.38563"]]},{"id":"3964a515.c69b5a","type":"inject","name":"R1 OFF","topic":"","payload":"{\"dev\":\"relay\", \"circuit\":\"1\", \"value\":\"0\"}","payloadType":"string","repeat":"","crontab":"","once":false,"x":357,"y":273,"z":"2b1b35ba.d4e4ca","wires":[["c7a9d1f1.38563"]]},{"id":"aa9c56df.5563a8","type":"inject","name":"AO set 5V a","topic":"","payload":"{\"dev\":\"ao\", \"circuit\":\"1\", \"value\":\"5\"}","payloadType":"string","repeat":"","crontab":"","once":false,"x":363,"y":114,"z":"2b1b35ba.d4e4ca","wires":[["c7a9d1f1.38563"]]},{"id":"efd26312.102da","type":"websocket in","name":"","server":"","client":"a3c2ed71.5c3d1","x":364,"y":627,"z":"2b1b35ba.d4e4ca","wires":[["69420e76.96bdf","f950058b.06aff8"]]},{"id":"69420e76.96bdf","type":"mqtt out","name":"","topic":"Unipi -> Bluemix Topic","qos":"","retain":"","broker":"b826f19f.47d91","x":629,"y":627,"z":"2b1b35ba.d4e4ca","wires":[]},{"id":"f950058b.06aff8","type":"debug","name":"","active":true,"console":"false","complete":"payload","x":616,"y":490,"z":"2b1b35ba.d4e4ca","wires":[]}]

Node-RED - Bluemix

Visit the Node-RED flow editor of your Bluemix and import this code.

[{"id":"69cf13da.9630ec","type":"mqtt-broker","broker":"","port":"1883","clientid":""},{"id":"3587e3c4.ca781c","type":"inject","name":"Set Relay 1 ON","topic":"UniPi_test_BM","payload":"{\"dev\":\"relay\", \"circuit\":\"1\", \"value\":\"1\"}","payloadType":"string","repeat":"","crontab":"","once":false,"x":523,"y":468,"z":"c8ebff8.f3714","wires":[["4640a6f1.b9bf58"]]},{"id":"47d95b68.b826a4","type":"inject","name":"Set Relay 1 OFF","topic":"UniPi_test_BM","payload":"{\"dev\":\"relay\", \"circuit\":\"1\", \"value\":\"0\"}","payloadType":"string","repeat":"","crontab":"","once":false,"x":525,"y":514,"z":"c8ebff8.f3714","wires":[["4640a6f1.b9bf58"]]},{"id":"4640a6f1.b9bf58","type":"mqtt out","name":"","topic":"Bluemix -> Unipi Topic","qos":"","retain":"","broker":"69cf13da.9630ec","x":761,"y":487,"z":"c8ebff8.f3714","wires":[]},{"id":"e33d237b.1cc2e","type":"mqtt in","name":"","topic":"Unipi -> Bluemix Topic","broker":"69cf13da.9630ec","x":262,"y":188,"z":"c8ebff8.f3714","wires":[["d52e5b57.2ad1a8","d73a77d5.28c588","3d062bfd.c2f9d4"]]},{"id":"d52e5b57.2ad1a8","type":"function","name":"IN 1-7 -> RELAY 1-7","func":"var status = JSON.parse(msg.payload);\nvar msg = {};\nif ( == \"input\" && status.circuit<8) {\n\t//leave the relay 8 to the temp control node\n\tif (status.value == 0) {\n\t\tmsg.payload = {\"dev\":\"relay\", \"circuit\":\"\", \"value\":\"0\"};\n\t\tmsg.payload[\"circuit\"] = status.circuit;\n\t}\n\telse {\n\t\tmsg.payload = {\"dev\":\"relay\", \"circuit\":\"\", \"value\":\"1\"};\n\t\tmsg.payload[\"circuit\"] = status.circuit;\n\t}\n\treturn msg;\n}\nelse return;\n","outputs":1,"x":512,"y":349,"z":"c8ebff8.f3714","wires":[["4640a6f1.b9bf58","95b369c3.6a4c98"]]},{"id":"d73a77d5.28c588","type":"function","name":"Temp >= 26 -> R8 On","func":"var status = JSON.parse(msg.payload);\nvar newMsg = {};\nif ( == \"temp\" && status.address==\"28E9E194040000C7\") {\n\tnewMsg.payload = {\"dev\":\"relay\", \"circuit\":\"8\"}\n\tif (status.value>=26) newMsg.payload[\"value\"]=1;\n\telse newMsg.payload[\"value\"]=0;\n\treturn newMsg;\n}\nelse return;","outputs":1,"x":509,"y":397,"z":"c8ebff8.f3714","wires":[["4640a6f1.b9bf58","c5f28a6b.3a0d78"]]},{"id":"95b369c3.6a4c98","type":"debug","name":"","active":true,"console":"false","complete":"payload","x":768,"y":349,"z":"c8ebff8.f3714","wires":[]},{"id":"c5f28a6b.3a0d78","type":"debug","name":"","active":true,"console":"false","complete":"false","x":768,"y":396,"z":"c8ebff8.f3714","wires":[]},{"id":"3d062bfd.c2f9d4","type":"debug","name":"","active":true,"console":"false","complete":"false","x":531,"y":188,"z":"c8ebff8.f3714","wires":[]}]

It will create both MQTT input and output nodes (make sure to give them topic names according to the nodes on UniPi) and some debug nodes (you can turn the debug nodes off by pressing the green button on the right side or delete them completely). It also contains the same inject nodes as on the UniPi for controlling relays. However it does not provide the inject node to set the analog output value. Try it on your own either by creating a new node or export the inject node from the UniPi and import it to Bluemix.

We have also created two custom function nodes to give you basic idea of writing your own behavior. The upper node called 'IN 1-7 -> RELAY 1-7' reacts on messages that contains information about the digital inputs of UniPi and accordingly switches relays on/off. So if you switch the digital input 1 on relay 1 will switch on also.

The second function node named 'Temp >= 26 -> R8 On' driver relay 8 according to a temperature read from a 1Wire sensor connected to the UniPi. If temperature on the sensor exceeds 26 degrees, relay 8 will switch on otherwise it switches off. You will need to modify address of the sensor in the function node to your sensor (If you need to know the address of your connected sensors visit the evok web on port 80 of your UniPi).

The great on this example is that you can export any of these function nodes and import them in the Node-RED on UniPi, wire it together in between the websockets of UniPi (WS out -> function in, function out -> WS in) and it will work right away without the delay caused by sending the messages around the world and without the need of reliable network connection. It is also a great example how you can put mission critical function nodes on the local machine and let the cloud take care of loggin of your data to database or providing web interface with basic control ability.

Our parts

UniPi 1.1
More than 20 items in stock

Add to cart
Subscribe to our newsletter