×
Community Blog Taking a Go at Gateway and Sub-device Development

Taking a Go at Gateway and Sub-device Development

In this article, we will be taking a look at how you can use a Gateway and Sub-device scenario for connecting IoT devices to the cloud.

By Yi Cheng

1

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.

2

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

3

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".

4

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.

5

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.

6

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

7

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.

8

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)
0 0 0
Share on

GXIC

24 posts | 5 followers

You may also like

Comments