OpenHarmony开发10 —— 复现D5 MQTT协议开发
参考文档:applications/BearPi/BearPi-HM_Nano/sample/D5_iot_mqtt/README.md · 小熊派开源社区/BearPi-HM_Nano - Gitee.com
-
今天在配置EMQX的时候就出现了极大的问题,发现emqx启动之后localhost进不去,于是先是重装了windows的lls,之后又打开hosts文件,发现docker在上面进行了影响,
于是卸载docker,之后重新写了hosts文件:127.0.0.1 localhost
,之后卸载emqx,重新装了个新的,这才解决问题 -
进行手机端mqtt测试,没问题,电脑能成功接收
-
修改
sample/build.gn
# Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import("//build/lite/config/component/lite_component.gni") lite_component("app") { features = [ #"A1_kernal_thread:thread_example", #"A2_kernel_timer:timer_example", #"A3_kernel_event:event_example", #"A4_kernel_mutex:mutex_example", #"A5_kernel_semaphore:semaphore_example", #"A6_kernel_message:message_example", #"B1_basic_led_blink:led_example", #"B2_basic_button:button_example", #"B3_basic_pwm_led:pwm_example", #"B4_basic_adc:adc_example", #"B5_basic_i2c_nfc:i2c_example", #"B6_basic_uart:uart_example", #"C1_e53_sf1_mq2:e53_sf1_example", #"C2_e53_ia1_temp_humi_pls:e53_ia1_example", #"C3_e53_sc1_pls:e53_sc1_example", #"C4_e53_sc2_axis:e53_sc2_example", #"C5_e53_is1_infrared:e53_is1_example", #"D1_iot_wifi_ap:wifi_ap", #"D2_iot_wifi_sta_connect:wifi_sta_connect", #"D3_iot_udp_client:udp_client", #"D4_iot_tcp_server:tcp_server", "D5_iot_mqtt:iot_mqtt", #"D6_iot_cloud_oc:oc_mqtt", #"D7_iot_cloud_onenet:onenet_mqtt", #"D8_iot_cloud_oc_smoke:cloud_oc_smoke", #"D9_iot_cloud_oc_light:cloud_oc_light", #"D10_iot_cloud_oc_manhole_cover:cloud_oc_manhole_cover", #"D11_iot_cloud_oc_infrared:cloud_oc_infrared", #"D12_iot_cloud_oc_agriculture:cloud_oc_agriculture", #"D13_iot_cloud_oc_gps:cloud_oc_gps", ] }
-
修改
iot_mqtt.c
中MQTT_DemoTask
-
添加宏定义,代码更规范
#include <stdio.h> #include <string.h> #include <stdlib.h> #include "ohos_init.h" #include "cmsis_os2.h" #include "wifi_connect.h" #include "MQTTClient.h" #define CONNECT_SSID "" #define CONNECT_PASSWORD "" #define CONNECT_IP "" #define CONNECT_PORT 1883
-
修改
MQTT_DemoTask
函数static void MQTT_DemoTask(void) { WifiConnect(CONNECT_SSID,CONNECT_PASSWORD); printf("Starting ...\n"); int rc, count = 0; MQTTClient client; NetworkInit(&network); printf("NetworkConnect ...\n"); begin: NetworkConnect(&network, CONNECT_IP, CONNECT_PORT); printf("MQTTClientInit ...\n"); MQTTClientInit(&client, &network, 2000, sendBuf, sizeof(sendBuf), readBuf, sizeof(readBuf)); MQTTString clientId = MQTTString_initializer; clientId.cstring = "bearpi"; MQTTPacket_connectData data = MQTTPacket_connectData_initializer; data.clientID = clientId; data.willFlag = 0; data.MQTTVersion = 3; data.keepAliveInterval = 0; data.cleansession = 1; printf("MQTTConnect ...\n"); rc = MQTTConnect(&client, &data); if (rc != 0) { printf("MQTTConnect: %d\n", rc); NetworkDisconnect(&network); MQTTDisconnect(&client); osDelay(200); goto begin; } printf("MQTTSubscribe ...\n"); rc = MQTTSubscribe(&client, "substopic", 2, messageArrived); if (rc != 0) { printf("MQTTSubscribe: %d\n", rc); osDelay(200); goto begin; } while (++count) { MQTTMessage message; char payload[30]; message.qos = 2; message.retained = 0; message.payload = payload; sprintf(payload, "message number %d", count); message.payloadlen = strlen(payload); if ((rc = MQTTPublish(&client, "pubtopic", &message)) != 0){ printf("Return code from MQTT publish is %d\n", rc); NetworkDisconnect(&network); MQTTDisconnect(&client); goto begin; } osDelay(50); } }
-
putty
hpm dist
-
经典取出
app_allinone
-
烧录,别忘了选择auto burn哦
-
打开HyperTerminal,监听端口
-
使用mqttx向
substopic
发送时,可以成功收到message
-