OpenHarmony开源社区
直播中

任凭风吹

9年用户 991经验值
擅长:可编程逻辑 电源/新能源 MEMS/传感技术 制造/封装 模拟技术
私信 关注
[经验]

【XR806鸿蒙开发板案例】实现IOT智慧机房方案(一)

一、设计目的
随着信息网络技术的不断发展,各类规模大小不等,设备种类不同的网络设备机房随着数据处理能力和数据量急剧增长。数据中心机房内,环境监控十分重要,一旦机房环境出问题,轻则引起服务器故障,重则导致大量设备损毁和数据丢失,甚至酿成火灾。结合现有的XR806开发板,作为一个IOT模组,可以拿来实现一个搭配鸿蒙APP的IOT智慧机房方案,实现对机房环境温湿度、烟感等的实时检测、查看、设置和报警功能。

二、系统组成及功能说明

XR806芯片是一款基于安谋科技STAR-MC1处理器来研发设计的支持WIFI和BLE的高集成度无线MCU芯片,同时搭载了鸿蒙L0系统,眼下鸿蒙开发正当热,所以想到将XR806运用于机房中,实现对机房环境的实时检测,并上报到自家服务平台,同时开发一个鸿蒙APP实现对其的查看,设置功能等。

此次先实现的是将手上现有的sht30温湿度传感器模块外接到XR806上,通过I2C通讯,对传感器实时采集温湿度数据;然后通过MQTT协议将XR806设备注册到自己搭建的服务器平台,同时维持心跳,并将主控实时采集到的温湿度数据上报到MQTT服务器;最后,自行开发一个鸿蒙手机APP,通过WebSocket协议实现对温湿度传感器数据的实时展示及报警阈值设置等业务功能。

三、具体实施步骤
  • 环境及物料准备  
    编译环境采用的是openharmony官网的docker镜像(省去了自己折腾环境的过程)
    XR806开发板、sht30温湿度传感器模块、鸿蒙系统的手机、自己搭建的服务器一台。
    sht30温湿度传感器模块通过I2C0引脚外接到XR806上(参照主芯片引脚说明图进行接线)。


参照如上两图;sht30和主控I2C通讯,使用I2C0,手动将sht30上的GND接到XR806的11引脚,将SDA和SCL接到XR806上的第7、8引脚。

2.新建工程及代码实现
参考ohosdemo目录下的各种demo示例代码,创建自己的工程目录iot_smartroom。
目录结构如下:
  1. iot_smartroom
  2. |-- BUILD.gn
  3. |-- include
  4. |   |-- i2c.h
  5. |   |-- mqtt.h
  6. |-- src
  7.     |-- i2c.c
  8.     |-- main.c
  9.     |-- mqtt.c
其中mqtt.c是封装了mqtt相关接口,i2c.c是封装了i2c相关接口,main.c负责业务流程,依赖mqtt.h和i2c.h中提供的接口进行调用(后续再将main.c中有关wifi热点扫描及连接的接口独立封装出来)
详细代码如下:
devicexradioxr806ohosdemoBUILD.gn
新增"iot_smartroom:app_smartroom"
  1. group("ohosdemo") {
  2.     deps = [
  3.         #"hello_demo:app_hello",
  4.         #"iot_peripheral:app_peripheral",
  5.         #"wlan_demo:app_WlanTest",
  6.         #"led:app_led",
  7.         "iot_smartroom:app_smartroom",  
  8.     ]
  9. }
devicexradioxr806ohosdemoiot_smartroomBUILD.gn
  1. import("//device/xradio/xr806/liteos_m/config.gni")

  2. static_library("app_smartroom") {
  3.    configs = []

  4.    sources = [
  5.       "src/main.c",
  6.       "src/i2c.c",
  7.       "src/mqtt.c",
  8.    ]

  9.    cflags = board_cflags

  10.    include_dirs = board_include_dirs
  11.    include_dirs += [
  12.       "//kernel/liteos_m/kernel/arch/include",
  13.       "include",
  14.       "//base/iot_hardware/peripheral/interfaces/kits",

  15.       ".",
  16.       "//utils/native/lite/include",
  17.       "//foundation/communication/wifi_lite/interfaces/wifiservice",
  18.       "//device/xradio/xr806/xr_skylark/project"
  19.    ]
  20. }

  21. devicexradioxr806ohosdemoiot_smartroomsrci2c.c 主要代码:

  22. #include
  23. #include
  24. #include

  25. #include "stdarg.h"
  26. #include "kernel/os/os.h"
  27. #include "i2c.h"
  28. #include "ohos_init.h"

  29. #define I2C_SPEED (400000)
  30. #define I2C_DEVICE_ADDR (0x45)
  31. #define I2C_DEVICE_ID_ADDR (0x0F)
  32. #define I2C_DEVICE_ID (0x65)
  33. #define I2C_ID (0)

  34. static OS_Thread_t g_main_thread;
  35. static SHT30_DATA sht30_data;

  36. static unsigned char sht30_checkcrc(unsigned char data[], unsigned char numOfBytes, unsigned char checkSum)
  37. {
  38.     unsigned char crc = 0xFF;
  39.     unsigned char bit = 0;
  40.     unsigned char byteCtr ;
  41.     const unsigned short POLYNOMIAL = 0x131;
  42.     for(byteCtr = 0; byteCtr < numOfBytes; ++byteCtr)
  43.     {
  44.         crc ^= (data[byteCtr]);
  45.         for ( bit = 8; bit > 0; --bit)
  46.         {
  47.             if (crc & 0x80) crc = (crc << 1) ^ POLYNOMIAL;
  48.             else crc = (crc << 1);
  49.         }
  50.     }

  51.     if(crc != checkSum)
  52.         return 1;
  53.     else
  54.         return 0;
  55. }

  56. static float sht30_calcrh(unsigned short u16sRH)
  57. {
  58.     float humidityRH = 0;              // variable for result
  59.    
  60.     u16sRH &= ~0x0003;          // clear bits [1..0] (status bits)
  61.     //-- calculate relative humidity [%RH] --
  62.     humidityRH = (100 * (float)u16sRH / 65535);  // RH = rawValue / (2^16-1) * 10
  63.    
  64.     return humidityRH;
  65. }

  66. static float sht30_calctemperature(unsigned short u16sT)
  67. {
  68.     float temperature = 0;            // variable for result

  69.     u16sT &= ~0x0003;           // clear bits [1..0] (status bits)
  70.     //-- calculate temperature [℃] --
  71.     temperature = (175 * (float)u16sT / 65535 - 45); //T = -45 + 175 * rawValue / (2^16-1)
  72.    
  73.     return temperature;
  74. }

  75. static int sht30_init(void)
  76. {
  77.     int s32Ret;
  78.     unsigned char send_data[2] = {0x23, 0x34};
  79.     s32Ret = IoTI2cWrite(I2C_ID, I2C_DEVICE_ADDR, send_data, 2);
  80.     if (s32Ret != 0) {
  81.         printf("Error: I2C write s32Ret = 0x%x!rn", s32Ret);
  82.         return -1;
  83.     }
  84.     return 0;
  85. }

  86. int IotI2C_Init(void)
  87. {
  88.     int s32Ret = 0;
  89.     s32Ret = IoTI2cInit(I2C_ID, I2C_SPEED);
  90.     if(s32Ret != 0){
  91.         printf("IoTI2cInit failed.rn");
  92.         return -1;
  93.     }else{
  94.         printf("IoTI2cInit success.rn");
  95.     }
  96.     s32Ret = sht30_init(); //sht30温湿度模块初始化
  97.     if(s32Ret != 0){
  98.         printf("sht30_init failed.rn");
  99.         return -1;
  100.     }else{
  101.         printf("sht30_init success.rn");
  102.     }
  103.     return 0;
  104. }

  105. int IoTI2c_Deinit(void)
  106. {
  107.     IoTI2cDeinit(I2C_ID);
  108.     return 0;
  109. }

  110. int IotI2C_ReadData(SHT30_DATA *pData)
  111. {
  112.     float a = 0;
  113.     unsigned int s32Ret;
  114.     float temperature = 0;
  115.     float humidity = 0;
  116.     unsigned char  data[3];    //data array for checksum verification
  117.     unsigned short dat, tmp;
  118.     unsigned char SHT3X_Data_Buffer[6];   //byte 0,1 is temperature byte 4,5 is humidity
  119.     unsigned char send_data[2] = {0xE0,0x00};

  120.     s32Ret = IoTI2cWrite(I2C_ID, I2C_DEVICE_ADDR, send_data, 2);
  121.     if (s32Ret != 0) {
  122.         printf("Error: I2C write s32Ret = 0x%x!rn", s32Ret);
  123.         return -1;
  124.     }
  125.     s32Ret = IoTI2cRead(I2C_ID, I2C_DEVICE_ADDR, SHT3X_Data_Buffer, 6);
  126.     if (s32Ret != 0) {
  127.         printf("Error: I2C read s32Ret = 0x%x!rn", s32Ret);
  128.         return -1;
  129.     }
  130.     //check tem
  131.     data[0] = SHT3X_Data_Buffer[0];
  132.     data[1] = SHT3X_Data_Buffer[1];
  133.     data[2] = SHT3X_Data_Buffer[2];

  134.     tmp = sht30_checkcrc(data, 2, data[2]);
  135.     if(!tmp) /* value is ture */
  136.     {
  137.         dat = ((unsigned short)data[0] << 8) | data[1];
  138.         temperature = sht30_calctemperature(dat);
  139.         printf("Temperature = %frn",temperature);
  140.     }
  141.     //check humidity
  142.     data[0] = SHT3X_Data_Buffer[3];
  143.     data[1] = SHT3X_Data_Buffer[4];
  144.     data[2] = SHT3X_Data_Buffer[5];

  145.     tmp = sht30_checkcrc(data, 2, data[2]);
  146.     if(!tmp) /* value is ture */
  147.     {
  148.         dat = ((unsigned short)data[0] << 8) | data[1];
  149.         humidity = sht30_calcrh(dat);
  150.         printf("Humidity = %.02frn",humidity);
  151.     }

  152.     pData->temperature = temperature;
  153.     pData->humidity = humidity;
  154.     return 0;
  155. }

devicexradioxr806ohosdemoiot_smartroomsrcmqtt.c 主要代码:
  1. #include
  2. #include
  3. #include "ohos_init.h"
  4. #include "kernel/os/os.h"

  5. #include "iot_gpio.h"
  6. #include "wifi_device.h"
  7. #include "net/mqtt/MQTTClient-C/MQTTClient.h"

  8. #define MQTT_DEMO_USERNAME  "admin"
  9. #define MQTT_DEMO_PASSWORD  "Unionman"

  10. #define MQTT_DEMO_CLIENT_ID "plug_ipc"
  11. #define MQTT_DEMO_BUF_SIZE (2*1024)

  12. static MQTTPacket_connectData mqtt_demo_connectData = MQTTPacket_connectData_initializer;
  13. static Client mqtt_client;
  14. static Network mqtt_demo_network;
  15. static int max_duty_ratio = 0;

  16. int IoTMqtt_init(void)
  17. {
  18.     char *send_buf;
  19.     char *recv_buf;

  20.     mqtt_demo_connectData.clientID.cstring = MQTT_DEMO_CLIENT_ID;
  21.     mqtt_demo_connectData.keepAliveInterval = 30; // 30s
  22.     mqtt_demo_connectData.cleansession = 0;
  23.     mqtt_demo_connectData.MQTTVersion = 4; //Version of MQTT 3.1.1

  24.     send_buf = malloc(MQTT_DEMO_BUF_SIZE);
  25.     if (send_buf == NULL) {
  26.         printf("no memoryn");
  27.         return -1;
  28.     }
  29.     recv_buf = malloc(MQTT_DEMO_BUF_SIZE);
  30.     if (recv_buf == NULL) {
  31.         free(send_buf);
  32.         printf("no memoryn");
  33.         return -1;
  34.     }

  35.     /* init network */
  36.     NewNetwork(&mqtt_demo_network);
  37.     /* init mqtt client object */
  38.     MQTTClient(&mqtt_client, &mqtt_demo_network, 6000,
  39.                (unsigned char *)send_buf, MQTT_DEMO_BUF_SIZE,
  40.                (unsigned char *)recv_buf, MQTT_DEMO_BUF_SIZE);

  41.     /* set username and password */
  42.     mqtt_demo_connectData.username.cstring = MQTT_DEMO_USERNAME;
  43.     mqtt_demo_connectData.password.cstring = MQTT_DEMO_PASSWORD;
  44.     return 0;
  45. }

  46. int IoTMqtt_connect(char *host_name, char *host_port)
  47. {
  48.     int ret = -1;

  49.     ret = ConnectNetwork(&mqtt_demo_network, host_name, atoi(host_port));
  50.     if (ret != 0) {
  51.         printf("mqtt connect faild, ret:%d, host:%s, port:%sn", ret, host_name, host_port);
  52.         goto exit;
  53.     }

  54.     ret = MQTTConnect(&mqtt_client, &mqtt_demo_connectData);
  55.     if (ret != 0) {
  56.         printf("mqtt connect faild, ret:%dn", ret);
  57.         mqtt_demo_network.disconnect(&mqtt_demo_network);
  58.         goto exit;
  59.     }
  60.     printf("mqtt connectedn");
  61. exit:
  62.     return ret;
  63. }

  64. int IoTMqtt_subscribe(char *topic)
  65. {
  66.     int ret = -1;
  67.     if (mqtt_client.isconnected) {
  68.         ret = MQTTSubscribe(&mqtt_client, topic, 0, NULL);
  69.         if (ret != 0)
  70.             printf("mqtt subscribe faild ret:%dn", ret);
  71.     }
  72.     return ret;
  73. }

  74. int IoTMqtt_unsubscribe(char *topic)
  75. {
  76.     int ret = -1;
  77.     if (mqtt_client.isconnected) {
  78.         ret = MQTTUnsubscribe(&mqtt_client, topic);
  79.         if (ret != 0)
  80.             printf("mqtt unsubscribe faild, ret:%dn", ret);
  81.     }
  82.     return ret;
  83. }

  84. int IoTMqtt_publish(char *topic, char *msg)
  85. {
  86.     int ret = -1;

  87.     MQTTMessage message;
  88.     memset(&message, 0, sizeof(message));
  89.     message.qos = 0;
  90.     message.retained = 0; /* disable retain the message in server */
  91.     message.payload = msg;
  92.     message.payloadlen = strlen(msg);
  93.     ret = MQTTPublish(&mqtt_client, topic, &message);
  94.     if (ret != 0)
  95.         printf("mqtt publish faild, ret:%dn", ret);
  96.     return ret;
  97. }

  98. int IoTMqtt_disconnect(void)
  99. {
  100.     int ret = -1;

  101.     if (mqtt_client.isconnected) {
  102.         ret = MQTTDisconnect(&mqtt_client);
  103.         if (ret != 0)
  104.             printf("mqtt disconnect fail, ret:%dn", ret);
  105.         mqtt_demo_network.disconnect(&mqtt_demo_network);
  106.     }
  107.     return ret;
  108. }

  109. void IoTMqtt_deinit(void)
  110. {
  111.     if (mqtt_client.buf) {
  112.         free(mqtt_client.buf);
  113.         mqtt_client.buf = NULL;
  114.     }
  115.     if (mqtt_client.readbuf) {
  116.         free(mqtt_client.readbuf);
  117.         mqtt_client.readbuf = NULL;
  118.     }
  119. }

  120. devicexradioxr806ohosdemoiot_smartroomsrcmain.c主要代码:

  121. #include
  122. #include "wifi_device.h"
  123. #include "ohos_init.h"
  124. #include "kernel/os/os.h"
  125. #include "mqtt.h"
  126. #include "i2c.h"
  127. #include "common/framework/net_ctrl.h"

  128. #define IOT_DEVICEID "666666"

  129. #define MQTT_DEMO_HOST_NAME "192.168.220.220" //MQTT服务器IP
  130. #define MQTT_DEMO_PORT      "1883"

  131. #define WIFI_DEVICE_CONNECT_AP_SSID "yur"
  132. #define WIFI_DEVICE_CONNECT_AP_PSK "06184188"

  133. #define MQTT_PUB_REG_TOPIC "/device/register"             //设备注册
  134. #define MQTT_PUB_HEART_TOPIC "/device/heartbeat"         //设备心跳
  135. #define MQTT_PUB_TEM_TOPIC "/door/device/temperature"     //设备发布温度TOPIC
  136. #define MQTT_PUB_HUM_TOPIC "/door/device/humidity"        //设备发布湿度TOPIC

  137. static OS_Thread_t g_mqtt_thread;
  138. static OS_Thread_t g_main_thread;
  139. static SHT30_DATA sht30_data;

  140. static void iot_task(void *arg)   
  141. {
  142.     int ret;
  143.     int reconnect_times = 0;
  144.     unsigned char payload[64] = "";
  145.         
  146.     //温湿度传感器初始化
  147.     IotI2C_Init();
  148.    
  149.     //MQTT初始化
  150.     IoTMqtt_init();

  151.     ret = IoTMqtt_connect(MQTT_DEMO_HOST_NAME, MQTT_DEMO_PORT);
  152.     if (ret != 0)
  153.     {
  154.         goto exit;
  155.     }
  156.    
  157.     //发布设备注册主题
  158.     IoTMqtt_publish(MQTT_PUB_REG_TOPIC, IOT_DEVICEID);
  159.     while (1)
  160.     {
  161.         IoTMqtt_publish(MQTT_PUB_HEART_TOPIC, IOT_DEVICEID);
  162.         
  163.         IotI2C_ReadData(&sht30_data);
  164.         memset(payload, 0x0, sizeof(payload));
  165.         snprintf(payload, sizeof(payload), "%s/%.2f/%d", IOT_DEVICEID,
  166.                         sht30_data.temperature, 0);
  167.         IoTMqtt_publish(MQTT_PUB_TEM_TOPIC, payload);
  168.         memset(payload, 0x0, sizeof(payload));   
  169.         snprintf(payload, sizeof(payload), "%s/%.2f/%d", IOT_DEVICEID,
  170.                         sht30_data.humidity, 0);            
  171.         IoTMqtt_publish(MQTT_PUB_HUM_TOPIC, payload);
  172.         
  173.         OS_MSleep(2000); //2s
  174.     }

  175. exit:
  176.     IoTMqtt_unsubscribe(MQTT_PUB_TEM_TOPIC);
  177.     IoTMqtt_unsubscribe(MQTT_PUB_HUM_TOPIC);
  178.     IoTMqtt_disconnect();
  179.     IoTMqtt_deinit();
  180.     IoTI2c_Deinit();
  181.     OS_ThreadDelete(&g_mqtt_thread);
  182. }

  183. static void net_cb(uint32_t event, uint32_t data, void *arg)
  184. {
  185.     uint16_t type = EVENT_SUBTYPE(event);
  186.     switch (type)
  187.     {
  188.         case NET_CTRL_MSG_NETWORK_UP:
  189.             printf("NET_CTRL_MSG_NETWORK_UPn");
  190.             if (!OS_ThreadIsValid(&g_mqtt_thread))
  191.             {
  192.                 OS_ThreadCreate(&g_mqtt_thread, "iot_task",
  193.                                     iot_task, (void *)NULL,  OS_THREAD_PRIO_APP, (8 * 1024));
  194.             }
  195.             break;
  196.         case NET_CTRL_MSG_NETWORK_DOWN:
  197.             break;
  198.         default:
  199.             break;
  200.     }
  201. }

  202. static int MainThread(void *arg)
  203. {
  204.     int s32Ret = 0;
  205.    
  206.     //使能、扫描、连接热点
  207.     if (WIFI_SUCCESS != EnableWifi())
  208.     {
  209.         printf("Error: EnableWifi failn");
  210.         return;
  211.     }

  212.     OS_Sleep(1);

  213.     if (WIFI_SUCCESS != Scan()) {
  214.         printf("Error: Scan fail.n");
  215.         return;
  216.     }

  217.     OS_Sleep(3);//这里为了方便用延时,实际用回调更好,否则3秒可能不够

  218.     const char ssid_want_connect[] = WIFI_DEVICE_CONNECT_AP_SSID;
  219.     const char psk[] = WIFI_DEVICE_CONNECT_AP_PSK;
  220.     WifiScanInfo scan_results[30];
  221.     unsigned int scan_num = 30;

  222.     if (WIFI_SUCCESS != GetScanInfoList(scan_results, &scan_num))
  223.     {
  224.         printf("Error: GetScanInfoList fail.n");
  225.         return;
  226.     }

  227.     WifiDeviceConfig config = { 0 };
  228.     int netId = 0;

  229.     int i;
  230.     for (i = 0; i < scan_num; i++)
  231.     {
  232.         printf("ssid: %s    ", scan_results[i].ssid);
  233.         printf("securityType: %dn", scan_results[i].securityType);
  234.         if (0 == strcmp(scan_results[i].ssid, ssid_want_connect))
  235.         {
  236.             memcpy(config.ssid, scan_results[i].ssid,
  237.                    WIFI_MAX_SSID_LEN);
  238.             memcpy(config.bssid, scan_results[i].bssid,
  239.                    WIFI_MAC_LEN);
  240.             strcpy(config.preSharedKey, psk);
  241.             config.securityType = scan_results[i].securityType;
  242.             config.wapiPskType = WIFI_PSK_TYPE_ASCII;
  243.             config.freq = scan_results[i].frequency;
  244.             break;
  245.         }
  246.     }

  247.     if (i >= scan_num)
  248.     {
  249.         printf("Error: No found ssid in scan_resultsn");
  250.         return -1;
  251.     }

  252.     if (WIFI_SUCCESS != AddDeviceConfig(&config, &netId))
  253.     {
  254.         printf("Error: AddDeviceConfig Failn");
  255.         return -1;
  256.     }
  257.     printf("Config Successn");

  258.     if (WIFI_SUCCESS != ConnectTo(netId))
  259.     {
  260.         printf("Error: ConnectTo Failn");
  261.         return -1;
  262.     }
  263.    
  264.     observer_base *net_ob;
  265.     net_ob = sys_callback_observer_create(CTRL_MSG_TYPE_NETWORK, NET_CTRL_MSG_ALL, net_cb, NULL);
  266.     if (net_ob == NULL)
  267.     {
  268.         return -1;
  269.     }

  270.     if (sys_ctrl_attach(net_ob) != 0)
  271.     {
  272.         return -1;
  273.     }

  274.     while(1)
  275.     {
  276.         OS_MSleep(2000);
  277.     }
  278.     return 0;
  279. }

  280. void SmartRoomMain(void)
  281. {
  282.     printf("rSmartRoomMain Startrn");

  283.     if (OS_ThreadCreate(&g_main_thread, "MainThread", MainThread, NULL,
  284.                 OS_THREAD_PRIO_APP, 4 * 1024) != OS_OK) {
  285.         printf("[ERR] Create MainThread Failedn");
  286.     }
  287. }

  288. SYS_RUN(SmartRoomMain);

完整代码上传到:
链接:https://pan.baidu.com/s/11Iy-...
提取码:80qj
3.编译及烧录
编译:
  1. hb build -f

  2. [OHOS INFO] [238/256] AR libs/libxr_wifi_adapter.a
  3. [OHOS INFO] [239/256] STAMP obj/device/xradio/xr806/adapter/hals/adapter.stamp
  4. [OHOS INFO] [240/256] gcc cross compiler obj/kernel/liteos_m/kal/cmsis/libcmsis.cmsis_liteos.o
  5. [OHOS INFO] [241/256] AR libs/libcmsis.a
  6. [OHOS INFO] [242/256] AR libs/libkal.a
  7. [OHOS INFO] [243/256] gcc cross compiler obj/third_party/bounds_checking_function/src/libsec.secureinput_w.o
  8. [OHOS INFO] [244/256] gcc cross compiler obj/kernel/liteos_m/kernel/src/mm/libkernel.los_memory.o
  9. [OHOS INFO] [245/256] gcc cross compiler obj/third_party/bounds_checking_function/src/libsec.secureinput_a.o
  10. [OHOS INFO] [246/256] AR libs/libsec.a
  11. [OHOS INFO] [247/256] AR libs/libwifiservice.a
  12. [OHOS INFO] [248/256] STAMP obj/foundation/communication/wifi_lite/wifi.stamp
  13. [OHOS INFO] [249/256] gcc cross compiler obj/kernel/liteos_m/kernel/src/libkernel.los_task.o
  14. [OHOS INFO] [250/256] AR libs/libkernel.a
  15. [OHOS INFO] [251/256] STAMP obj/kernel/liteos_m/kernel.stamp
  16. [OHOS INFO] [252/256] STAMP obj/build/lite/ohos.stamp
  17. [OHOS INFO] [253/256] ACTION //build/lite:gen_rootfs(//build/lite/toolchain:arm-none-eabi-gcc)
  18. [OHOS INFO] [254/256] STAMP obj/build/lite/gen_rootfs.stamp
  19. [OHOS INFO] [255/256] ACTION //device/xradio/xr806:libSDK(//build/lite/toolchain:arm-none-eabi-gcc)
  20. [OHOS INFO] [256/256] STAMP obj/device/xradio/xr806/libSDK.stamp
  21. [OHOS INFO] /sdg/xiaoyuan.cai/project/xr806/vendor/xradio/xr806/fs.yml not found, stop packing fs. If the product does not need to be packaged, ignore it.
  22. [OHOS INFO] wifi_skylark build success
  23. [OHOS INFO] cost time: 0:00:11
  24. [OpenHarmony][xr806]#
代码编译成功,参照官方烧录文档,使用PhoenixMC工具烧录到XR806开发板上:


4.服务端搭设及鸿蒙APP开发
MQTT服务端搭设省略,可自行百度;服务端由于需要同时MQTT对接XR806设备端以及WebSocket对接鸿蒙APP端,所以需要实现业务逻辑,在此是通过java编写打包成jar运行于服务器上,主要是实现了对接XR806 MQTT发起的设备注册以及心跳,同时通知给APP,此实现还涉及到mqsql数据库的应用,在此不赘述。

因为XR806是运行的鸿蒙L0系统,为了统一到鸿蒙的旗帜下,此次手机应用也采用了基于鸿蒙手机开发APP,重点是实现了对设备端采集到的温湿度实时展示,其他功能后续继续完善。

服务端运行LOG监视(接收XR806的心跳及上报的温湿度信息):
  1. 2022-01-11 16:41:14.692  INFO 1 --- [C41A5031E110123] c.u.smartroom.common.util.LogUtils        : 接收到信息topic:/device/heartbeat msg:666666,开始处理
  2. 2022-01-11 16:41:14.692  INFO 1 --- [C41A5031E110123] c.u.smartroom.common.util.LogUtils        : ------刷新心跳------
  3. 2022-01-11 16:41:14.696  INFO 1 --- [C41A5031E110123] c.u.smartroom.common.util.LogUtils        : 接收到信息topic:/door/device/temperature msg:666666/23.33/0,开始处理
  4. 2022-01-11 16:41:14.696  INFO 1 --- [C41A5031E110123] c.u.smartroom.common.util.LogUtils        : ------温度信息处理------
  5. 2022-01-11 16:41:14.696 DEBUG 1 --- [C41A5031E110123] c.u.d.m.N.findNewDeviceBySerialId        : ==>  Preparing: select * from um_new_device where serial_id=?
  6. 2022-01-11 16:41:14.697 DEBUG 1 --- [C41A5031E110123] c.u.d.m.N.findNewDeviceBySerialId        : ==> Parameters: 666666(String)
  7. 2022-01-11 16:41:14.697 DEBUG 1 --- [C41A5031E110123] c.u.d.m.N.findNewDeviceBySerialId        : <==      Total: 1
  8. 2022-01-11 16:41:14.697 DEBUG 1 --- [C41A5031E110123] d.m.N.UpdateTemperatureAndWarnBySerialId : ==>  Preparing: update um_new_device set temperature = ?, temperature_warn = ? where serial_id = ?
  9. 2022-01-11 16:41:14.698 DEBUG 1 --- [C41A5031E110123] d.m.N.UpdateTemperatureAndWarnBySerialId : ==> Parameters: 23.33(String), 0(Integer), 666666(String)
  10. 2022-01-11 16:41:14.725 DEBUG 1 --- [C41A5031E110123] d.m.N.UpdateTemperatureAndWarnBySerialId : <==    Updates: 1
  11. 2022-01-11 16:41:14.725  INFO 1 --- [C41A5031E110123] c.u.smartroom.common.util.LogUtils        : 更新温度信息成功!
  12. 2022-01-11 16:41:14.725  INFO 1 --- [C41A5031E110123] c.u.smartroom.common.util.LogUtils        : ------------666666--------
  13. 2022-01-11 16:41:14.725  INFO 1 --- [C41A5031E110123] c.u.smartroom.common.util.LogUtils        : 发送消息+channelId:0242acfffe150002-00000001-0000004a-140074f3a73178fa-1e4d0f61param:{"temperatureWarn":0,"temperature":"23.33","state":1,"type":"smart_computer_room","deviceId":"666666"}
  14. 2022-01-11 16:41:14.725  INFO 1 --- [C41A5031E110123] c.u.smartroom.common.util.LogUtils        : 接收到信息topic:/door/device/humidity msg:666666/48.00/0,开始处理
  15. 2022-01-11 16:41:14.725  INFO 1 --- [C41A5031E110123] c.u.smartroom.common.util.LogUtils        : ------湿度信息处理------
  16. 2022-01-11 16:41:14.725 DEBUG 1 --- [C41A5031E110123] c.u.d.m.N.findNewDeviceBySerialId        : ==>  Preparing: select * from um_new_device where serial_id=?
  17. 2022-01-11 16:41:14.725 DEBUG 1 --- [C41A5031E110123] c.u.d.m.N.findNewDeviceBySerialId        : ==> Parameters: 666666(String)
  18. 2022-01-11 16:41:14.726 DEBUG 1 --- [C41A5031E110123] c.u.d.m.N.findNewDeviceBySerialId        : <==      Total: 1
  19. 2022-01-11 16:41:14.726 DEBUG 1 --- [C41A5031E110123] .u.d.m.N.UpdateHumidityAndWarnBySerialId : ==>  Preparing: update um_new_device set humidity = ?, humidity_warn = ? where serial_id = ?
  20. 2022-01-11 16:41:14.726 DEBUG 1 --- [C41A5031E110123] .u.d.m.N.UpdateHumidityAndWarnBySerialId : ==> Parameters: 48.00(String), 0(Integer), 666666(String)
  21. 2022-01-11 16:41:14.750 DEBUG 1 --- [C41A5031E110123] .u.d.m.N.UpdateHumidityAndWarnBySerialId : <==    Updates: 1
  22. 2022-01-11 16:41:14.750  INFO 1 --- [C41A5031E110123] c.u.smartroom.common.util.LogUtils        : 更新湿度信息成功!
  23. 2022-01-11 16:41:14.750  INFO 1 --- [C41A5031E110123] c.u.smartroom.common.util.LogUtils        : ------------666666--------
  24. [0242acfffe150002-00000001-0000004a-140074f3a73178fa-1e4d0f61]
  25. 2022-01-11 16:41:14.750  INFO 1 --- [C41A5031E110123] c.u.smartroom.common.util.LogUtils        : 发送消息+channelId:0242acfffe150002-00000001-0000004a-140074f3a73178fa-1e4d0f61param:{"humidityWarn":0,"humidity":"48.00","state":1,"type":"smart_computer_room","deviceId":"666666"}

最终华为Mate40E运行效果:

PS:由于鸿蒙APP开发经验不足,过程还是遇到不少问题,由于文章篇幅原因,这里不详细讲解服务端及APP代码,需要服务器JAVA及鸿蒙APP源码的可以私信。

四、参考资料
https://aijishu.com/a/1060000...(基于安谋科技STAR-MC1处理器的「全志科技XR806开发板」简介)
https://aijishu.com/a/1060000...(全志XR806开发板硬件bomlist,开发板及底板设计原理图下载)
https://aijishu.com/a/1060000...(全志XR806开发板软件系统简介)
https://aijishu.com/a/1060000...(【开发板试用报告】学习MQTT开发)

文章转自极术社区
                                                   

回帖(1)

无人岛

2022-3-14 18:16:58
很棒的案例,学习收藏了
举报

更多回帖

发帖
×
20
完善资料,
赚取积分