Skip to content

Device Simulator with Node RED

HNorlen edited this page Aug 29, 2016 · 3 revisions

This page describes a device simulator, created with Node-Red, that sends MQTT messages to the IoT Platform.

Device simulator in Node-Red: Node-Red device simulator

Copy and import this flow into Node-RED:

[{"id":"26772df.195bfd2","type":"mqtt out","z":"cc43659.76d3b98","name":"Publish to IoT","topic":"iot-2/evt/scanEvent/fmt/json","qos":"","retain":"","broker":"6e1fa130.9c4a","x":640,"y":160,"wires":[]},{"id":"8bbb3a0d.1aef18","type":"inject","z":"cc43659.76d3b98","name":"Send Message","topic":"","payload":"","payloadType":"num","repeat":"","crontab":"","once":false,"x":160,"y":160,"wires":[["d991b19f.92451"]]},{"id":"d991b19f.92451","type":"function","z":"cc43659.76d3b98","name":"Generate Payload","func":"var lat = [ 29.42, 30.26, 32.77, 36.37, 38.62, 39.09, 41.87, 43.03, 44.97, 41.25];\nvar lon = [-98.49,-97.74,-96.79,-94.20,-90.19,-94.57,-87.62,-87.90,-93.26,-95.99];\nvar tmp = (Math.random() * (30 - (-30)) + (-30)).toFixed(1);  //random temp from -30.0 to 30.0 degrees\n\nvar idx = context.get('count') || 0;\n\nif( idx > 9 )\n{\n    idx = 0;  \n}\ncontext.set('count',idx+1); //store next idx for later\n\nmsg.payload = \n            {\n                \"packageNum\" : \"ABC123456\",\n                \"coord\" : \n                {\n                    \"lat\" : lat[idx],\n                    \"lon\" : lon[idx]\n                },\n                \"company\" : \"ACME Corporation\",\n                \"avgTemp\" : tmp\n            };\n\nmsg.payload = JSON.stringify(msg.payload);\n\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":160,"wires":[["cba73eee.ee3b7","26772df.195bfd2"]]},{"id":"cba73eee.ee3b7","type":"debug","z":"cc43659.76d3b98","name":"","active":true,"console":"false","complete":"false","x":630,"y":240,"wires":[]},{"id":"707bd6fb.7eefc8","type":"comment","z":"cc43659.76d3b98","name":"Configure Publish to IoT for your environment","info":"1. Double click the 'Publish to IoT' node.\n2. Click the edit icon next to the Server field.\n3. Update the Server name to correspond to your IoT Platform organization.\n    `{{your org id}}.messaging.internetofthings.ibmcloud.com`\n\n4. Update the Client ID with your orgainzaiton ID, device type, and device ID:\n    `d:{{your org id}}:device_type:device_ID`\n\n5. Click the Security tab.\n6. Set Username to `use-token-auth`.\n7. Set Password to the Auth Token obtained when you created a device in the Watson IoT Platfom.\n8. Click Update and then OK.\n9. In the upper right corner of the Node-RED flow editor, click Deploy. ","x":650,"y":120,"wires":[]},{"id":"d0dbb9b7.4b4a28","type":"comment","z":"cc43659.76d3b98","name":"Inject Node","info":"The Inject node allows you to inject messages into a flow, either by clicking the button on the node, or setting a time interval between injects.","x":150,"y":120,"wires":[]},{"id":"6e1fa130.9c4a","type":"mqtt-broker","z":"cc43659.76d3b98","broker":"{{orgid}}.messaging.internetofthings.ibmcloud.com","port":"1883","clientid":"d:{{orgid}}:myScanners:scanner001","usetls":false,"verifyservercert":true,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willRetain":"false","willPayload":"","birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":""}]

Here's a sample MQTT payload sent by the device simulator:

{
  "packageNum":"ABC123456",
  "coord":
  {
    "lat":29.42,
    "lon":-98.49
  },
  "company":"ACME Corporation",
  "avgTemp":"-21.6"
}
Clone this wiki locally