The server connects to alicloud Iot platform to issue instructions

Keywords: IoT server Alibaba Cloud ESP8266 NodeMCU

brief introduction

This routine issues the instruction for NodeMCU-ESP8266 to connect to alicloud Iot platform. In fact, it publishes messages for the specified device Topic to achieve the effect of control. Specifically, it publishes messages to the Topic of product / product details / Topic class list / object model communication / sys/gmvzwtDHyC6/${deviceName}/thing/service/property/set. As long as you fill in the necessary parameters in Aliyun's API, it will simulate a client publishing data to the topics of the Iot server platform.

Friendly tips:

  • The author's work is only for the integration of, so this blog only shows the author's successful debugging process for reference only.
  • I recommend your reference Official documents The author simply summarizes the steps of official document development.

Development environment preparation

The author uses java environment for server development, so these environments need to be configured before development

  • JDK1.8
  • idea-pro

Get started quickly

Note: firstly, the author defaults that you have created a public instance of AliyunIot, successfully created product and public instances, and NodeMCU has successfully connected to the cloud, so you can display data on the Iot platform. If you haven't done this yet, please NodeMCU-ESP8266 is connected to alicloud Iot platform for data monitoring The method in.

Create a new Maven project

This step is not illustrated in detail. You can create an empty Maven project on Idea. If you don't create it, you can consult the data yourself.

Import Maven dependencies

Import dependencies in pow.xml

<dependencies>
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>iot20180120</artifactId>
      <version>1.4.0</version>
    </dependency>
</dependencies>

Create instance run

Create a program, fill in the necessary information and run the following program.

// This file is auto-generated, don't edit it. Thanks.
package com.xxx;

import com.aliyun.tea.*;
import com.aliyun.iot20180120.*;
import com.aliyun.iot20180120.models.*;
import com.aliyun.teaopenapi.*;
import com.aliyun.teaopenapi.models.*;
import com.aliyun.teaconsole.*;
import com.aliyun.teautil.*;
import com.zeeland.pojo.DeviceInfo;
import com.zeeland.pojo.KeyInfo;

public class Pub {

    /**
     * Initialize the Client account with AK & sk
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.iot20180120.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // Your AccessKey ID
                .setAccessKeyId(accessKeyId)
                // Your AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // Domain name accessed
        config.endpoint = "iot.cn-shanghai.aliyuncs.com";
        return new com.aliyun.iot20180120.Client(config);
    }

    public static void main(String[] args_) throws Exception {
        java.util.List<String> args = java.util.Arrays.asList(args_);

        com.aliyun.iot20180120.Client client = Pub.createClient(KeyInfo.accessKeyId, KeyInfo.accesssKeySecret);
        //Parameter configuration
        PubRequest pubRequest = new PubRequest()
                .setIotInstanceId(KeyInfo.instanceId)
                .setProductKey(KeyInfo.NodeMCUProductId)
                .setQos(0)
                .setTopicFullName(DeviceInfo.topicSet)
                .setMessageContent("eyJwYXJhbXMiOnsib3BlbkRvb3IiOjF9fQ==");//base64 encoded results
        PubResponse resp = client.pub(pubRequest);
        com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(resp)));
    }
}

If you can't understand the above code, you can use online debugging. First debug it online, as shown in the figure below. First fill in the data to be filled in the middle column, and then click initiate call to see the results after operation.

matters needing attention:

  • The maximum number of requests per second (QPS) that a single alicloud account calls the interface is 1600

Posted by nareshrevoori on Sat, 04 Dec 2021 11:41:50 -0800