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\n");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\n");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\n");
}

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\n");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\n");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 \n");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("\n");// topic_info->payload为接收到的数据if(strcmp(topic_info->payload,"1") == 0){cm_printf("开灯\n");cm_gpio_write(21,CM_GPIO_LOW);}else{cm_printf("关灯\n");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)})},}

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

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

    B站:https://space.bilibili.com/309103931 中移4G模块-ML302专栏:https://blog.csdn.net/qq_33259323/category_10 ...

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

    B站:https://space.bilibili.com/309103931 中移4G模块-ML302专栏:https://blog.csdn.net/qq_33259323/category_10 ...

  3. 带你开发一个远程控制项目---->STM32+标准库+阿里云平台+传感器模块+远程显示-------之 MQTT连接阿里云平台

    目录 第一篇: 第二篇: 项目清单 视频验证效果 Android Studio开发介绍 步1:此次需要下载本人开发的MQTT阿里云连接项目 步2:替换阿里云 设备三元信息 查看三元 替换 Androi ...

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

    B站:https://space.bilibili.com/309103931 中移4G模块-ML302专栏:https://blog.csdn.net/qq_33259323/category_10 ...

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

    B站:https://space.bilibili.com/309103931 中移4G模块-ML302专栏:https://blog.csdn.net/qq_33259323/category_10 ...

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

    B站:https://space.bilibili.com/309103931 中移4G模块-ML302专栏:https://blog.csdn.net/qq_33259323/category_10 ...

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

    B站:https://space.bilibili.com/309103931 中移4G模块-ML302专栏:https://blog.csdn.net/qq_33259323/category_10 ...

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

    B站:https://space.bilibili.com/309103931 中移4G模块-ML302专栏:https://blog.csdn.net/qq_33259323/category_10 ...

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

    B站:https://space.bilibili.com/309103931 中移4G模块-ML302专栏:https://blog.csdn.net/qq_33259323/category_10 ...

最新文章

  1. Decision stump、Bootstraping、bagging、boosting、Random Forest、Gradient Boosting
  2. laravel5.8笔记八:数据库(单库和多库)
  3. python 如何放心干净的卸载模块
  4. linux设置永久别名
  5. vb.net读取excel并写入dgv_读取PDF中的表格写入EXCEL?30行代码搞定
  6. QPS 提升60%,揭秘阿里巴巴轻量级开源 Web 服务器 Tengine 负载均衡算法
  7. 15. 二进制中1的个数
  8. 【BAT】中文数字to阿拉伯数字转换
  9. Floyd 多源最短路径
  10. WebCollector 网页正文快速提取
  11. centos7 快速安装 mariadb(mysql)
  12. IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统
  13. 如何去掉华三交换日志中机烦人的 STP_NOTIFIED_TC
  14. “寒江独钓”错误列表
  15. 年礼成快递企业不再接件主因:苹果产品最疯狂
  16. dg怎么了(最近dg怎么了)
  17. 智能生活 App SDK 如何实现设备控制
  18. 怎样把计算机扫描放到桌面,电脑扫描文件怎么弄【面对方法】
  19. 【教学类-15-03】20221119《姓名描字帖-A4竖版(1*7笔画复杂大字)》(大班适合中层次幼儿)
  20. Flask项目之手机端租房网站的实战开发(九)

热门文章

  1. Eigen官网教程(2) Array类和元素级操作
  2. 用三元操作符替代if-else以降低CPU分支预测惩罚实现Unity内函数13倍提速
  3. iis php多版本共存,IIS7中多个版本php共存的方法
  4. 直接选择排序与冒泡排序
  5. Apple发布watchOS 5 Beta 9
  6. Spring详解(四)------注解配置IOC、DI
  7. MYSQ--SHOW PROFILE Syntax and useing
  8. POJ 2449 Remmarguts' Date
  9. 十年编程经验凝结 与新人们分享
  10. java 非法线程_JVM中的线程行为