Insights

|

08 Jan 2022

如何使用 u-blox Thingstream 将物联网设备连接到 Cumulocity

u-blox 物联网通信即服务平台 Thingstream 可通过 MQTT 将物联网设备连接到世界各地的 Cumulocity。

cumulocity iot

CumulocitySoftware AG 推出的物联网平台,可提供物联网设备管理、数据可视化和 API,旨在将物联网与企业网络的其他部分连接起来。

Cumulocity 通过 Thingstream 服务交付平台u-blox 全球通信网络无缝集成在一起,实现设备与企业之间的全球通信。按照本指南使用 u-blox Thingstream 将您的物联网设备与 Cumulocity 平台连接起来。

将数据从设备传输到 Cumulocity 的最简单的方法是通过MQTT(MQ 遥测传输)。Software AG 提供了许多 MQTT 静态模板,您可以使用这些模板在 Cumulocity 中快速注册设备,并发送常见用例的更新,包括位置更新和发送遥测。

立即将 Cumulocity 连接到 u-blox Thingstream

在 Thingstream 数据流中使用 Cumulocity MQTT 静态模板

要在 Thingstream 数据流中使用静态模板,您需要添加函数节点(如下图所示)以使用所需格式为要使用的 Cumulocity 静态模板创建有效负载。

Function node - Thingstream

MQTT 发布到 Cumulocity 需要使用设备唯一的客户端 ID。一个好主意是从 Thingstream 客户端 ID 派生该 ID,此操作可使用 Thingstream 函数节点通过以下代码完成:

var id = msg.contextProperties.deviceId.split(':');  //此操作将使用 Thing 的唯一设备 ID,并分离前缀“device:”以使其与 Cumulocity 兼容

msg.clientid = id[1];  // 此操作将设置 Thingstream MQTT 发布节点使用的 msg.clientid 属性

在 Cumulocity 中注册新设备时,需要使用静态模板“100”来创建设备,并使用静态模板“110”来用客户端 ID 更新该设备。如果设备已存在于 Cumulocity 中,则此类请求将被忽略。将以下代码添加到 Thingstream 函数节点:

msg.payload="100,"+msg.contextProperties.deviceName+",MQTT\n110,"+msg.clientid // 此操作将使用 Thingstream Thing 的显示名称和在上一步中创建的客户端 ID 在 Cumulocity 中创建设备

在 Cumulocity 中向设备添加位置时,需要使用静态模板“112”,此操作可以通过 Thingstream 函数节点使用以下代码完成,其中 lat 和 lon 是您声明的来自设备的传入负载的变量:

msg.payload = "112,"+lat+","+lon;

如果愿意,您可以通过简单地将每个静态模板发布与换行符连接起来,依次进行注册、更新和设置位置任务:

msg.payload="100,"+msg.contextProperties.deviceName+",MQTT\n110,"+msg.clientid+"\n112,"+lat+","+lon;

一旦准备好有效负载,就可以将函数节点加入配置了 Cumulocity 租户凭证的 MQTT 发布节点。服务器 URL 是 mqtt.cumulocity.com,要发布的主题是 s/us。将客户端 ID 留空,以便使用您之前设置的 msg.clientid 值,然后将用户名设置为您的 Cumulocity 用户 ID,该 ID 以您的租户名称(如 mytenant/myusername)为前缀。将密码设置为用于登录 Cumulocity 的密码

立即将 Cumulocity 连接到 u-blox Thingstream

Connect Cumulocity to u-blox Thingstream today

Using Cumulocity MQTT static templates in Thingstream flows

To use the static templates in a Thingstream flow, you will need to add a function node (pictured below) to create a payload with the required format for the Cumulocity static template you wish to use.

Function node - Thingstream

MQTT publishes to Cumulocity need a unique Client ID for the device.  A good idea is to derive this from the Thingstream Client ID which would be done using a Thingstream function node with the following code:

var id = msg.contextProperties.deviceId.split(':');  //This takes the unique device ID of your thing, splitting off the prefix "device:" to make it compatible with Cumulocity

msg.clientid = id[1];  // This sets the msg.clientid property which is used by the Thingstream MQTT Publish Node

Registering a new device in Cumulocity uses the static template “100” to create the device and static template “110” to update it with the Client ID. If the device already exists in Cumulocity, these requests are simply ignored. Add the following code to the Thingstream function node:

msg.payload="100,"+msg.contextProperties.deviceName+",MQTT\n110,"+msg.clientid // This creates a device in Cumulocity using the display name of the Thingstream Thing and the client ID created in the previous step

Adding a location to the device in Cumulocity uses the static template “112” which can be done using a Thingstream function node with the following code where lat and lon are variables that you have declared from the incoming payload of your device:

msg.payload = "112,"+lat+","+lon;

If you like, you can do the register, update, and set location tasks one after the other by simply joining each static template publish with a line break:

msg.payload="100,"+msg.contextProperties.deviceName+",MQTT\n110,"+msg.clientid+"\n112,"+lat+","+lon;

Once your payload is ready, you would join the function node to an MQTT Publish Node configured with the credentials for your Cumulocity tenant. The server URL is mqtt.cumulocity.com and the topic to publish to is s/us. Leave the Client ID empty so that the msg.clientid value you set earlier is used and then set the username as your Cumulocity user ID prefixed with your tenant name e.g. mytenant/myusername. Set the password to the same password you use to login to Cumulocity

Connect Cumulocity to u-blox Thingstream today

Nick Hayes

客户运营主管 - u-blox 服务

You might also be interested in