Virtual Device Simulator

This flow acts like a virtual device simulator that sends Json data to Mqtt brokers(servers) on the internet. In my example I send it to test.mosquitto.org and AWS IoT devices. Pretty simple flow. You can add additional devices if you like.

[{"id":"6dd95995.8f3ca8","type":"inject","z":"a4fd0a97.d6a5c","name":"Send Message","topic":"","payload":"","payloadType":"num","repeat":"","crontab":"","once":false,"x":119,"y":189,"wires":[["901dc50a.12bf"]]},{"id":"b25e6d55.9f44d","type":"debug","z":"a4fd0a97.d6a5c","name":"Main Debug","active":true,"console":"false","complete":"payload","x":553,"y":113,"wires":[]},{"id":"b3ee66c.31af698","type":"comment","z":"a4fd0a97.d6a5c","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":564,"y":70,"wires":[]},{"id":"f0c71016.7d753","type":"comment","z":"a4fd0a97.d6a5c","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":99,"y":149,"wires":[]},{"id":"901dc50a.12bf","type":"function","z":"a4fd0a97.d6a5c","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 randomTemp = (Math.random() * (30 - (-30)) + (-30)).toFixed(1);  //random temp from -30.0 to 30.0 degrees\n\nvar timeIndex = Date.now();\n//This function below gets date and time and displays it via shortDate variable\nvar timeStamp = new Date().toLocaleTimeString();\nvar dateObj= new Date();\nvar newDate = dateObj.getFullYear() + '/' + ('0' + (dateObj.getMonth() + 1)).slice(-2) + '/' + ('0' + dateObj.getDate()).slice(-2) + ' <' + timeStamp + '>';\n\n// var month = ('0' + (dateObj.getMonth() + 1)).slice(-2);\n// var date = ('0' + dateObj.getDate()).slice(-2);\n// var year = dateObj.getFullYear();\n// var dateTime = year + '/' + month + '/' + date + ' <' + timeStamp + '>';\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               \"uid\" : timeIndex,\n               \"timeStamp\" : newDate,\n                \"coord\" : \n                {\n                    \"lat\" : lat[idx],\n                    \"lon\" : lon[idx]\n                },\n                \"company\" : \"Acme Corp\",\n                \"avgTemp\" : randomTemp\n            };\n\nmsg.payload = JSON.stringify(msg.payload);\n\nreturn msg;","outputs":1,"noerr":0,"x":336,"y":189,"wires":[["b25e6d55.9f44d","31ac6556.03bb5a","28bc2962.28766e"]]},{"id":"31ac6556.03bb5a","type":"mqtt out","z":"a4fd0a97.d6a5c","name":"Mosquitto.org Mqtt","topic":"testsak","qos":"","retain":"","broker":"5cb9e151.9886b8","x":591,"y":183,"wires":[]},{"id":"28bc2962.28766e","type":"mqtt out","z":"a4fd0a97.d6a5c","name":"AWS Mqtt","topic":"testsak","qos":"","retain":"","broker":"5cb9e151.9886b8","x":571,"y":246,"wires":[]},{"id":"5cb9e151.9886b8","type":"mqtt-broker","z":"","broker":"a30dilo1soj41h.iot.us-east-1.amazonaws.com","port":"8883","tls":"aeb0331d.fe2ad","clientid":"DataTrafficSim","usetls":true,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"aeb0331d.fe2ad","type":"tls-config","z":"","name":"AWS","cert":"C:\\Users\\SakDev\\Desktop\\Dev\\AWS\\Certs\\MySimulatedDevice\\15a6d5bf2f-certificate.pem.crt","key":"C:\\Users\\SakDev\\Desktop\\Dev\\AWS\\Certs\\MySimulatedDevice\\15a6d5bf2f-private.pem.key","ca":"C:\\Users\\SakDev\\Desktop\\Dev\\AWS\\Certs\\MySimulatedDevice\\rootCA.pem","certname":"","keyname":"","caname":"","verifyservercert":true}]
baraynavab

Flow Info

created 2 weeks, 2 days ago

Node Types

Core
  • comment (x2)
  • debug (x1)
  • function (x1)
  • inject (x1)
  • mqtt out (x2)
  • mqtt-broker (x1)
Other
  • tls-config (x1)

Tags

  • AWS
  • Simulator
  • IoT
  • Virtual
  • Device
  • MQTT
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option