Step By Step
- OpenAPI
- 云端SDK
一.OpenAPI
二.云端SDK(本文以Java为例)
1.添加pom依赖
<!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-iot --> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-iot</artifactId> <version>7.31.0</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.5.6</version> </dependency>
2.发起调用
String accessKey = "${accessKey}"; String accessSecret = "${accessSecret}"; IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKey, accessSecret); DefaultAcsClient client = new DefaultAcsClient(profile); PubRequest request = new PubRequest(); request.setIotInstanceId("${iotInstanceId}"); request.setProductKey("${productKey}"); request.setMessageContent(Base64.encodeBase64String("hello world".getBytes())); request.setTopicFullName("/${productKey}/${deviceName}/user/get"); request.setQos(0); //目前支持QoS0和QoS1。 try { PubResponse response = client.getAcsResponse(request); System.out.println(response.getSuccess()); System.out.println(response.getCode()); System.out.println(response.getErrorMessage()); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { System.out.println("ErrCode:" + e.getErrCode()); System.out.println("ErrMsg:" + e.getErrMsg()); e.printStackTrace(); }