[文章]【HarmonyOS_Hi3861学习笔记】【连载】上位机监控温湿度数据

阅读量0
0
1
当初申请的时候就是要完成在上位机上监控传感器数据。
基本功能,用c#编写上位机,监控板子上传的数据。通信格式自定义。这里就随便定义的标志头A0 5A + 4bytes湿度数据 + 4bytes温度数据 + 0xA5  ,共11bytes。
主要步骤为驱动wifi,连接热点,获取IP。然后建立socket,创建服务器并监听客户端连接。接收发送数据。
wifi连接热点参考的连志安的例程。这里为了方便将连接ap以及创建socket并绑定端口放一起了

  1. static volatile int g_connected = 0;
  2. volatile int connfd = -1;
  3. static void OnWifiConnectionChanged(int state, WifiLinkedInfo* info)
  4. {
  5.     if (!info) return;

  6.     printf("%s %d, state = %d, info = rn", __FUNCTION__, __LINE__, state);
  7.     PrintLinkedInfo(info);

  8.     if (state == WIFI_STATE_AVALIABLE) {
  9.         g_connected = 1;
  10.     } else {
  11.         g_connected = 0;
  12.         connfd = -1;
  13.     }
  14. }

  15. static void OnWifiScanStateChanged(int state, int size)
  16. {
  17.     printf("%s %d, state = %X, size = %drn", __FUNCTION__, __LINE__, state, size);
  18. }

  19. static WifiEvent g_defaultWifiEventListener = {
  20.     .OnWifiConnectionChanged = OnWifiConnectionChanged,
  21.     .OnWifiScanStateChanged = OnWifiScanStateChanged
  22. };

  23. static struct netif* g_iface = NULL;

  24. err_t netifapi_set_hostname(struct netif *netif, char *hostname, u8_t namelen);

  25. int ConnectToHotspot(WifiDeviceConfig* apConfig)
  26. {
  27.     WifiErrorCode errCode;
  28.     int netId = -1;

  29.     errCode = RegisterWifiEvent(&g_defaultWifiEventListener);
  30.     printf("RegisterWifiEvent: %drn", errCode);

  31.     errCode = EnableWifi();
  32.     printf("EnableWifi: %drn", errCode);

  33.     errCode = AddDeviceConfig(apConfig, &netId);
  34.     printf("AddDeviceConfig: %drn", errCode);

  35.     g_connected = 0;
  36.     errCode = ConnectTo(netId);
  37.     printf("ConnectTo(%d): %drn", netId, errCode);

  38.     while (!g_connected) { // wait until connect to AP
  39.         osDelay(10);
  40.     }
  41.     printf("g_connected: %drn", g_connected);

  42.     g_iface = netifapi_netif_find("wlan0");
  43.     if (g_iface)
  44.     {
  45.         err_t ret = 0;
  46.         char* hostname = "hispark";
  47.         ret = netifapi_set_hostname(g_iface, hostname, strlen(hostname));
  48.         printf("netifapi_set_hostname: %drn", ret);

  49.         ret = netifapi_dhcp_start(g_iface);
  50.         printf("netifapi_dhcp_start: %drn", ret);

  51.         osDelay(100); // wait DHCP server give me IP
  52. #if 1
  53.         ret = netifapi_netif_common(g_iface, dhcp_clients_info_show, NULL);
  54.         printf("netifapi_netif_common: %drn", ret);
  55. #else
  56.         // 下面这种方式也可以打印 IP、网关、子网掩码信息
  57.         ip4_addr_t ip = {0};
  58.         ip4_addr_t netmask = {0};
  59.         ip4_addr_t gw = {0};
  60.         ret = netifapi_netif_get_addr(g_iface, &ip, &netmask, &gw);
  61.         if (ret == ERR_OK) {
  62.             printf("ip = %srn", ip4addr_ntoa(&ip));
  63.             printf("netmask = %srn", ip4addr_ntoa(&netmask));
  64.             printf("gw = %srn", ip4addr_ntoa(&gw));
  65.         }
  66.         printf("netifapi_netif_get_addr: %drn", ret);
  67. #endif
  68.     }
  69.     return netId;
  70. }

  71. void DisconnectWithHotspot(int netId)
  72. {
  73.     if (g_iface) {
  74.         err_t ret = netifapi_dhcp_stop(g_iface);
  75.         printf("netifapi_dhcp_stop: %drn", ret);
  76.     }

  77.     WifiErrorCode errCode = Disconnect(); // disconnect with your AP
  78.     printf("Disconnect: %drn", errCode);

  79.     errCode = UnRegisterWifiEvent(&g_defaultWifiEventListener);
  80.     printf("UnRegisterWifiEvent: %drn", errCode);

  81.     RemoveDevice(netId); // remove AP config
  82.     printf("RemoveDevice: %drn", errCode);

  83.     errCode = DisableWifi();
  84.     printf("DisableWifi: %drn", errCode);
  85. }





  86. static void *wifiConnectTask(void *arg)
  87. {
  88.     int netId = 0;
  89.     WifiErrorCode errCode;
  90.     WifiDeviceConfig config = {0};
  91.     int sockfd = -1;
  92.    
  93.     // ssize_t retval = 0;
  94.     // char request[128] = "";

  95.     struct sockaddr_in clientAddr = {0};
  96.     socklen_t clientAddrLen = sizeof(clientAddr);

  97.     (void)arg;
  98.     WifiEvent eventListener = {
  99.         .OnWifiConnectionChanged = OnWifiConnectionChanged,
  100.         .OnWifiScanStateChanged = OnWifiScanStateChanged
  101.     };

  102.     errCode = RegisterWifiEvent(&eventListener);
  103.     printf("RegisterWifiEvent: %drn", errCode);

  104.     // 准备AP的配置参数
  105.     strcpy(config.ssid, PARAM_HOTSPOT_SSID);
  106.     strcpy(config.preSharedKey, PARAM_HOTSPOT_PSK);
  107.     config.securityType = PARAM_HOTSPOT_TYPE;
  108.     usleep(10000);
  109.     printf("start wifiConnectTaskrn");
  110.     while(1)
  111.     {
  112.         if(key_val == KEY_S1)
  113.         {
  114.             key_val = KEY_NULL;
  115.             printf("connect to AP ...rn");
  116.             netId = ConnectToHotspot(&config);
  117.             printf("netId: %drn", netId);
  118.             sockfd = creat_TCP_Server(8900);
  119.             // NetDemoTest(PARAM_SERVER_PORT, PARAM_SERVER_ADDR);
  120.         }
  121.         else if(key_val == KEY_S2)
  122.         {         
  123.             key_val = KEY_NULL;
  124.             if(g_connected)
  125.             {
  126.                 lwip_close(sockfd);
  127.                 usleep(100000);
  128.                 printf("disconnect to AP ...rn");
  129.                 DisconnectWithHotspot(netId);
  130.                 printf("disconnect to AP done!rn");
  131.             }
  132.         }
  133.         if(g_connected)
  134.         {
  135.             usleep(100000);
  136.             connfd = accept(sockfd, (struct sockaddr *)&clientAddr, &clientAddrLen);
  137.             if (connfd < 0)
  138.             {
  139.                 printf("accept failed, %d, %drn", connfd, errno);
  140.                 // lwip_close(sockfd);
  141.             }
  142.             else
  143.             {
  144.                 printf("accept success, connfd = %d!rn", connfd);
  145.                 printf("client addr info: host = %s, port = %drn", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port));

  146.             }
  147.         }
  148.         usleep(10000);
  149.     }
  150.     return NULL;
  151. }

  152. static void wifiConnectEntry(void)
  153. {
  154.     osThreadAttr_t attr;

  155.     attr.name = "wifiTask";
  156.     attr.attr_bits = 0U;
  157.     attr.cb_mem = NULL;
  158.     attr.cb_size = 0U;
  159.     attr.stack_mem = NULL;
  160.     attr.stack_size = 5120;
  161.     attr.priority = osPriorityNormal;

  162.     if (osThreadNew((osThreadFunc_t)wifiConnectTask, NULL, &attr) == NULL) {
  163.         printf("[wifiConnectEntry] Falied to create wifiConnectEntry!n");
  164.     }
  165. }

  166. SYS_RUN(wifiConnectEntry);


  167. static void *tcpServerReceiveTask(void *arg)
  168. {
  169.     unsigned char rcv_buf[100] = {0};
  170.     ssize_t retval = 0;
  171.     (void)arg;

  172.     while(1)
  173.     {
  174.         if(connfd >= 0)
  175.         {
  176.             retval = recv(connfd, rcv_buf, sizeof(rcv_buf), 0);
  177.             if (retval < 0)
  178.             {
  179.                 printf("recv request failed, %ld!rn", retval);

  180.             }
  181.             else
  182.             {
  183.                 printf("recv request{%s} from client done!rn", rcv_buf);
  184.             }
  185.         }
  186.         usleep(10000);
  187.     }
  188.     return NULL;
  189. }

  190. static void tcpServer_ReceiveEntry(void)
  191. {
  192.     osThreadAttr_t attr;

  193.     attr.name = "tcpServerReceiveTask";
  194.     attr.attr_bits = 0U;
  195.     attr.cb_mem = NULL;
  196.     attr.cb_size = 0U;
  197.     attr.stack_mem = NULL;
  198.     attr.stack_size = 1024;
  199.     attr.priority = osPriorityNormal3;

  200.     if (osThreadNew((osThreadFunc_t)tcpServerReceiveTask, NULL, &attr) == NULL) {
  201.         printf("[tcpServer_ReceiveEntry] Falied to create tcpServer_ReceiveEntry!n");
  202.     }
  203. }

  204. SYS_RUN(tcpServer_ReceiveEntry);


  205. static void *tcpServerSendTask(void *arg)
  206. {
  207.     unsigned char send_buf[100] = {0xA0, 0x5A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA5};
  208.     ssize_t retval = 0;
  209.     (void)arg;

  210.     while(1)
  211.     {
  212.         if(connfd >= 0)
  213.         {
  214.             send_buf[2] = (hi_u8)(AHT20_Reseult.HT[0]>>24);
  215.             send_buf[3] = (hi_u8)(AHT20_Reseult.HT[0]>>16);
  216.             send_buf[4] = (hi_u8)(AHT20_Reseult.HT[0]>>8);
  217.             send_buf[5] = (hi_u8)(AHT20_Reseult.HT[0]);

  218.             send_buf[6] = (hi_u8)(AHT20_Reseult.HT[1]>>24);
  219.             send_buf[7] = (hi_u8)(AHT20_Reseult.HT[1]>>16);
  220.             send_buf[8] = (hi_u8)(AHT20_Reseult.HT[1]>>8);
  221.             send_buf[9] = (hi_u8)(AHT20_Reseult.HT[1]);
  222.             retval = send(connfd, send_buf, 11, 0);
  223.             if (retval <= 0)
  224.             {
  225.                 printf("send response failed, %ld!rn", retval);
  226.             }
  227.         }
  228.         usleep(1000000);
  229.     }
  230.     return NULL;
  231. }

  232. static void tcpServer_SendEntry(void)
  233. {
  234.     osThreadAttr_t attr;

  235.     attr.name = "tcpServerSendTask";
  236.     attr.attr_bits = 0U;
  237.     attr.cb_mem = NULL;
  238.     attr.cb_size = 0U;
  239.     attr.stack_mem = NULL;
  240.     attr.stack_size = 10240;
  241.     attr.priority = osPriorityNormal2;

  242.     if (osThreadNew((osThreadFunc_t)tcpServerSendTask, NULL, &attr) == NULL) {
  243.         printf("[tcpServer_SendEntry] Falied to create tcpServer_SendEntry!n");
  244.     }
  245. }

  246. SYS_RUN(tcpServer_SendEntry);
复制代码


上位机客户端连接上服务器后1s钟间隔发送传感器数据给上位机。上位机解析后显示出来。效果如下:
上位机显示\n
上位机显示

视频效果:


回帖

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容图片侵权或者其他问题,请联系本站作侵删。 侵权投诉
链接复制成功,分享给好友