Cette rubrique explique comment configurer un SDK de périphérique pour connecter une passerelle à IoT Platform.
Prérequis
Assurez-vous d'avoir effectué les opérations suivantes :
Créer une passerelle et un sous-périphérique : Créez une passerelle et un sous-périphérique dans IoT Platform et récupérez les informations de certificat du sous-périphérique.
Initialiser les SDK : Importez le package SDK contenant l'exemple de code.
Configurer le SDK de périphérique pour la passerelle
Dans cet exemple, le fichier DeviceTopoManager situé dans le répertoire java/src/main/java/com/aliyun/iot/api/common/deviceApi contient l'exemple de code permettant de connecter la passerelle à IoT Platform.
-
Spécifiez les informations de certificat de la passerelle à l'aide des paramètres GWproductKey, GWdeviceName et GWdeviceSecret.
Consultez la valeur de ce paramètre sur la page Device Details de la console IoT Platform.
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(); } -
Configurez l'endpoint MQTT à l'aide du paramètre channelHost pour établir une connexion MQTT.
Pour plus d'informations sur l'endpoint MQTT, consultez la rubrique Gérer les endpoints d'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(); } }); }
Tester la connexion
Une fois les informations de la passerelle configurées dans le SDK de périphérique, testez la connexion entre la passerelle et IoT Platform.
Exécutez le fichier DeviceTopoManager.java.
Connectez-vous à la console IoT Platform, cliquez sur l'instance à gérer, puis choisissez dans le volet de navigation de gauche.
Sur la page Devices, localisez la passerelle connectée à IoT Platform et vérifiez son statut. Si le statut de la passerelle est Online, la passerelle est connectée à IoT Platform.
Étapes suivantes
Connecter un sous-périphérique à IoT Platform
.