This topic describes how to configure a device SDK to connect a gateway to IoT Platform.

Prerequisites

The following operations are complete:

Configure the device SDK for the gateway

In this example, the DeviceTopoManager file in the java/src/main/java/com/aliyun/iot/api/common/deviceApi directory contains sample code that is used to connect the gateway to IoT Platform.

  1. Specify the certificate information for the gateway by using the GWproductKey, GWdeviceName, and GWdeviceSecret parameters.

    You can view the value of this parameter on the Device Details page in the IoT Platform console.

        private static String regionId = "cn-shanghai";
        private static final String TAG = "TOPO";
    
        // The certificate information about the gateway.
        private static String GWproductKey = "a1Bxp*********";
        private static String GWdeviceName = "XMtrv3y*************";
        private static String GWdeviceSecret = "19xJNybifnmgc*************";
    
    
        public static void main(String[] args) {
            /**
             * The information about the MQTT connection.
             */
            DeviceTopoManager manager = new DeviceTopoManager();
    
            /**
             * The Java HTTP client on the server supports TSLv1.2. 
             */
            System.setProperty("https.protocols", "TLSv2");
    
            manager.init();
        }
  2. Configure the MQTT endpoint by using the channelHost parameter to establish an MQTT connection.

    For more information about the MQTT endpoint, see View the endpoint of an instance.

        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 connection 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);
    
                    //Query the topological relationships of the gateway, and check whether a topological relationship exists between the gateway and the specified sub-device.
                    //Connect the sub-device to IoT Platform if the topological relationship exists.
                    getGWDeviceTopo();
    
                    //Dynamically register the sub-device to obtain the DeviceSecret of the sub-device.If the gateway obtained the certificate of the sub-device, skip this step.
                    //When you create the sub-device in IoT Platform, set the DeviceName parameter to the serial number or MAC address of the sub-device.
                    gatewaySubDevicRegister();
    
                    //The information about the sub-device in the topological relationship.
                    gatewayAddSubDevice();
    
    
                }
            });
        }

Test the connection

After you configure the information about the gateway in the device SDK, you can test the connection between the gateway and IoT Platform.

  1. Run the DeviceTopoManager.java file.
  2. Log on to the IoT Platform console, click the instance that you want to manage, and then choose Devices > Devices in the left-side navigation pane.
  3. On the Devices page, find the gateway that you connected to IoT Platform and check the status of the gateway. If the status of the gateway is Online, the gateway is connected to IoT Platform.

What to do next

Connect the sub-device to IoT Platform.