本期為大家推送迪文開發(fā)者論壇獲獎開源案例——基于T5L智能屏的便攜功率計(jì)解決方案。該方案通過智能屏串口進(jìn)行通信,實(shí)時接收監(jiān)測到的電流、電壓和功率數(shù)據(jù),并同步顯示在屏幕上,使用戶可以直觀地監(jiān)測電氣參數(shù)狀態(tài)。此外,該方案還支持WiFi聯(lián)網(wǎng),無論是手機(jī)、PC還是其他移動設(shè)備,用戶均可輕松訪問電氣數(shù)據(jù),大幅提升了數(shù)據(jù)管理的便捷性與靈活性。適用于電氣工程、智能家居和工業(yè)自動化等領(lǐng)域。
UI素材展示
GUI工程設(shè)計(jì) C51工程設(shè)計(jì)
主控板將I2C采集的電流、電壓和功率數(shù)據(jù),通過串口發(fā)送給迪文智能屏,同時通過UDP將數(shù)據(jù)發(fā)送至上位機(jī),部分參考代碼如下:
staticvoidwifi_event_handler(void*arg,esp_event_base_tevent_base,int32_tevent_id,void*event_data){ if(event_id==WIFI_EVENT_AP_STACONNECTED){ wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;ESP_LOGI(TAG,"station"MACSTR"join,AID=%d",MAC2STR(event->mac),event->aid);} elseif(event_id==WIFI_EVENT_AP_STADISCONNECTED){ wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;ESP_LOGI(TAG,"station"MACSTR"leave,AID=%d",MAC2STR(event->mac),event->aid); }}void wifi_init_softap(void){ ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); esp_netif_create_default_wifi_ap(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg));ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,ESP_EVENT_ANY_ID,&wifi_event_handler,NULL,NULL)); wifi_config_twifi_config= { .ap = { .ssid = EXAMPLE_ESP_WIFI_SSID, .ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID), .channel = EXAMPLE_ESP_WIFI_CHANNEL, .password = EXAMPLE_ESP_WIFI_PASS, .max_connection = EXAMPLE_MAX_STA_CONN,.authmode=WIFI_AUTH_WPA_WPA2_PSK}, }; if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) { wifi_config.ap.authmode = WIFI_AUTH_OPEN; } ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP)); ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_start()); ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s channel:%d", EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS, EXAMPLE_ESP_WIFI_CHANNEL);}esp_ip4_addr_t ip_addr;static void udp_server_task(void *pvParameters){ // 將IPv4地址轉(zhuǎn)換為字符串格式的IP地址 char ip_addr_str[INET_ADDRSTRLEN]; inet_ntoa_r(ip_addr.addr, ip_addr_str, sizeof(ip_addr_str)); // 設(shè)置廣播地址和接收方的端口號 struct sockaddr_in broadcast_addr = { .sin_addr.s_addr = ip_addr.addr | htonl(0xFF), //廣播地址,其類型為uint32_t .sin_family = AF_INET, .sin_port = htons(BROADCAST_PORT), }; // 將廣播地址轉(zhuǎn)換為字符串 ESP_LOGI(TAG, "broadcast_addr IP: %s", inet_ntoa(broadcast_addr.sin_addr.s_addr)); // Create a socket for UDP broadcast int broadcast_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (broadcast_socket < 0) { ESP_LOGE(TAG, "Failed to create socket: %d", broadcast_socket); goto error; } // 設(shè)置套接字選項(xiàng)以啟用地址重用 int reuseEnable = 1; setsockopt(broadcast_socket, SOL_SOCKET, SO_REUSEADDR, &reuseEnable, sizeof(reuseEnable)); // 使能廣播 int broadcast_enable = 1; if (setsockopt(broadcast_socket, SOL_SOCKET, SO_BROADCAST, &broadcast_enable, sizeof(broadcast_enable)) < 0) { ESP_LOGE(TAG, "Failed to enable broadcasting"); close(broadcast_socket); goto error; } // Send the broadcast message every BROADCAST_INTERVAL_MS milliseconds while (1) { // 創(chuàng)建一個 JSON 對象,并添加三個屬性 cJSON *json = cJSON_CreateObject(); cJSON_AddStringToObject(json, "device", "EZPower"); cJSON_AddStringToObject(json, "ip", ip_addr_str); cJSON_AddNumberToObject(json, "port", 3334); // 將 JSON 數(shù)據(jù)轉(zhuǎn)換為字符串 char *json_str = cJSON_Print(json); ESP_LOGI(TAG, "Broadcasting message: %s", json_str); dwin_screen_send(json_str, strlen(json_str)); int ret = sendto(broadcast_socket, json_str, strlen(json_str), 0, (struct sockaddr *)&broadcast_addr, sizeof(broadcast_addr)); if (ret < 0) { ESP_LOGE(TAG, "Failed to send broadcast message: %d", ret); } cJSON_Delete(json); free(json_str); vTaskDelay(2000 / portTICK_PERIOD_MS); }error: vTaskDelete(NULL);}void app_main(void){ //Initialize NVS esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); } ESP_ERROR_CHECK(ret); ESP_LOGI(TAG, "ESP_WIFI_MODE_AP"); wifi_init_softap(); ip_addr.addr = 0xFF04A8C0; dwin_init(); xTaskCreate(udp_server_task, "udp_server", 4096, (void*)AF_INET, 5, NULL); }
-
開源
+關(guān)注
關(guān)注
3文章
3349瀏覽量
42501 -
功率計(jì)
+關(guān)注
關(guān)注
2文章
203瀏覽量
29831 -
智能屏幕
+關(guān)注
關(guān)注
0文章
65瀏覽量
3335
發(fā)布評論請先 登錄
相關(guān)推薦
評論