[JAVA] Connect IoT devices to the cloud using gateway and subdevice scenarios

This time, we'll look at gateway and sub-device scenarios for connecting ** IoT ** devices to the cloud.

image.png

In many IoT scenarios, the device itself does not access the Internet. So you may be wondering how to move your data to the cloud.

The cloud IoT Platform supports direct connections on devices using a special protocol called MQTT.

Interestingly, this type of connection protocol also supports mounting the device on the gateway to act as a subdevice of the gateway, allowing the gateway proxy to connect the device to the IoT platform. The gateway device itself then acts as an IoT gateway device, establishing an MQTT connection to the IoT platform to send and receive data. In addition, this device is also responsible for managing sub-devices. As such, all of this therefore also includes the following operations:

--The gateway adds a network topology relationship for the subdevices. --The subdevice reuses the gateway's MQTT connection channel to implement the connection. --The gateway reports the data of the subdevice to the cloud. --The gateway receives the command and forwards it to the subdevice. --The gateway reports the disconnection of the subdevice. --The gateway removes the subdevice's network topology relationship.

Also, depending on the local network, the communication protocols between the gateway and the subdevice are HTTP, MQTT, ZigBee. , [Modbus](http://www.bb-elec.com/Learning-Center/All-White-Papers/Modbus/The-Answer-to-the-14-Most-Frequently-Asked-Modbus.aspx?spm = a2c65.11461447.0.0.13e627e6SUARNc), [BLE](https://blog.beaconstac.com/2018/08/ble-made-simple-a-complete-guide-to-ble-bluetooth-beacons/?spm= a2c65.11461447.0.0.13e627e6SUARNc), OPC-UA. This logic is implemented by the gateway. However, these connectivity features cannot be provided by the IoT SDK.

Now let's look at how to set up the scenario described above, specifically the gateway and subdevice scenarios. To do this, you need to follow the steps below.

Creating network products

To create a network product, you need to select a node type (referred to as a gateway in this tutorial). The gateway can manage the subdevices, maintain the topological relationships that exist on the subdevices, and synchronize these topological relationships to the cloud.

image.png

For reference, the figure below shows the topological relationships that exist between the gateway and its subdevices.

image.png

Connect gateway

You can connect the gateway device by setting the following parameters.

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 will be displayed in the console with an "Online" status.

image.png

Add network topology

You can add a network topology by setting the following parameters:

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 do it in the console instead.

image.png

Connect sub-devices

You can connect sub-devices by setting the following parameters.

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 do it in the console instead.

image.png

Then, you can check the connection information of the sub device on the console.

image.png

Report subdevice data

You can report data on subdevices by setting the following parameters.

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 use the console instead.

image.png

Have subdevices subscribe to topics

You can have sub-devices subscribe to topics by setting the following parameters:

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 subdevice

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)

Deletion of subdevice network topology

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)

Recommended Posts

Connect IoT devices to the cloud using gateway and subdevice scenarios
Deploy your application to EDAS using the Cloud Toolkit Maven plugin
Dynamically switch the database to connect to
How to connect Heroku and Sequel
I summarized the points to note when using resources and resources in combination
[Android] How to turn the Notification panel on and off using StatusBarManager
Deploy a Node.js application to an ECS instance using the Cloud Toolkit
Send a request to the backend after authenticating with Spring Cloud Gateway