中移4G模块-ML302-OpenCpu开发-(MQTT连接阿里云-订阅主题)

B站:https://space.bilibili.com/309103931

中移4G模块-ML302专栏:https://blog.csdn.net/qq_33259323/category_10453372.html

中移4G模块-ML302文集:https://www.bilibili.com/read/readlist/rl328642

1.中移4G模块-ML302-OpenCpu开发-(固件编译和烧录)

https://blog.csdn.net/qq_33259323/article/details/108586847

https://www.bilibili.com/read/cv7876504

2.中移4G模块-ML302-OpenCpu开发-(MQTT连接阿里云)

https://blog.csdn.net/qq_33259323/article/details/108638945

https://www.bilibili.com/read/cv7876527

2.1中移4G模块-ML302-OpenCpu开发-(MQTT连接阿里云-订阅主题)

https://blog.csdn.net/qq_33259323/article/details/108960540

https://www.bilibili.com/read/cv7879954

2.2中移4G模块-ML302-OpenCpu开发-(MQTT连接阿里云-接收和发送数据)

https://blog.csdn.net/qq_33259323/article/details/108964810

https://www.bilibili.com/read/cv7886836

2.3中移4G模块-ML302-OpenCpu开发-(MQTT连接阿里云-RRPC通讯)

https://blog.csdn.net/qq_33259323/article/details/108965071

https://www.bilibili.com/read/cv7888259

3.中移4G模块-ML302-OpenCpu开发-串口开发

https://blog.csdn.net/qq_33259323/article/details/108974888

https://www.bilibili.com/read/cv7888865

4.中移4G模块-ML302-OpenCpu开发-51单片机串口转I2C

https://blog.csdn.net/qq_33259323/article/details/109020642

https://www.bilibili.com/read/cv7922942

5.中移4G模块-ML302-OpenCpu开发-MCP23017输入/输出

https://blog.csdn.net/qq_33259323/article/details/109109136

https://www.bilibili.com/read/cv7969395

7.中移4G模块-ML302-OpenCpu开发-PCF8591测量电压

https://blog.csdn.net/qq_33259323/article/details/109109266

https://www.bilibili.com/read/cv7969365

8.中移4G模块-ML302-OpenCpu开发-GPIO

https://blog.csdn.net/qq_33259323/article/details/108960947

https://www.bilibili.com/read/cv7877192

9.中移4G模块-ML302-OpenCpu开发-ADC

https://blog.csdn.net/qq_33259323/article/details/109020864

https://www.bilibili.com/read/cv7922958

10.中移4G模块-ML302-OpenCpu开发-CJSON

https://blog.csdn.net/qq_33259323/article/details/109020898

https://www.bilibili.com/read/cv7923020

11.中移4G模块-ML302-OpenCpu开发-HTTP

https://blog.csdn.net/qq_33259323/article/details/109020904

https://www.bilibili.com/read/cv7923054

中移4G模块-ML302-OpenCpu开发-(MQTT连接阿里云-订阅主题)

模块OpenCpu部分

cm_main.c文件里面的cm_main_task是主函数,在主函数里面调用cm_test_aliyun函数。

cm_test_aliyun函数:

void cm_test_aliyun(){
    cm_printf("[ALIYUN]: aliyun demo start
");

    void                   *pclient = NULL;
    int                     res = 0;
    int                     rpc_res = 0;
    int                     loop_cnt = 0;
    iotx_mqtt_param_t       mqtt_params;

    HAL_GetProductKey(DEMO_PRODUCT_KEY);
    HAL_GetDeviceName(DEMO_DEVICE_NAME);
    HAL_GetDeviceSecret(DEMO_DEVICE_SECRET);

    memset(&mqtt_params, 0x0, sizeof(mqtt_params));

    mqtt_params.handle_event.h_fp = example_event_handle;

    pclient = IOT_MQTT_Construct(&mqtt_params);
    if (NULL == pclient) {
        cm_printf("[ALIYUN]: MQTT construct failed
");
        return -1;
    }

    res = example_subscribe(pclient);      //调用example_subscribe函数
    if (res < 0) {
        IOT_MQTT_Destroy(&pclient);
        return -1;
    }

    while (1) {
        if (0 == loop_cnt % 20) {
            example_publish(pclient);
        }

        IOT_MQTT_Yield(pclient, 200);

        loop_cnt += 1;

        if(loop_cnt >= 100)  {
            //break;
        }
    }
    
    cm_printf("[ALIYUN]: aliyun demo end
");
}

example_subscribe函数:

example_subscribe一开始进行字符串连接,把${YourProductKey}/${YourDeviceName}/user/get拼接出来,然后调用IOT_MQTT_Subscribe函数订阅topic

int example_subscribe(void *handle){
    int res = 0;
    const char *fmt = "/%s/%s/user/get";    //订阅的MQTT路径
    char *topic = NULL;
    int topic_len = 0;

    topic_len = strlen(fmt) + strlen(DEMO_PRODUCT_KEY) + strlen(DEMO_DEVICE_NAME) + 1;
    topic = HAL_Malloc(topic_len);
    if (topic == NULL) {
        cm_printf("[ALIYUN]: memory not enough
");
        return -1;
    }
    memset(topic, 0, topic_len);
    HAL_Snprintf(topic, topic_len, fmt, DEMO_PRODUCT_KEY, DEMO_DEVICE_NAME);

    //通过HAL_Snprintf函数的拼接得出最终的MQTT路径
    //示例:${YourProductKey}/${YourDeviceName}/user/get
    //其中的:example_message_arrive函数为接收的数据的回调函数    

    res = IOT_MQTT_Subscribe(handle, topic, IOTX_MQTT_QOS0, example_message_arrive, NULL);
    if (res < 0) {
        cm_printf("[ALIYUN]: subscribe failed
");
        HAL_Free(topic);
        return -1;
    }

    HAL_Free(topic);
    return 0;
}

example_message_arrive函数

example_subscribe为topic接收回调函数,当接收到此topic的数据时会放到这里处理

void example_message_arrive(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg){
    iotx_mqtt_topic_info_t     *topic_info = (iotx_mqtt_topic_info_pt) msg->msg;

    cm_printf("example_message_arrive 
");

    switch (msg->event_type) {
        case IOTX_MQTT_EVENT_PUBLISH_RECEIVED:
            /* print topic name and topic message */
            cm_printf("[ALIYUN]: Message Arrived:");
            cm_printf("Topic  : %.*s", topic_info->topic_len, topic_info->ptopic);
            cm_printf("Payload: %.*s", topic_info->payload_len, topic_info->payload);
            cm_printf("
");

            // topic_info->payload为接收到的数据

            if(strcmp(topic_info->payload,"1") == 0){
                cm_printf("开灯
");
                cm_gpio_write(21,CM_GPIO_LOW);
            }else{
                cm_printf("关灯
");
                cm_gpio_write(21,CM_GPIO_HIGH);
            }



            break;
        default:
            break;
    }

}

服务器部分

前端通过传入lightState来控制GPIO21是高电平还是低电平

    @GetMapping(path="hello")
    public WebResult setGPIOState(int lightState){

        // XXXXXX:ProductKey
        // YYYYYY: 设备名称

        PubRequest request = new PubRequest();
        request.setProductKey("XXXXXXXXXXXX");
          request.setMessageContent(Base64.encodeBase64String((Integer.toString(lightState)).getBytes()));
        request.setTopicFullName("/XXXXXXXXXX/YYYYYYYYYYY/user/get");
        request.setQos(0); //目前支持QoS0和QoS1。
        try {
            PubResponse response = defaultAcsClient.getAcsResponse(request);
            //System.out.println(response.getSuccess());
            //System.out.println(response.getErrorMessage());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }


        return WebResult.success(1,"hello");
    }

前端网页部分

            <div class="my-2 my-tab">
                <v-btn small @click="setLightState(1)">灯光开</v-btn>
                <v-btn small style="margin-left:10px;" @click="setLightState(0)">灯光关</v-btn>
            </div>
    methods:{
        setLightState(state){
            console.log(state);
            // 开关灯
            axios({
                method: 'get',
                url: "/iot/hello",
                params: {
                    'lightState':state
                }
            }).then(res => {
                console.log(res)
            })
        },
    }
原文地址:https://www.cnblogs.com/kawayidamiao/p/13843727.html