AVNET's iotconnect.io cloud platform is an online service that you can use to send data to, and then show it on a dashboard. In this blog series I'm learning how to talk to it with Node-RED. In this post: create and use a Gateway Device.
Authentication is with Trusted Platform Module (TPM). I'm actually using the TPM module and Pi from Infineon Trust Platform Module + Raspberry Pi 3 B - Review . |
What's a Gateway Device?
Luckily, I answer that in a previous post: Connect to AVNET iotconnect.io with Python IoTConnectSDK and BLE - part 6: Register as a Gateway Device
In that post, I review how Avnet's sample application integrates gateways and their client devices.
Short story:
A gateway can exchange with the cloud, data it measures itself itself, and data from other devices connected to it.
Unlike an edge device, it doesn't usually aggregate or process that data. Focus is on exchanging the raw telemetry.
Registering the Template with Gateway and Client Attributes
Creating a Gateway template is very similar to creating a Device one.
The only addition is the use of TAGs. These indicate if an attribute is for the Gateway itself, or for a particular type of Client Device.
Client Devices that share the same rules amongst each other, can use the same tag.
If you have 3 kinds of devices that all send temperature (even if they are of a different supplier, or different model), they could share the same tag.
For this post, I made an example template that knows the attributes of 2 entities:
- the gateway itself, with tag BLOG. Can send its firmware version and temperature.
- client devices of type self; with tag SELF. Can also send firmware version and temp. And gyro meter data (x, y, z).
Later, in the firmware, the gateway (I tagged it with BLOG) can only send telemetry data about itself that is in the list of attributes tagged BLOG.
It can however, send attributes with tag SELF for each registered client that's tagged with SELF.
Read this section a few times and you'll get it.
Register the Gateway Device and a Client Device
Now I'll create a new Gateway device that complies to (is linked to) the template created above.
Because the gateway is assigned to the template, and on the template the gateway attributes are tagged with BLOG, the gateway will be able to exchange these.
Now we'll register a first Client device for this gateway. The client device complies with type SELF, sow e tag it with the SELF tag.
The Gateway will now be able to send, for that Client Device, attributes that are tagged with SELF.
Later on in this post you'll see that I'm using a composite attribute gyroscope (x, y, z). I'll explain that in a next post.
In today's example, I'm adding clients manually.
There's an API to do this dynamically. If a gateway detects a client it understands, it can be registered against that gateway via a REST API.
The Avnet Python SDK that I discuss in post #7 does that.
Node-RED Flow to Send Gateway and Client Data
The flow looks similar to all previous ones.
The difference is in the message payload.
All previous posts had a single device in the payload. With a gateway you can have several.
When you transfer gateway data, you can use exactly the same format as with a common Device.
When sending Client data, you'll always have at least two blocks. Gateway and (one or more) Client.
The gateway section has to be present, but you don't have to send data in it. That's optional.
Here are 3 combinations (and one with random data for dynamic demo purposes):
1: Both Gateway and 1 Client data in the message
[ { "uniqueId": "7xxxx", "data": { "firmwareversion": 1, "temp": 1 } }, { "uniqueId": "7xxxx_self", "data": { "firmwareversion": 2, "temp": 2, "gyroscope": { "x": -1.2, "y": 0.25, "z": 1.1 } } } ]
2: Only 1 Client data in the message
[ { "uniqueId": "7xxxx", "data": {} }, { "uniqueId": "7xxxx_self", "data": { "firmwareversion": 3, "temp": 3, "gyroscope": { "x": -1.2, "y": 0.25, "z": 1.1 } } } ]
3: Only Gateway data in the message
[ { "uniqueId": "7xxxx", "data": { "firmwareversion": 4, "temp": 4 } } ]
I've provided option 4. in my Node-RED flow to randomize part of the data and send it at regular times. Just for demo purposes - to get a dashboard that looks real.
The message has data for both gateway and 1 client, and 5 Random Function blocks simulate seemingly real values for the data.
click for a readable version
Final flow and source:
If you have an iotconnect.io demo account, you can import the following source in Node-RED. First install the necessary SDK modules - see Node-RED blog #4 in the table at the end of this post.
By default, the whole process is disabled. That's to allow you to adapt the connection and identification settings before enabling.
Don't enable this, unless you have set up the assets on iotconnect.io, and adapted the unique IDs and CPIDs.
If you have a real (not demo) account, you must also change the idScope value on the connection node to the ID of your application. The Random injector exchanges data every 2 minutes. If you get billed per message, you may want to change that.
Unlike the majority of palette modules, The Node-RED SDK from Avnet/iotconnect.io can render your Node-RED unresponsive or makes it continuously restart if there are connectivity errors.
Back up your *flows*.json file in your node-red directory.
[ { "id": "c44fe94e221ba0da", "type": "tab", "label": "IoTConnect TPM Gateway", "disabled": true, "info": "", "env": [] }, { "id": "57e5858f09c98972", "type": "group", "z": "c44fe94e221ba0da", "name": "random", "style": { "label": true }, "nodes": [ "69652348b95f44c8", "cabc5080ea57d5bd", "664b02174711caec", "7a6fa241f97cab14", "7a252521135b3ef3" ], "env": [], "x": 234, "y": 259, "w": 242, "h": 82 }, { "id": "cc2db60ddde59b24", "type": "IoTConnect", "z": "c44fe94e221ba0da", "cpId": "<YOUR-CPID>", "uniqueId": "<YOUR-TPM-REGISTRY-CODE>", "env": "PROD", "auth": "4", "offlineStorageEnable": false, "sizeLimit": 0, "fileCount": 0, "sslKey": "", "sslCert": "", "sslCA": "", "idScope": "0ne0005B8E6", "x": 735, "y": 220, "wires": [ [ "ed847c762ac3a801", "55533cf4629a2eb9" ], [ "6c3608bac5c30558" ], [] ], "l": false }, { "id": "90509c9e9cde4f7c", "type": "IoTConnect-Injector", "z": "c44fe94e221ba0da", "iType": "SendData", "descText": "", "payload": "[\n {\n \"uniqueId\": \"<YOUR-TPM-REGISTRY-CODE>\",\n \"time\": \"2021-11-17T11:27:29.680Z\",\n \"data\": {\n \"firmwareversion\": \"\"\n }\n },\n {\n \"uniqueId\": \"<YOUR-TPM-REGISTRY-CODE>self\",\n \"time\": \"2021-11-17T11:27:29.680Z\",\n \"data\": {\n \"firmwareversion\": \"\"\n }\n }\n] \n", "x": 575, "y": 220, "wires": [ [ "cc2db60ddde59b24", "43d6e4710604c26a" ] ], "l": false }, { "id": "3af0d8b8686b19b2", "type": "inject", "z": "c44fe94e221ba0da", "name": "gateway and client data", "props": [ { "p": "payload" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "[{\"uniqueId\":\"<YOUR-TPM-REGISTRY-CODE>\",\"data\":{\"firmwareversion\":1,\"temp\":1}},{\"uniqueId\":\"<YOUR-TPM-REGISTRY-CODE>self\",\"data\":{\"firmwareversion\":2,\"temp\":2,\"gyroscope\":{\"x\":-1.2,\"y\":0.25,\"z\":1.1}}}]", "payloadType": "json", "x": 280, "y": 100, "wires": [ [ "90509c9e9cde4f7c" ] ] }, { "id": "69652348b95f44c8", "type": "random", "z": "c44fe94e221ba0da", "g": "57e5858f09c98972", "name": "random gateway temp", "low": "-10", "high": "45", "inte": "true", "property": "payload[0].data.temp", "x": 275, "y": 300, "wires": [ [ "cabc5080ea57d5bd" ] ], "l": false }, { "id": "eb6f8c411fb7ca4f", "type": "inject", "z": "c44fe94e221ba0da", "name": "", "props": [ { "p": "payload" } ], "repeat": "120", "crontab": "", "once": true, "onceDelay": "60", "topic": "", "payload": "[{\"uniqueId\":\"<YOUR-TPM-REGISTRY-CODE>\",\"data\":{\"firmwareversion\":5,\"temp\":0}},{\"uniqueId\":\"<YOUR-TPM-REGISTRY-CODE>self\",\"data\":{\"firmwareversion\":5,\"temp\":0,\"gyroscope\":{\"x\":0,\"y\":0,\"z\":0}}}]", "payloadType": "json", "x": 195, "y": 300, "wires": [ [ "69652348b95f44c8" ] ], "l": false }, { "id": "f9cf6c9acc85dfd3", "type": "comment", "z": "c44fe94e221ba0da", "name": "iotconnect.io \\n send", "info": "", "x": 610, "y": 160, "wires": [] }, { "id": "c974a42b1cb707c2", "type": "comment", "z": "c44fe94e221ba0da", "name": "iotconnect.io \\n communicate", "info": "", "x": 790, "y": 160, "wires": [] }, { "id": "bcb8a4a28e1c66ab", "type": "comment", "z": "c44fe94e221ba0da", "name": "telemetry", "info": "", "x": 220, "y": 60, "wires": [] }, { "id": "ed847c762ac3a801", "type": "debug", "z": "c44fe94e221ba0da", "name": "", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "true", "targetType": "full", "statusVal": "", "statusType": "auto", "x": 1015, "y": 220, "wires": [], "icon": "font-awesome/fa-bug", "l": false }, { "id": "73603164f036fb8d", "type": "comment", "z": "c44fe94e221ba0da", "name": "iotconnect.io \\n receive", "info": "", "x": 1030, "y": 160, "wires": [] }, { "id": "6c3608bac5c30558", "type": "debug", "z": "c44fe94e221ba0da", "name": "", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "true", "targetType": "full", "statusVal": "", "statusType": "auto", "x": 835, "y": 240, "wires": [], "icon": "font-awesome/fa-bug", "l": false }, { "id": "c46c7e696ddabf3b", "type": "IoTConnect-Injector", "z": "c44fe94e221ba0da", "iType": "Disconnect", "descText": "", "payload": "", "x": 350, "y": 440, "wires": [ [ "cc2db60ddde59b24" ] ] }, { "id": "ef8c794883c52228", "type": "IoTConnect-Injector", "z": "c44fe94e221ba0da", "iType": "Connect", "descText": "", "payload": "", "x": 360, "y": 480, "wires": [ [ "cc2db60ddde59b24" ] ] }, { "id": "8491cef36d91bd0f", "type": "IoTConnect-Injector", "z": "c44fe94e221ba0da", "iType": "SendCommandAck", "descText": "", "payload": "", "x": 855, "y": 300, "wires": [ [ "cc2db60ddde59b24" ] ], "l": false }, { "id": "55533cf4629a2eb9", "type": "switch", "z": "c44fe94e221ba0da", "name": "ACK?", "property": "payload.ack", "propertyType": "msg", "rules": [ { "t": "true" }, { "t": "false" } ], "checkall": "false", "repair": false, "outputs": 2, "x": 670, "y": 340, "wires": [ [ "da1cbf9eb6a6f814" ], [ "8182ce160de0637a" ] ] }, { "id": "41b54d8a50a95c15", "type": "debug", "z": "c44fe94e221ba0da", "name": "", "active": false, "tosidebar": true, "console": false, "tostatus": false, "complete": "\"ACK sent\"", "targetType": "jsonata", "statusVal": "", "statusType": "auto", "x": 835, "y": 340, "wires": [], "icon": "font-awesome/fa-bug", "l": false }, { "id": "8182ce160de0637a", "type": "debug", "z": "c44fe94e221ba0da", "name": "", "active": false, "tosidebar": true, "console": false, "tostatus": false, "complete": "\"ACK not required\"", "targetType": "jsonata", "statusVal": "", "statusType": "auto", "x": 835, "y": 380, "wires": [], "icon": "font-awesome/fa-bug", "l": false }, { "id": "61815deccc9413af", "type": "comment", "z": "c44fe94e221ba0da", "name": "receipt mgt", "info": "", "x": 650, "y": 381, "wires": [] }, { "id": "da1cbf9eb6a6f814", "type": "function", "z": "c44fe94e221ba0da", "name": "", "func": "msg.payload['st']=6\nmsg.payload['msg']=\"ACK Success\"\nreturn msg;", "outputs": 1, "noerr": 0, "initialize": "", "finalize": "", "x": 765, "y": 321, "wires": [ [ "8491cef36d91bd0f", "41b54d8a50a95c15" ] ], "l": false }, { "id": "43d6e4710604c26a", "type": "debug", "z": "c44fe94e221ba0da", "name": "", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "true", "targetType": "full", "statusVal": "", "statusType": "auto", "x": 715, "y": 100, "wires": [], "icon": "font-awesome/fa-bug", "l": false }, { "id": "cabc5080ea57d5bd", "type": "random", "z": "c44fe94e221ba0da", "g": "57e5858f09c98972", "name": "random client temp", "low": "-10", "high": "45", "inte": "true", "property": "payload[1].data.temp", "x": 315, "y": 300, "wires": [ [ "664b02174711caec" ] ], "l": false }, { "id": "8c630752cc3d697c", "type": "inject", "z": "c44fe94e221ba0da", "name": "client data only", "props": [ { "p": "payload" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "[{\"uniqueId\":\"<YOUR-TPM-REGISTRY-CODE>\",\"data\":{}},{\"uniqueId\":\"<YOUR-TPM-REGISTRY-CODE>self\",\"data\":{\"firmwareversion\":3,\"temp\":3,\"gyroscope\":{\"x\":-1.2,\"y\":0.25,\"z\":1.1}}}]", "payloadType": "json", "x": 260, "y": 160, "wires": [ [ "90509c9e9cde4f7c" ] ] }, { "id": "d589c7fe4ae157d6", "type": "inject", "z": "c44fe94e221ba0da", "name": "gateway data only", "props": [ { "p": "payload" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "[{\"uniqueId\":\"<YOUR-TPM-REGISTRY-CODE>\",\"data\":{\"firmwareversion\":4,\"temp\":4}}]", "payloadType": "json", "x": 270, "y": 220, "wires": [ [] ] }, { "id": "664b02174711caec", "type": "random", "z": "c44fe94e221ba0da", "g": "57e5858f09c98972", "name": "random g.x", "low": "-3", "high": "+3", "inte": "false", "property": "payload[1].data.gyroscope.x", "x": 355, "y": 300, "wires": [ [ "7a6fa241f97cab14" ] ], "l": false }, { "id": "7a6fa241f97cab14", "type": "random", "z": "c44fe94e221ba0da", "g": "57e5858f09c98972", "name": "random g.y", "low": "-3", "high": "+3", "inte": "false", "property": "payload[1].data.gyroscope.y", "x": 395, "y": 300, "wires": [ [ "7a252521135b3ef3" ] ], "l": false }, { "id": "7a252521135b3ef3", "type": "random", "z": "c44fe94e221ba0da", "g": "57e5858f09c98972", "name": "random g.z", "low": "-3", "high": "+3", "inte": "false", "property": "payload[1].data.gyroscope.z", "x": 435, "y": 300, "wires": [ [ "90509c9e9cde4f7c" ] ], "l": false }, { "id": "377767a5115b852f", "type": "comment", "z": "c44fe94e221ba0da", "name": "connection", "info": "", "x": 320, "y": 400, "wires": [] } ]
iotconnect.io Receives Telemetry
Here is a Gateway data transfer:
And a Client data transfer:
Here's a mix of Gateway and Client data presented on a dashboard:
Not covered: handling ACKs
I'll write another post that shows how too handle OTA ACKs for the Gateway and for a particular client. I need to read up on that first.