【开源获奖案例】基于T5L智能屏的便携功率计解决方案

——来自迪文开发者论坛

 

本期为大家推送迪文开发者论坛获奖开源案例——基于T5L智能屏的便携功率计解决方案。该方案通过智能屏串口进行通信,实时接收监测到的电流、电压和功率数据,并同步显示在屏幕上,使用户可以直观地监测电气参数状态。此外,该方案还支持WiFi联网,无论是手机、PC还是其他移动设备,用户均可轻松访问电气数据,大幅提升了数据管理的便捷性与灵活性。适用于电气工程、智能家居和工业自动化等领域。

     

    【演示视频】

完整开发资料含迪文屏DGUS工程资料与C51代码,获取方式:

1. 前往迪文开发者论坛获取:http://inforum.dwin.com.cn:20080/forum.php?mod=viewthread&tid=10600&extra=&_dsign=e227a7c6

2. 微信公众号中回复“便携功率计”获取。


【UI素材展示】

12

%E5%9B%BE%E7%89%873%E5%9B%BE%E7%89%874



GUI工程设计

GUI工程设计

程序代码设计

主控板将I2C采集的电流、电压和功率数据,通过串口发送给迪文智能屏,同时通过UDP将数据发送至上位机,部分参考代码如下:

static void wifi_event_handler(void* arg, esp_event_base_t event_base,int32_t event_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);

    }

    else if (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_t wifi_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地址转换为字符串格式的IP地址

    char ip_addr_str[INET_ADDRSTRLEN];

    inet_ntoa_r(ip_addr.addr, ip_addr_str, sizeof(ip_addr_str));

    // 设置广播地址和接收方的端口号

    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),

    };

     // 将广播地址转换为字符串

    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;

    }

    // 设置套接字选项以启用地址重用

    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) {

    // 创建一个 JSON 对象,并添加三个属性

    cJSON *json = cJSON_CreateObject();

    cJSON_AddStringToObject(json, "device", "EZPower");

    cJSON_AddStringToObject(json, "ip", ip_addr_str);

    cJSON_AddNumberToObject(json, "port", 3334);

    // 将 JSON 数据转换为字符串

    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);

}