Skip to content

Commit bf3d572

Browse files
committed
fix: previously incorrect IP address retrieved from the API
1 parent 3046434 commit bf3d572

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

main/webserver/api/v1/v1.c

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,12 @@ esp_err_t status_handler(httpd_req_t *req) {
2020
char uptime_str[64];
2121
snprintf(uptime_str, sizeof(uptime_str), "%dd %02dh %02dm %02ds", days, hours, minutes, seconds);
2222

23-
esp_netif_ip_info_t ip_info;
24-
char ip_str[INET_ADDRSTRLEN] = "unknown";
25-
26-
if (ap_netif == NULL) {
27-
if (sta_netif && esp_netif_get_ip_info(sta_netif, &ip_info) == ESP_OK) {
28-
snprintf(ip_str, sizeof(ip_str), IPSTR, IP2STR(&ip_info.ip));
29-
}
30-
} else {
31-
if (ap_netif && esp_netif_get_ip_info(ap_netif, &ip_info) == ESP_OK) {
32-
snprintf(ip_str, sizeof(ip_str), IPSTR, IP2STR(&ip_info.ip));
33-
}
34-
}
35-
3623
time_t now;
3724
time(&now);
3825

3926
json_entry_t entries[] = {{"status", JSON_TYPE_STRING, "ok"},
4027
{"sys_timestamp", JSON_TYPE_NUMBER, &now},
41-
{"ip", JSON_TYPE_STRING, ip_str},
28+
{"ip", JSON_TYPE_STRING, wifi_get_current_ip_str()},
4229
{"free_heap", JSON_TYPE_STRING, free_heap_str},
4330
{"uptime", JSON_TYPE_STRING, uptime_str}};
4431

main/wireless/wifi/wifi.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ static void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t e
4040
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
4141
captive_portal_stop_dns_server();
4242

43-
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
44-
char ip_str[16];
45-
snprintf(ip_str, sizeof(ip_str), IPSTR, IP2STR(&event->ip_info.ip));
46-
43+
const char *ip_str = wifi_get_current_ip_str();
4744
ESP_LOGI(TAG, "Connected successfully with IP : %s", ip_str);
4845

4946
ui_clear_main_container();
@@ -247,4 +244,21 @@ const char *wifi_resolve_domain(char *domain_name) {
247244
ESP_LOGW(TAG, "No IPs returned for %s", domain_name);
248245
return NULL;
249246
}
247+
}
248+
249+
/// @brief Return the current ip as a string or NULL
250+
/// @param void
251+
/// @return NULL|char ip
252+
const char *wifi_get_current_ip_str(void) {
253+
static char ip_str[INET_ADDRSTRLEN] = "0.0.0.0";
254+
255+
if (sta_netif) {
256+
esp_netif_ip_info_t ip_info;
257+
if (esp_netif_get_ip_info(sta_netif, &ip_info) == ESP_OK && ip_info.ip.addr != 0) {
258+
snprintf(ip_str, sizeof(ip_str), IPSTR, IP2STR(&ip_info.ip));
259+
return ip_str;
260+
}
261+
}
262+
263+
return NULL;
250264
}

main/wireless/wifi/wifi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ esp_err_t wifi_scan_networks(wifi_ap_record_t **results, uint16_t *ap_count);
2525
uint16_t wifi_get_scan_results(wifi_ap_record_t **results);
2626
bool wifi_start_sta(const char *ssid, const char *password);
2727
const char *wifi_resolve_domain(char *domain_name);
28+
const char *wifi_get_current_ip_str(void);

0 commit comments

Comments
 (0)