Description
The Node-red includes documentation and a usage guide for the user to connect the device to the IoTConnect.
Prerequisite tools:
- Node version: 18.4.4
- NodeRed version: v2.2.2
Go to the node-red folder and run below two npm commands
npm install axios
npm install async-mqtt
npm install fs
Step 2: Setup environment variables
Before starting you need to setup IoTConnect variables like environment, cpid & uniqueid, and add module in global context.
Step 2.1: Setup IoTConnect sub flow environment variables
Open settings.js file form your Node-RED user directory add below lines before module.exports =
process.env.iotc_cpid = “”;
process.env.iotc_env = “”;
WARNING: If above settings are missing or blank then IoTConnect MQTT Sub flow will not start and give exception
step 2.2: Add modules in global context
Search for function functionGlobalContext and add two lines
axiosModule:require(‘axios’),
asyncmqtt:require(‘async-mqtt’)
fs:require(‘fs’)
step 3: Import sub-flow into your node-red
WARNING: Do not make any changes to this flow.
step 4: Create your flow or add sub flow in your existing flow [below is snap of new flow]
Sub flow have one input and 3 output.
Input#1: Ingestion should have format as mentioned below
- uniqueId: device uniqueid
- time: current time
- data: attribute json
Output #1: Gives telemetry log
Output #2: Gives cloud to device commands
Output #3: Gives error
How it works?
- Connection will get created on first inject of individual device
- On first inject it will ask for IoTConnect identity and if device is okay then it will connect and send data. if device is not okay then it will add data into memory queue.
- Below state will add data in memory queue
- Device initialization failed due to device not found, device not registered/acquired, device is inactive & cpid not found
- On reconnect it will send data form memory queue first
- If connection got lost after successful connection it will start adding data into queue
- If connection is lost during publishing data; it will wait for some second to restore connection if not, then it will drop data.
- On connection break, it will check internet connectivity every 10 sec – if restored data will be sent form queue
- On 10 consecutive failed attempts of initialization, it will wait for 10 sec – data will be added to memory queue
- Telemetry output will display data which has been sent
- Error output will show error occurred during operation
- Command output will give two commands
- cmdType: 0x01 (Simple command)
{ "cmdType": "0x01", "data": { "cpid": "", "guid": "", "uniqueId": "", "command": "", "ack": false, "ackId": "", "cmdType": "0x01" } }
-
- cmdType: 0x02 (OTA Command)
{ "cmdType": "0x02", "data": { "cpid": "", "guid": "", "uniqueId": "", "command": "ota", "ack": false, "ackId": "", "cmdType": "0x02", "ver": { "sw": "", "hw": "" }, "urls": [{ "url": "", "tag": "" } ] } }