Add RSSI and enhanced network info to WiFi connection logs

Show signal strength (RSSI in dBm) when connected to AP
Show gateway and netmask in addition to IP address
Useful for debugging WiFi connectivity in multi-device deployments
This commit is contained in:
Bob 2025-11-09 21:01:11 +00:00
parent a0e29973e1
commit 1654753bbb
1 changed files with 14 additions and 2 deletions

View File

@ -32,6 +32,15 @@ static void event_handler(void* arg, esp_event_base_t event_base,
{ {
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
esp_wifi_connect(); esp_wifi_connect();
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED) {
wifi_event_sta_connected_t* event = (wifi_event_sta_connected_t*) event_data;
// Get RSSI
wifi_ap_record_t ap_info;
esp_wifi_sta_get_ap_info(&ap_info);
ESP_LOGI(TAG, "Connected to AP SSID:%s, BSSID:" MACSTR " Channel:%d RSSI:%d dBm",
event->ssid, MAC2STR(event->bssid), event->channel, ap_info.rssi);
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
if (s_retry_num < WIFI_MAXIMUM_RETRY) { if (s_retry_num < WIFI_MAXIMUM_RETRY) {
esp_wifi_connect(); esp_wifi_connect();
@ -43,7 +52,10 @@ static void event_handler(void* arg, esp_event_base_t event_base,
ESP_LOGI(TAG,"connect to the AP fail"); ESP_LOGI(TAG,"connect to the AP fail");
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); ESP_LOGI(TAG, "got ip:" IPSTR " gw:" IPSTR " netmask:" IPSTR,
IP2STR(&event->ip_info.ip),
IP2STR(&event->ip_info.gw),
IP2STR(&event->ip_info.netmask));
s_retry_num = 0; s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
} }
@ -110,7 +122,7 @@ void wifi_init_sta(void)
portMAX_DELAY); portMAX_DELAY);
if (bits & WIFI_CONNECTED_BIT) { if (bits & WIFI_CONNECTED_BIT) {
ESP_LOGI(TAG, "connected to ap SSID:%s", WIFI_SSID); ESP_LOGI(TAG, "WiFi connection successful");
} else if (bits & WIFI_FAIL_BIT) { } else if (bits & WIFI_FAIL_BIT) {
ESP_LOGI(TAG, "Failed to connect to SSID:%s", WIFI_SSID); ESP_LOGI(TAG, "Failed to connect to SSID:%s", WIFI_SSID);
} else { } else {