After you configure the device SDK, connect the gateway device to IoT Platform.
Prerequisites
Before you start, make sure that the following operations are performed:
Configure the device SDK for the gateway device
In the SDK package, the DeviceTopoManager file in the java/src/main/java/com/aliyun/iot/api/common/deviceApi directory contains sample code for connecting the gateway device to IoT Platform.
- Configure the information about the gateway device for connection.
private static String regionId = "cn-shanghai"; private static final String TAG = "TOPO"; // The information about the gateway device. private static String GWproductKey = "a1BxptK***"; private static String GWdeviceName = "XMtrv3yvftEHAzrTfX1U"; private static String GWdeviceSecret = "19xJNybifnmgcK057vYhazYK4b64****"; public static void main(String[] args) { /** * The information about the MQTT connection. */ DeviceTopoManager manager = new DeviceTopoManager(); /** * The Java HTTP client of this device supports TSLv1.2. */ System.setProperty("https.protocols", "TLSv2"); manager.init(); }
- Establish a connection.
public void init() { LinkKitInitParams params = new LinkKitInitParams(); /** * Specify the parameters for MQTT initialization. */ IoTMqttClientConfig config = new IoTMqttClientConfig(); config.productKey = GWproductKey; config.deviceName = GWdeviceName; config.deviceSecret = GWdeviceSecret; config.channelHost = GWproductKey + ".iot-as-mqtt." + regionId + ".aliyuncs.com:1883"; /** * Specify whether to receive offline messages. * The cleanSession field that corresponds to the MQTT connection. */ config.receiveOfflineMsg = false; params.mqttClientConfig = config; ALog.setLevel(LEVEL_DEBUG); ALog.i(TAG, "mqtt connetcion info=" + params); /** * Configure the initialization and pass in the certificate information about the device. */ DeviceInfo deviceInfo = new DeviceInfo(); deviceInfo.productKey = GWproductKey; deviceInfo.deviceName = GWdeviceName; deviceInfo.deviceSecret = GWdeviceSecret; params.deviceInfo = deviceInfo; /**Establish a connection. **/ LinkKit.getInstance().init(params, new ILinkKitConnectListener() { public void onError(AError aError) { ALog.e(TAG, "Init Error error=" + aError); } public void onInitDone(InitResult initResult) { ALog.i(TAG, "onInitDone result=" + initResult); // Obtain the topological relationships of the gateway and check whether a topological relationship exists between the gateway and the sub-device. // If a topological relationship exists between the gateway and the sub-device, the gateway connects the sub-device to IoT Platform. getGWDeviceTopo(); // Dynamically register the sub-device to obtain a DeviceSecret for the sub-device. If the gateway has obtained the certificate of the sub-device, skip this step. // When you register the sub-device, set the deviceName parameter to the serial number or MAC address of the sub-device. gatewaySubDevicRegister(); //Specify the information about the sub-device for which you want to add a topological relationship. gatewayAddSubDevice(); } }); }
Test the connection
After you configure the information about the gateway device in the device SDK, you can test the connection between the gateway device and IoT Platform.
- Run DeviceTopoManager.
- Log on to the IoT Platform console.In the left-side navigation pane, choose .
- On the Devices page, find the gateway device and check its status. If the status of the gateway device is Online, the gateway device is connected to IoT Platform.