IoT Cloud Device simulator in a minute using Node-RED

In my last post I used a Ruby based device simulator publishing MQTT messages to IBM IoT Cloud. In the meantime I realized that the same can be easily achieved using Node-RED tool.

Here is how.

Create a directory for Node-REDn and enter it.

mkdir Node-RED && cd Node-RED

Install Node.js if you do not have it already installed.

Install Node-RED tool and several useful additional components.

npm install node-red node-red-contrib-async node-red-ibmiotpush node-red-iotcloudapp node-red-iotclouddev node-red-node-cf-cloudant node-red-node-sqlite node-red-node-suncalc node-red-node-twilio

Start the Node-RED server

node node_modules/node-red/red.js

Open a browser and point it to the server URL:

http://127.0.0.1:1880/

And copy the following text into clipboard.

[{"id":"115fc1f5.eea03e","type":"mqtt-broker","broker":"kbrhh.messaging.internetofthings.ibmcloud.com","port":"1883","clientid":"d:kbrhh:sensor-type:863835454201"},{"id":"3ffdf51f.c0020a","type":"inject","name":"Send message","topic":"iot-2/evt/event1/fmt/json","payload":"Test","payloadType":"date","repeat":"","crontab":"","once":false,"x":165.5454559326172,"y":119.27274560928345,"z":"e406d10.f1bf93","wires":[["d189022.f2e77"]]},{"id":"6dd6373c.9229c8","type":"debug","name":"Show message","active":true,"console":"false","complete":"true","x":675,"y":122,"z":"e406d10.f1bf93","wires":[]},{"id":"b6d7d519.492828","type":"mqtt out","name":"MQTT Publisher","topic":"iot-2/evt/event1/fmt/json","broker":"115fc1f5.eea03e","x":680,"y":313.6363868713379,"z":"e406d10.f1bf93","wires":[]},{"id":"d189022.f2e77","type":"function","name":"Enhancer 2","func":"var temp = context.temp || 37;\nvar pulse = context.pulse || 89;\n\n\ntemp = temp + Math.floor(Math.random() * 3 - 1);\npulse = pulse + Math.floor(Math.random() * 3 - 1);\n\ncontext.temp = temp;\ncontext.pulse = pulse;\n\nmsg.payload = { \"temp\": temp, \"pulse\": pulse, \"timestamp\": msg.payload };\n\n\nreturn msg;","outputs":1,"x":413,"y":222,"z":"e406d10.f1bf93","wires":[["6dd6373c.9229c8","b6d7d519.492828"]]}]

Now just modify the „MQTT Publisher“ properties and you are ready to go!

Screen Shot 2014-08-06 at 16.37.57

Press the pad next to Send message node and it should fire an event to IoT! You can modify Send message node parameters to periodically send a message without the need to press the pad…

(Assumed that you have created accounts and registered a device as described in the previous post.)

Edited 7.8.2014:

Topic in the Node-RED flow changed to correct:

iot-2/evt/event1/fmt/json

Otherwise payload is not recognized as JSON format and cannot be stored in DB in a parsabe way…

Napsat komentář

Vaše emailová adresa nebude zveřejněna. Vyžadované informace jsou označeny *