From ec5ed309a30faff3178da688bcbe16bdeefa7221 Mon Sep 17 00:00:00 2001 From: Yilin Sun Date: Sun, 14 May 2023 11:04:16 +0800 Subject: [PATCH] Use unique interface name. Signed-off-by: Yilin Sun --- main/app_wifi.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/main/app_wifi.c b/main/app_wifi.c index 17ff252..811071d 100644 --- a/main/app_wifi.c +++ b/main/app_wifi.c @@ -14,6 +14,9 @@ #define APP_WIFI_EVENT_GROUP_EVENT_CONNECTED (1 << 0U) #define APP_WIFI_EVENT_GROUP_EVENT_FAILED (1 << 1U) + +#define APP_NETIF_NAME_PREFIX "LilyGo_EPD47" + #define APP_LOG_TAG "APP_WIFI" EventGroupHandle_t g_app_wifi_event_group; @@ -30,11 +33,20 @@ int app_wifi_init(void) { ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); - esp_netif_create_default_wifi_sta(); + + esp_netif_t *sta_if = esp_netif_create_default_wifi_sta(); wifi_init_config_t wifi_init_cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_cfg)); + char hostname_buf[64]; + uint8_t mac_addr[6]; + esp_wifi_get_mac(WIFI_IF_STA, mac_addr); + + snprintf(hostname_buf, 64, APP_NETIF_NAME_PREFIX "_%02X%02X%02X", mac_addr[3], mac_addr[4], mac_addr[5]); + + esp_netif_set_hostname(sta_if, hostname_buf); + esp_event_handler_instance_t inst_any_id; esp_event_handler_instance_t inst_got_ip; @@ -63,7 +75,7 @@ int app_wifi_init(void) { } int app_wifi_is_ready(void) { - if(xEventGroupGetBits(g_app_wifi_event_group) & APP_WIFI_EVENT_GROUP_EVENT_CONNECTED) { + if (xEventGroupGetBits(g_app_wifi_event_group) & APP_WIFI_EVENT_GROUP_EVENT_CONNECTED) { return 1; } @@ -71,7 +83,8 @@ int app_wifi_is_ready(void) { } int app_wifi_wait_ready(uint32_t msec) { - if(xEventGroupWaitBits(g_app_wifi_event_group, APP_WIFI_EVENT_GROUP_EVENT_CONNECTED, 0U, 0U, pdMS_TO_TICKS(msec)) == pdTRUE) { + if (xEventGroupWaitBits(g_app_wifi_event_group, APP_WIFI_EVENT_GROUP_EVENT_CONNECTED, 0U, 0U, + pdMS_TO_TICKS(msec)) == pdTRUE) { return 0; } @@ -98,14 +111,14 @@ static void app_wifi_event_handler(void *arg, esp_event_base_t event_base, int32 ESP_LOGW(APP_LOG_TAG, "Connection lost, maximum retries reached."); } } - } else if(event_base == IP_EVENT) { - if(event_id == IP_EVENT_STA_GOT_IP) { + } else if (event_base == IP_EVENT) { + if (event_id == IP_EVENT_STA_GOT_IP) { xEventGroupSetBits(g_app_wifi_event_group, APP_WIFI_EVENT_GROUP_EVENT_CONNECTED); s_retries = 0U; ip_event_got_ip_t *event = event_data; - ESP_LOGI(APP_LOG_TAG, "Connected, IP address: "IPSTR, IP2STR(&event->ip_info.ip)); + ESP_LOGI(APP_LOG_TAG, "Connected, IP address: " IPSTR, IP2STR(&event->ip_info.ip)); } } }