Taking a Go at Gateway and Sub-device Development

By Yi Cheng

Image for post
Image for post

In many IoT scenarios, terminal devices themselves do not access the Internet. So, you may be wondering, then, how can data be migrated to the cloud?

Well, the IoT Platform in the cloud supports direct connections on devices using a special protocol called MQTT, which is a machine-to-machine connectivity protocol specifically designed for IoT scenarios.

Interestingly, this sort of connection protocol also supports the mounting of a device onto the gateway so as to serve as a sub-device of the gateway, which can in turn allow the gateway proxy to connect the device to the IoT Platform. Then, following this, the gateway device itself, serving as the IoT gateway device, establishes the MQTT connections to the IoT Platform to send and receive data. In addition, the device is also responsible for managing the sub-device. As such, all of this thus also includes the following operations:

  • The gateway adds the network topology relationship for the sub-device.
  • The sub-device reuses the gateway MQTT connection channel to implement the connection.
  • The gateway reports the sub-device data to the cloud.
  • The gateway receives the directive and forwards it to the sub-device.
  • The gateway reports the disconnection of the sub-device.
  • The gateway deletes the network topology relationship for the sub-device.

Also, depending on the local network, the communication protocol between the gateway and the sub-device could be HTTP, MQTT, ZigBee, Modbus, BLE, OPC-UA, among other protocols. This logic is implemented by the gateway. These connectivity features are cannot be provided, however, by the IoT SDK.

Now, let’s see how you can have a go at setting up the scenario discussed above, specifically the gateway and sub-device scenario. To do so, you’ll need to follow these steps:

Create a Network Product

To create a network product, you’ll need to select a node type-which, for this tutorial, you’ll want it to be gateway-this will serve as the direct connection means that can mount sub-devices. The gateway can manage sub-devices, maintain the topological relationships that exists with sub-devices, and synchronize these topological relationships to the cloud.

For reference, the topological relationship that exists between a gateway and its sub-devices is shown in the following figure:

Image for post
Image for post

Connect the Gateway

You can set the following parameters to connect the gateway device.

LinkKitInitParams params = new LinkKitInitParams();DeviceInfo gatewayInfo = new DeviceInfo();
gatewayInfo.productKey = gateway.productKey;
gatewayInfo.deviceName = gateway.deviceName;
gatewayInfo.deviceSecret = gateway.deviceSecret;
params.deviceInfo = gatewayInfo;
LinkKit.getInstance().init(params, ILinkKitConnectListener)

When the gateway device is connected, it is shown in the console, with the status of “Online”.

Image for post
Image for post

Add the Network Topology Relationship

You can set the following parameters to add the network topology relationship.

DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.productKey = productKey;
deviceInfo.deviceName = deviceName;
deviceInfo.deviceSecret = deviceSecret;
LinkKit.getInstance().getGateway().gatewayAddSubDevice(
deviceInfo, // the ID of the sub-device
SubDeviceConnectListener)

You can alternatively do this on the console.

Image for post
Image for post

Connect the Sub-device

You can set the following parameters to connect the sub-device.

DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.productKey = productKey;
deviceInfo.deviceName = deviceName;
deviceInfo.deviceSecret = deviceSecret;
LinkKit.getInstance().getGateway().gatewaySubDeviceLogin(
deviceInfo, // the ID of the sub-device
ISubDeviceActionListener)

You can alternatively do this on the console.

Image for post
Image for post

And you can view the connection information of the sub-device on the console.

Image for post
Image for post

Report the Sub-device Data

You can set the following parameters to report the data of the sub-device.

DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.productKey = productKey;
deviceInfo.deviceName = deviceName;
deviceInfo.deviceSecret = deviceSecret;
LinkKit.getInstance().getGateway().gatewaySubDevicePublish(
topic, // sub-device topic
data, // the data
deviceInfo, // the ID of the sub-device
ISubDeviceActionListener)

You can alternatively use the console.

Image for post
Image for post

Make the Sub-device Subscribe to Topics

You can set the following parameters to make the sub-device subscribe to topics.

DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.productKey = productKey;
deviceInfo.deviceName = deviceName;
deviceInfo.deviceSecret = deviceSecret;
LinkKit.getInstance().getGateway().gatewaySubDeviceSubscribe(
topic, // sub-device subscription topic
deviceInfo, // the ID of the sub-device
ISubDeviceActionListener)

Disconnect the Sub-device

DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.productKey = productKey;
deviceInfo.deviceName = deviceName;
deviceInfo.deviceSecret = deviceSecret;
LinkKit.getInstance().getGateway().gatewaySubDeviceLogout(
deviceInfo, // the ID of the sub-device
ISubDeviceActionListener)

Delete the Network Topology Relationship for the Sub-device

DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.productKey = productKey;
deviceInfo.deviceName = deviceName;
deviceInfo.deviceSecret = deviceSecret;
LinkKit.getInstance().getGateway().gatewayDeleteSubDevice(
deviceInfo, // the ID of the sub-device
ISubDeviceRemoveListener)

Original Source

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store