|
【HarmonyOS_Hi3861学习笔记】【连载】上位机监控温湿度数据, 当初申请的时候就是要完成在上位机上监控传感器数据。
基本功能,用c#编写上位机,监控板子上传的数据。通信格式自定义。这里就随便定义的标志头A0 5A + 4bytes湿度数据 + 4bytes温度数据 + 0xA5 ,共11bytes。
主要步骤为驱动wifi,连接热点,获取IP。然后建立socket,创建服务器并监听客户端连接。接收发送数据。
wifi连接热点参考的连志安的例程。这里为了方便将连接ap以及创建socket并绑定端口放一起了
- static volatile int g_connected = 0;
- volatile int connfd = -1;
- static void OnWifiConnectionChanged(int state, WifiLinkedInfo* info)
- {
- if (!info) return;
- printf(“%s %d, state = %d, info = \r\n“, __FUNCTION__, __LINE__, state);
- PrintLinkedInfo(info);
- if (state == WIFI_STATE_AVALIABLE) {
- g_connected = 1;
- } else {
- g_connected = 0;
- connfd = -1;
- }
- }
- static void OnWifiScanStateChanged(int state, int size)
- {
- printf(“%s %d, state = %X, size = %d\r\n“, __FUNCTION__, __LINE__, state, size);
- }
- static WifiEvent g_defaultWifiEventListener = {
- .OnWifiConnectionChanged = OnWifiConnectionChanged,
- .OnWifiScanStateChanged = OnWifiScanStateChanged
- };
- static struct netif* g_iface = NULL;
- err_t netifapi_set_hostname(struct netif *netif, char *hostname, u8_t namelen);
- int ConnectToHotspot(WifiDeviceConfig* apConfig)
- {
- WifiErrorCode errCode;
- int netId = -1;
- errCode = RegisterWifiEvent(&g_defaultWifiEventListener);
- printf(“RegisterWifiEvent: %d\r\n“, errCode);
- errCode = EnableWifi();
- printf(“EnableWifi: %d\r\n“, errCode);
- errCode = AddDeviceConfig(apConfig, &netId);
- printf(“AddDeviceConfig: %d\r\n“, errCode);
- g_connected = 0;
- errCode = ConnectTo(netId);
- printf(“ConnectTo(%d): %d\r\n“, netId, errCode);
- while (!g_connected) { // wait until connect to AP
- osDelay(10);
- }
- printf(“g_connected: %d\r\n“, g_connected);
- g_iface = netifapi_netif_find(“wlan0“);
- if (g_iface)
- {
- err_t ret = 0;
- char* hostname = “hispark“;
- ret = netifapi_set_hostname(g_iface, hostname, strlen(hostname));
- printf(“netifapi_set_hostname: %d\r\n“, ret);
- ret = netifapi_dhcp_start(g_iface);
- printf(“netifapi_dhcp_start: %d\r\n“, ret);
- osDelay(100); // wait DHCP server give me IP
- #if 1
- ret = netifapi_netif_common(g_iface, dhcp_clients_info_show, NULL);
- printf(“netifapi_netif_common: %d\r\n“, ret);
- #else
- // 下面这种方式也可以打印 IP、网关、子网掩码信息
- ip4_addr_t ip = {0};
- ip4_addr_t netmask = {0};
- ip4_addr_t gw = {0};
- ret = netifapi_netif_get_addr(g_iface, &ip, &netmask, &gw);
- if (ret == ERR_OK) {
- printf(“ip = %s\r\n“, ip4addr_ntoa(&ip));
- printf(“netmask = %s\r\n“, ip4addr_ntoa(&netmask));
- printf(“gw = %s\r\n“, ip4addr_ntoa(&gw));
- }
- printf(“netifapi_netif_get_addr: %d\r\n“, ret);
- #endif
- }
- return netId;
- }
- void DisconnectWithHotspot(int netId)
- {
- if (g_iface) {
- err_t ret = netifapi_dhcp_stop(g_iface);
- printf(“netifapi_dhcp_stop: %d\r\n“, ret);
- }
- WifiErrorCode errCode = Disconnect(); // disconnect with your AP
- printf(“Disconnect: %d\r\n“, errCode);
- errCode = UnRegisterWifiEvent(&g_defaultWifiEventListener);
- printf(“UnRegisterWifiEvent: %d\r\n“, errCode);
- RemoveDevice(netId); // remove AP config
- printf(“RemoveDevice: %d\r\n“, errCode);
- errCode = DisableWifi();
- printf(“DisableWifi: %d\r\n“, errCode);
- }
- static void *wifiConnectTask(void *arg)
- {
- int netId = 0;
- WifiErrorCode errCode;
- WifiDeviceConfig config = {0};
- int sockfd = -1;
-
- // ssize_t retval = 0;
- // char request[128] = ““;
- struct sockaddr_in clientAddr = {0};
- socklen_t clientAddrLen = sizeof(clientAddr);
- (void)arg;
- WifiEvent eventListener = {
- .OnWifiConnectionChanged = OnWifiConnectionChanged,
- .OnWifiScanStateChanged = OnWifiScanStateChanged
- };
- errCode = RegisterWifiEvent(&eventListener);
- printf(“RegisterWifiEvent: %d\r\n“, errCode);
- // 准备AP的配置参数
- strcpy(config.ssid, PARAM_HOTSPOT_SSID);
- strcpy(config.preSharedKey, PARAM_HOTSPOT_PSK);
- config.securityType = PARAM_HOTSPOT_TYPE;
- usleep(10000);
- printf(“start wifiConnectTask\r\n“);
- while(1)
- {
- if(key_val == KEY_S1)
- {
- key_val = KEY_NULL;
- printf(“connect to AP ...\r\n“);
- netId = ConnectToHotspot(&config);
- printf(“netId: %d\r\n“, netId);
- sockfd = creat_TCP_Server(8900);
- // NetDemoTest(PARAM_SERVER_PORT, PARAM_SERVER_ADDR);
- }
- else if(key_val == KEY_S2)
- {
- key_val = KEY_NULL;
- if(g_connected)
- {
- lwip_close(sockfd);
- usleep(100000);
- printf(“disconnect to AP ...\r\n“);
- DisconnectWithHotspot(netId);
- printf(“disconnect to AP done!\r\n“);
- }
- }
- if(g_connected)
- {
- usleep(100000);
- connfd = accept(sockfd, (struct sockaddr *)&clientAddr, &clientAddrLen);
- if (connfd < 0)
- {
- printf(“accept faiLED, %d, %d\r\n“, connfd, errno);
- // lwip_close(sockfd);
- }
- else
- {
- printf(“accept success, connfd = %d!\r\n“, connfd);
- printf(“client addr info: host = %s, port = %d\r\n“, inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port));
- }
- }
- usleep(10000);
- }
- return NULL;
- }
- static void wifiConnectEntry(void)
- {
- osThreadAttr_t attr;
- attr.name = “wifiTask“;
- attr.attr_bits = 0U;
- attr.cb_mem = NULL;
- attr.cb_size = 0U;
- attr.stack_mem = NULL;
- attr.stack_size = 5120;
- attr.priority = osPriorityNormal;
- if (osThreadNew((osThreadFunc_t)wifiConnectTask, NULL, &attr) == NULL) {
- printf(“[wifiConnectEntry] Falied to create wifiConnectEntry!\n“);
- }
- }
- SYS_RUN(wifiConnectEntry);
- static void *tcpServerReceiveTask(void *arg)
- {
- unsigned char rcv_buf[100] = {0};
- ssize_t retval = 0;
- (void)arg;
- while(1)
- {
- if(connfd >= 0)
- {
- retval = recv(connfd, rcv_buf, sizeof(rcv_buf), 0);
- if (retval < 0)
- {
- printf(“recv request failed, %ld!\r\n“, retval);
- }
- else
- {
- printf(“recv request{%s} from client done!\r\n“, rcv_buf);
- }
- }
- usleep(10000);
- }
- return NULL;
- }
- static void tcpServer_ReceiveEntry(void)
- {
- osThreadAttr_t attr;
- attr.name = “tcpServerReceiveTask“;
- attr.attr_bits = 0U;
- attr.cb_mem = NULL;
- attr.cb_size = 0U;
- attr.stack_mem = NULL;
- attr.stack_size = 1024;
- attr.priority = osPriorityNormal3;
- if (osThreadNew((osThreadFunc_t)tcpServerReceiveTask, NULL, &attr) == NULL) {
- printf(“[tcpServer_ReceiveEntry] Falied to create tcpServer_ReceiveEntry!\n“);
- }
- }
- SYS_RUN(tcpServer_ReceiveEntry);
- static void *tcpServerSendTask(void *arg)
- {
- unsigned char send_buf[100] = {0xA0, 0x5A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA5};
- ssize_t retval = 0;
- (void)arg;
- while(1)
- {
- if(connfd >= 0)
- {
- send_buf[2] = (hi_u8)(AHT20_Reseult.HT[0]>>24);
- send_buf[3] = (hi_u8)(AHT20_Reseult.HT[0]>>16);
- send_buf[4] = (hi_u8)(AHT20_Reseult.HT[0]>>8);
- send_buf[5] = (hi_u8)(AHT20_Reseult.HT[0]);
- send_buf[6] = (hi_u8)(AHT20_Reseult.HT[1]>>24);
- send_buf[7] = (hi_u8)(AHT20_Reseult.HT[1]>>16);
- send_buf[8] = (hi_u8)(AHT20_Reseult.HT[1]>>8);
- send_buf[9] = (hi_u8)(AHT20_Reseult.HT[1]);
- retval = send(connfd, send_buf, 11, 0);
- if (retval <= 0)
- {
- printf(“send response failed, %ld!\r\n“, retval);
- }
- }
- usleep(1000000);
- }
- return NULL;
- }
- static void tcpServer_SendEntry(void)
- {
- osThreadAttr_t attr;
- attr.name = “tcpServerSendTask“;
- attr.attr_bits = 0U;
- attr.cb_mem = NULL;
- attr.cb_size = 0U;
- attr.stack_mem = NULL;
- attr.stack_size = 10240;
- attr.priority = osPriorityNormal2;
- if (osThreadNew((osThreadFunc_t)tcpServerSendTask, NULL, &attr) == NULL) {
- printf(“[tcpServer_SendEntry] Falied to create tcpServer_SendEntry!\n“);
- }
- }
- SYS_RUN(tcpServer_SendEntry);
-
复制代码
上位机客户端连接上服务器后1s钟间隔发送传感器数据给上位机。上位机解析后显示出来。效果如下:
Snipaste_2021-01-24_20-14-41.png (111.32 KB, 下载次数: 0)
下载附件 保存到相册
上位机显示
昨天 20:15 上传
视频效果: |
|