【XR806开发板试用】 基于MQTT远程温度采集+灯光控制
-
第一步:由于ROM的空间不够,先要将控制台程序移出编译
- 将源码中的device\xradio\xr806\BUILD.gn文件中44行的"adapter/console:app_console"注释掉
if (IsBootloader == "false") { deps += [ "adapter/hals:adapter", #"adapter/console:app_console", "ohosdemo:ohosdemo" ] }
- 否则编译时可能会出现如下报错信息
[OHOS ERROR] /opt/gcc-arm-none-eabi-10-2020-q4-major/bin/arm-none-eabi-objcopy -O binary -R .xip audio_demo.elf audio_demo.bin [OHOS ERROR] /opt/gcc-arm-none-eabi-10-2020-q4-major/bin/arm-none-eabi-size audio_demo.elf [OHOS ERROR] text data bss dec hex filename [OHOS ERROR] 1155588 8032 76280 1239900 12eb5c audio_demo.elf [OHOS ERROR] /opt/gcc-arm-none-eabi-10-2020-q4-major/bin/arm-none-eabi-objcopy -O binary -j .xip audio_demo.elf audio_demo_xip.bin [OHOS ERROR] cp audio_demo_xip.bin ../image/"xr806"/app_xip.bin [OHOS ERROR] cd ../image/"xr806" && \ [OHOS ERROR] chmod a+r *.bin && \ [OHOS ERROR] /opt/gcc-arm-none-eabi-10-2020-q4-major/bin/arm-none-eabi-gcc -E -P -CC -DCONFIG_CHIP_ARCH_VER=3 -DCONFIG_ARCH_APP_CORE -DCONFIG_CPU_CM33F -DPRJ_XIP_SIZE=8M -DPRJ_BOOT_CERT="\"null\"" -DPRJ_IMAGE_BOOT_ATTR="\"0x1\"" -DPRJ_IMAGE_APP_SRAM_OFFS="\"0x00201000\"" -DPRJ_IMAGE_APP_EP="\"0x00201101\"" -DPRJ_IMAGE_BOOT_SRAM_OFFS="\"0x00230000\"" -DPRJ_IMAGE_BOOT_EP="\"0x00230101\"" -DPRJ_IMAGE_TZ_ATTR="\"0x25\"" -DPRJ_IMAGE_TZ_XIP_ATTR="\"0x26\"" -DPRJ_IMAGE_TZ_PSRAM_ATTR="\"0x25\"" -DPRJ_APP_BIN_CERT="\"null\"" -DPRJ_APP_XIP_BIN_CERT="\"null\"" -DPRJ_APP_PSRAM_BIN_CERT="\"null\"" -DPRJ_IMAGE_APP_ATTR="\"0x1\"" -DPRJ_IMAGE_APP_XIP_ATTR="\"0x2\"" -DPRJ_IMAGE_APP_PSRAM_ATTR="\"0x1\"" -DCONFIG_RAM_START=0x00201000 -DPRJ_RAM_SIZE=300K -DPRJ_PSRAM_START_OFFS="\"0x01400000\"" -DCONFIG_PSRAM_START=0x01400000 -DPRJ_PSRAM_SIZE=0K -DPRJ_IMAGE_BOOT_BIN="\"boot_"40"M.bin\"" -DPRJ_IMAGE_SYS_SDD_BIN="\"sys_sdd_"40"M.bin\"" -I../../../../../include/generated -include autoconf.h -o .image.cfg - < image.cfg && \ [OHOS ERROR] true && \ [OHOS ERROR] chmod 777 ../../../../../tools/mkimage && ../../../../../tools/mkimage -c .image.cfg -o xr_system.img [OHOS ERROR] err: bin 1 and bin 2 were overlaped! [OHOS ERROR] Overlapped size: 4096 Byte(4kB) [OHOS ERROR] bin 1 name:app.bin begin: 0x00008000 end: 0x00018C00 [OHOS ERROR] bin 2 name:app_xip.bin begin: 0x00017C00 [OHOS ERROR] [OHOS ERROR] We've rearranged bin files and generated new cfg file 'image_auto_cal.cfg', the new one is recommended. [OHOS ERROR] Generate image file failed [OHOS ERROR] cfg string: [OHOS ERROR] /* [OHOS ERROR] * [OHOS ERROR] * Automatically generated file; DO NOT EDIT. [OHOS ERROR] * XR806 SDK Configuration [OHOS ERROR] * [OHOS ERROR] */ [OHOS ERROR] { [OHOS ERROR] "magic" : "AWIH", [OHOS ERROR] "version" : "0.5", [OHOS ERROR] "image" : {"max_size": "1532K"}, [OHOS ERROR] "section" :[ [OHOS ERROR] {"id": "0xa5ff5a00", "bin" :"boot_40M.bin", "cert": "null", "flash_offs": "0K", "sram_offs": "0x00230000", "ep": "0x00230101", "attr":"0x1"}, [OHOS ERROR] {"id": "0xa5fe5a01", "bin" :"app.bin", "cert": "null", "flash_offs": "32K", "sram_offs": "0x00201000", "ep": "0x00201101", "attr":"0x1"}, [OHOS ERROR] {"id": "0xa5fd5a02", "bin" :"app_xip.bin", "cert": "null", "flash_offs": "95K", "sram_offs": "0xffffffff", "ep": "0xffffffff", "attr":"0x2"}, [OHOS ERROR] {"id": "0xa5fa5a05", "bin" :"wlan_bl.bin", "cert": "null", "flash_offs": "1166K", "sram_offs": "0xffffffff", "ep": "0xffffffff", "attr":"0x1"}, [OHOS ERROR] {"id": "0xa5f95a06", "bin" :"wlan_fw.bin", "cert": "null", "flash_offs": "1169K", "sram_offs": "0xffffffff", "ep": "0xffffffff", "attr":"0x1"}, [OHOS ERROR] {"id": "0xa5f85a07", "bin" :"sys_sdd_40M.bin", "cert": "null", "flash_offs": "1194K", "sram_offs": "0xffffffff", "ep": "0xffffffff", "attr":"0x1"}, [OHOS ERROR] {} [OHOS ERROR] ] [OHOS ERROR] } [OHOS ERROR] [OHOS ERROR] ../../../../project/project.mk:519: recipe for target 'image' failed [OHOS ERROR] make[2]: *** [image] Error 255 [OHOS ERROR] make[2]: Leaving directory '/home/qq1253176313/ubuntu_share/samba/openharmony/device/xradio/xr806/xr_skylark/project/demo/audio_demo/gcc' [OHOS ERROR] ../../../../project/project.mk:493: recipe for target '__build' failed [OHOS ERROR] make[1]: *** [__build] Error 2 [OHOS ERROR] make[1]: Leaving directory '/home/qq1253176313/ubuntu_share/samba/openharmony/device/xradio/xr806/xr_skylark/project/demo/audio_demo/gcc' [OHOS ERROR] Makefile:164: recipe for target 'build' failed [OHOS ERROR] make: *** [build] Error 2 [OHOS ERROR] you can check build log in /home/qq1253176313/ubuntu_share/samba/openharmony/out/xr806/wifi_skylark/build.log [OHOS ERROR] command: "/home/qq1253176313/ubuntu_share/samba/openharmony/prebuilts/build-tools/linux-x86/bin/ninja -w dupbuild=warn -C /home/qq1253176313/ubuntu_share/samba/openharmony/out/xr806/wifi_skylark" failed [OHOS ERROR] return code: 1 [OHOS ERROR] execution path: /home/qq1253176313/ubuntu_share/samba/openharmony
第二步 设置device\xradio\xr806\ohosdemo\BUILD.gn文件
- 新增一行"mqtt:app_mqtttest"
group("ohosdemo") { deps = [ "mqtt:app_mqtttest", #"LED:app_led", #"hello_demo:app_hello", #"iot_peripheral:app_peripheral", #"wlan_demo:app_WlanTest", ] }
第三步 编写源码
在device\xradio\xr806\ohosdemo\mqtt路径下创建BUILD.gn、ds18b20.c、ds18b20.h、main.c四个文件
- device\xradio\xr806\ohosdemo\mqtt\ds18b20.c
#include "ds18b20.h" ////////////////////////////////////////////////////////////////////////////////// //本程序只供学习使用,未经作者许可,不得用于其它任何用途 //ALIENTEK miniSTM32开发板 //DS18B20驱动代码 //正点原子@ALIENTEK //技术论坛:www.openedv.com //修改日期:2012/9/12 //版本:V1.0 //版权所有,盗版必究。 //Copyright(C) 广州市星翼电子科技有限公司 2009-2019 //All rights reserved ////////////////////////////////////////////////////////////////////////////////// const GPIO_InitParam input_param = { .driving = GPIO_DRIVING_LEVEL_1, .mode = GPIOx_Pn_F0_INPUT, .pull = GPIO_PULL_UP, }; const GPIO_InitParam output_param = { .driving = GPIO_DRIVING_LEVEL_1, .mode = GPIOx_Pn_F1_OUTPUT, .pull = GPIO_PULL_NONE, }; //复位DS18B20 void DS18B20_Rst(void) { DS18B20_IO_OUT(); //SET PA0 OUTPUT DS18B20_DQ_OUT_0; //拉低DQ delay_us(750); //拉低750us DS18B20_DQ_OUT_1; //DQ=1 delay_us(15); //15US } //等待DS18B20的回应 //返回1:未检测到DS18B20的存在 //返回0:存在 u8 DS18B20_Check(void) { u8 retry=0; DS18B20_IO_IN();//SET PA0 INPUT while (DS18B20_DQ_IN&&retry<200) { retry++; delay_us(1); }; if(retry>=200)return 1; else retry=0; while (!DS18B20_DQ_IN&&retry<240) { retry++; delay_us(1); }; if(retry>=240)return 1; return 0; } //从DS18B20读取一个位 //返回值:1/0 u8 DS18B20_Read_Bit(void) // read one bit { u8 data; DS18B20_IO_OUT();//SET PA0 OUTPUT DS18B20_DQ_OUT_0; delay_us(2); DS18B20_DQ_OUT_1; DS18B20_IO_IN();//SET PA0 INPUT delay_us(12); if(DS18B20_DQ_IN)data=1; else data=0; delay_us(50); return data; } //从DS18B20读取一个字节 //返回值:读到的数据 u8 DS18B20_Read_Byte(void) // read one byte { u8 i,j,dat; dat=0; for (i=1;i<=8;i++) { j=DS18B20_Read_Bit(); dat=(j<<7)|(dat>>1); } return dat; } //写一个字节到DS18B20 //dat:要写入的字节 void DS18B20_Write_Byte(u8 dat) { u8 j; u8 testb; DS18B20_IO_OUT();//SET PA0 OUTPUT; for (j=1;j<=8;j++) { testb=dat&0x01; dat=dat>>1; if (testb) { DS18B20_DQ_OUT_0;// Write 1 delay_us(2); DS18B20_DQ_OUT_1; delay_us(60); } else { DS18B20_DQ_OUT_0;// Write 0 delay_us(60); DS18B20_DQ_OUT_1; delay_us(2); } } } //开始温度转换 void DS18B20_Start(void)// ds1820 start convert { DS18B20_Rst(); DS18B20_Check(); DS18B20_Write_Byte(0xcc);// skip rom DS18B20_Write_Byte(0x44);// convert } //初始化DS18B20的IO口 DQ 同时检测DS的存在 //返回1:不存在 //返回0:存在 u8 DS18B20_Init(void) { DS18B20_IO_OUT(); DS18B20_Rst(); return 0; } //从ds18b20得到温度值 //精度:0.1C //返回值:温度值 (-550~1250) short DS18B20_Get_Temp(void) { u8 temp; u8 TL,TH; short tem; DS18B20_Start (); // ds1820 start convert DS18B20_Rst(); DS18B20_Check(); DS18B20_Write_Byte(0xcc);// skip rom DS18B20_Write_Byte(0xbe);// convert TL=DS18B20_Read_Byte(); // LSB TH=DS18B20_Read_Byte(); // MSB if(TH>7) { TH=~TH; TL=~TL; temp=0;//温度为负 }else temp=1;//温度为正 tem=TH; //获得高八位 tem<<=8; tem+=TL;//获得底八位 tem=(float)tem*0.625;//转换 if(temp)return tem; //返回温度值 else return -tem; }
- device\xradio\xr806\ohosdemo\mqtt\ds18b20.h
#ifndef __DS18B20_H #define __DS18B20_H #include "driver/chip/hal_gpio.h" #include "driver/chip/hal_util.h" typedef unsigned long int u32; typedef unsigned short int u16; typedef unsigned char u8; typedef long int s32; typedef short int s16; typedef char s8; extern const GPIO_InitParam input_param; extern const GPIO_InitParam output_param; #define DS18B20_PORT GPIO_PORT_B #define DS18B20_PIN GPIO_PIN_4 #define DS18B20_IO_IN() {HAL_GPIO_Init(DS18B20_PORT, DS18B20_PIN, &input_param);} #define DS18B20_IO_OUT() {HAL_GPIO_Init(DS18B20_PORT, DS18B20_PIN, &output_param);} #define delay_us HAL_UDelay ////IO操作函数 #define DS18B20_DQ_OUT_0 HAL_GPIO_WritePin(DS18B20_PORT, DS18B20_PIN, GPIO_PIN_LOW) #define DS18B20_DQ_OUT_1 HAL_GPIO_WritePin(DS18B20_PORT, DS18B20_PIN, GPIO_PIN_HIGH) #define DS18B20_DQ_IN HAL_GPIO_ReadPin(DS18B20_PORT, DS18B20_PIN) //数据端口 PA0 u8 DS18B20_Init(void); //初始化DS18B20 short DS18B20_Get_Temp(void); //获取温度 void DS18B20_Start(void); //开始温度转换 void DS18B20_Write_Byte(u8 dat);//写入一个字节 u8 DS18B20_Read_Byte(void); //读出一个字节 u8 DS18B20_Read_Bit(void); //读出一个位 u8 DS18B20_Check(void); //检测是否存在DS18B20 void DS18B20_Rst(void); //复位DS18B20 #endif
- device\xradio\xr806\ohosdemo\mqtt\main.c
#include <stdio.h> #include <math.h> #include "ohos_init.h" #include "kernel/os/os.h" #include <string.h> #include "wifi_device.h" #include "wifi_hotspot.h" #include "common/framework/platform_init.h" #include "net/wlan/wlan.h" #include "common/framework/net_ctrl.h" #include "net/mqtt/MQTTClient-C/MQTTClient.h" #include "driver/chip/hal_gpio.h" #include "driver/chip/hal_util.h" #include "driver/chip/hal_util.h" #include "ds18b20.h" #define WIFI_DEVICE_CONNECT_AP_SSID "ZhuoTK_5F91" #define WIFI_DEVICE_CONNECT_AP_PSK "qq1253176313" #define MQTT_DEMO_THREAD_STACK_SIZE (8 * 1024) /* ssl need more stack */ static OS_Thread_t mqtt_demo_thread; static OS_Thread_t g_main_thread; static MQTTPacket_connectData mqtt_demo_connectData = MQTTPacket_connectData_initializer; static Client mqtt_demo_client; static Network mqtt_demo_network; #define MQTT_DEMO_CLIENT_ID "mqt1212ad564617" #define MQTT_DEMO_HOST_NAME "xxx.xxx.xxx.xxx" //填写自己MQTT服务器地址 #define MQTT_DEMO_PORT "1883" #define MQTT_DEMO_USERNAME "user1" #define MQTT_DEMO_PASSWORD "123123" #define MQTT_DEMO_TOPIC "/topic/led" // #define MQTT_DEMO_CLIENT_ID "1234785|securemode=2,signmethod=hmacsha1,timestamp=789|" // #define MQTT_DEMO_HOST_NAME "iot-as-mqtt.cn-shanghai.aliyuncs.com" // #define MQTT_DEMO_PORT "1883" // #define MQTT_DEMO_USERNAME "xr806_senor_set0&vo84Hm3xbUj" // #define MQTT_DEMO_PASSWORD "4f9ebb2143f01e4489133c1ea55051fbbfc4c44e" // #define MQTT_DEMO_TOPIC "/vo84Hm3xbUj/xr806_senor_set0/data" #define MQTT_DEMO_BUF_SIZE (2*1024) #define WIFI_START 1 #define MQTT_DEMO_MSG_TEXT "mqtt demo test" #define LED_PORT GPIO_PORT_A #define LED_PIN GPIO_PIN_21 static void led_init(void) { GPIO_InitParam param; param.driving = GPIO_DRIVING_LEVEL_1; param.mode = GPIOx_Pn_F1_OUTPUT; param.pull = GPIO_PULL_NONE; HAL_GPIO_Init(LED_PORT, LED_PIN, ¶m); } static void led_ctl(uint8_t level) { HAL_GPIO_WritePin(LED_PORT, LED_PIN, level ? GPIO_PIN_HIGH : GPIO_PIN_LOW); } void wifi_scan_connect(){ if(WIFI_SUCCESS != EnableWifi()){ printf("Error:wifi enable fail!\n"); return; } if(WIFI_SUCCESS != Scan()){ printf("Error:wifi scan fail!\n"); } printf("WIFI SCAN STARTED!\n"); OS_Sleep(3); WifiScanInfo scan_result[30]; unsigned int scan_num = 30; if(WIFI_SUCCESS != GetScanInfoList(scan_result, &scan_num)){ printf("Error: get scan result fail!\n"); return; } printf("Scan successful,you've got:\n"); for(int i = 0;i < scan_num;i++){ printf("SSID: %s ",scan_result[i].ssid); printf("RSSI: %d",scan_result[i].rssi); } printf("Scan End \n"); const char ssid_want_connect[] = WIFI_DEVICE_CONNECT_AP_SSID; const char psk[] = WIFI_DEVICE_CONNECT_AP_PSK; printf("Connecting..."); if(WIFI_STA_ACTIVE == IsWifiActive()){ printf("Wifi is active.\n"); } OS_Sleep(1); WifiDeviceConfig config = {0}; int netId = 0; int i; for(i = 0;i < scan_num;i++){ if(0 == strcmp(scan_result[i].ssid, ssid_want_connect)){ memcpy(config.ssid, scan_result[i].ssid, WIFI_MAX_SSID_LEN); memcpy(config.bssid, scan_result[i].bssid, WIFI_MAC_LEN); strcpy(config.preSharedKey, psk); config.securityType = scan_result[i].securityType; config.wapiPskType = WIFI_PSK_TYPE_ASCII; config.freq = scan_result[i].frequency; break; } } if(i >= scan_num){ printf("Error: No SSID SET FOUND!\n"); return; } printf("Found Correct SSID in Scan List!\n"); if(WIFI_SUCCESS != AddDeviceConfig(&config, &netId)){ printf("Error: Add Device config failed!\n"); return; } printf("Add Device Config successful!\n"); if(WIFI_SUCCESS != ConnectTo(netId)){ printf("Error: Connect to Wifi FAILED!\n"); return; } printf("Connect to %s successful!\n",config.ssid); OS_Sleep(3); } static int mqtt_demo_init(void) { char *send_buf; char *recv_buf; /* init client id */ mqtt_demo_connectData.clientID.cstring = MQTT_DEMO_CLIENT_ID; /* init keep alive interval */ mqtt_demo_connectData.keepAliveInterval = 30; // 30s /* enable session reuse */ mqtt_demo_connectData.cleansession = 0; /* set mqtt version */ mqtt_demo_connectData.MQTTVersion = 4; //Version of MQTT 3.1.1 /* send/recv buffer must free when mqtt deinit */ send_buf = malloc(MQTT_DEMO_BUF_SIZE); if (send_buf == NULL) { printf("no memory\n"); return -1; } recv_buf = malloc(MQTT_DEMO_BUF_SIZE); if (recv_buf == NULL) { free(send_buf); printf("no memory\n"); return -1; } /* init network */ NewNetwork(&mqtt_demo_network); /* init mqtt client object */ MQTTClient(&mqtt_demo_client, &mqtt_demo_network, 6000, (unsigned char *)send_buf, MQTT_DEMO_BUF_SIZE, (unsigned char *)recv_buf, MQTT_DEMO_BUF_SIZE); /** * set will function, when this client disconnect, * server will sent the message to every client in MQTT_DEMO_TOPIC */ mqtt_demo_connectData.willFlag = 1; mqtt_demo_connectData.will.topicName.cstring = MQTT_DEMO_TOPIC; mqtt_demo_connectData.will.message.cstring = "I am disconnected"; mqtt_demo_connectData.will.retained = 0; mqtt_demo_connectData.will.qos = 0; /* set username and password */ mqtt_demo_connectData.username.cstring = MQTT_DEMO_USERNAME; mqtt_demo_connectData.password.cstring = MQTT_DEMO_PASSWORD; return 0; } static int mqtt_demo_connect(char *host_name, char *host_port) { int ret = -1; /* need connect the server in tcp level first, if use ssl, use TLSConnectNetwork() */ ret = ConnectNetwork(&mqtt_demo_network, host_name, atoi(host_port)); if (ret != 0) { printf("mqtt connect faild, ret:%d, host:%s, port:%s\n", ret, host_name, host_port); goto exit; } /* if tcp level connected, then connect mqtt level */ ret = MQTTConnect(&mqtt_demo_client, &mqtt_demo_connectData); if (ret != 0) { printf("mqtt connect faild, ret:%d\n", ret); /* disconnect the tcp level */ mqtt_demo_network.disconnect(&mqtt_demo_network); goto exit; } printf("mqtt connected\n"); exit: return ret; } static void mqtt_demo_msg_cb(MessageData *data) { char topic_buf[20]={0}; char payload_buf[20]={0}; memcpy(topic_buf, data->topicName->lenstring.data, data->topicName->lenstring.len); memcpy(payload_buf,(char *)data->message->payload, data->message->payloadlen); printf("get a message, topic: %s, msg: %s\n", topic_buf, payload_buf); if(strcmp(topic_buf,"/topic/led") == 0){ if(strcmp((char *)payload_buf,"led8 on") == 0){ led_ctl(1); printf("led1 on\r\n"); } else if(strcmp((char *)payload_buf,"led8 off") == 0){ led_ctl(0); printf("led1 off\r\n"); } } } static int mqtt_demo_subscribe(char *topic) { int ret = -1; if (mqtt_demo_client.isconnected) { /* set the message callback */ ret = MQTTSubscribe(&mqtt_demo_client, topic, 0, mqtt_demo_msg_cb); if (ret != 0) printf("mqtt subscribe faild ret:%d\n", ret); } return ret; } static int mqtt_demo_unsubscribe(char *topic) { int ret = -1; if (mqtt_demo_client.isconnected) { ret = MQTTUnsubscribe(&mqtt_demo_client, topic); if (ret != 0) printf("mqtt unsubscribe faild, ret:%d\n", ret); } return ret; } static int mqtt_demo_publish(char *topic, char *msg) { int ret = -1; MQTTMessage message; memset(&message, 0, sizeof(message)); message.qos = 0; message.retained = 0; /* disable retain the message in server */ message.payload = msg; message.payloadlen = strlen(msg); ret = MQTTPublish(&mqtt_demo_client, topic, &message); if (ret != 0) printf("mqtt publish faild, ret:%d\n", ret); return ret; } static int mqtt_demo_disconnect(void) { int ret = -1; if (mqtt_demo_client.isconnected) { /* need disconnect mqtt level first */ ret = MQTTDisconnect(&mqtt_demo_client); if (ret != 0) printf("mqtt disconnect fail, ret:%d\n", ret); /* then disconnect tcp level */ mqtt_demo_network.disconnect(&mqtt_demo_network); } return ret; } static void mqtt_demo_deinit(void) { if (mqtt_demo_client.buf) { free(mqtt_demo_client.buf); mqtt_demo_client.buf = NULL; } if (mqtt_demo_client.readbuf) { free(mqtt_demo_client.readbuf); mqtt_demo_client.readbuf = NULL; } } static void mqtt_demo_fun(void *arg) { // uint8_t level = 1; int ret; int reconnect_times = 0; short int int_temp=0; char temp_buf[10]={0}; led_init(); DS18B20_Init(); //初始化DS18B20 wifi_scan_connect(); /* mqtt init */ mqtt_demo_init(); /* mqtt connect */ re: ret = mqtt_demo_connect(MQTT_DEMO_HOST_NAME, MQTT_DEMO_PORT); if (ret != 0) { OS_MSleep(500); goto re; } /* subscribe topic */ ret = mqtt_demo_subscribe(MQTT_DEMO_TOPIC); if (ret != 0) goto exit; while (1) { int_temp = DS18B20_Get_Temp(); sprintf(temp_buf,"%d.%d",int_temp/10, abs(int_temp%10)); printf("temp:%s\r\n", temp_buf); /* publish message to topic */ mqtt_demo_publish("/topic/temp", temp_buf); ret = MQTTYield(&mqtt_demo_client, 300); if (ret != 0) { printf("mqtt yield err, ret:%d\n", ret); reconnect: printf("mqtt reconnect\n"); mqtt_demo_disconnect(); ret = mqtt_demo_connect(MQTT_DEMO_HOST_NAME, MQTT_DEMO_PORT); if (ret != 0) { reconnect_times++; if (reconnect_times > 5) goto exit; OS_MSleep(5000); //5s goto reconnect; } } // level = (!level & 0x01); // led_ctl(level); OS_MSleep(1000); //1s } exit: mqtt_demo_unsubscribe(MQTT_DEMO_TOPIC); mqtt_demo_disconnect(); mqtt_demo_deinit(); OS_ThreadDelete(&mqtt_demo_thread); } int mqtt_entry(void) { if (OS_ThreadCreate(&mqtt_demo_thread, "mqtt_demo_thread", mqtt_demo_fun, NULL, OS_THREAD_PRIO_APP, 8 * 1024) != OS_OK) { printf("[ERR] Create MainThread Failed\n"); } } SYS_RUN(mqtt_entry);
- device\xradio\xr806\ohosdemo\mqtt\BUILD.gn;
import("//device/xradio/xr806/liteos_m/config.gni") #必须,config中定义了头文件路径和关键宏定义 static_library("app_mqtttest") { #必须,所有应用工程必须是app_打头 configs = [] sources = [ "main.c", "ds18b20.c", ] cflags = board_cflags #必须,board_cflags是在config.gni中定义的关键宏定义 include_dirs = board_include_dirs #必须,board_include_dirs是在config.gni中定义的文件路径 include_dirs += [ "//foundation/communication/wifi_lite/interfaces/wifiservice", ".", "//utils/native/lite/include", "//device/xradio/xr806/xr_skylark/include/net/mqtt/MQTTClient-C", "//device/xradio/xr806/xr_skylark/project", "//kernel/liteos_m/kernel/arch/include", "//base/iot_hardware/peripheral/interfaces/kits", ] }
第四步 编译下载
- 编译:hb build -f命令开始编译
- 烧写程序:在下载器设置里勾选下载后重启(由于注释掉了控制台,所以没复位功能了,复位需要拔掉电源线)
第五步 验证效果
1.打开串口助手,每秒会输出一次温度
2. 在电脑上打开MQTT.fx软件连接上对应的MQTT服务器,并阅订“/topic/temp”主题,每秒会接收到一次开发板传来的温度
3. 在电脑上打开MQTT.fx软件连接上对应的MQTT服务器发布“/topic/led”主题,内容“led8 on”,就可以远程打开开发板上的LED,发布“led8 off”就可以关开发板上的LED
Copyright © 2024 深圳全志在线有限公司 粤ICP备2021084185号 粤公网安备44030502007680号