Skip to content

Commit

Permalink
fix(wlan): Fix IP config output (DHCP) + Enable connection retry (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
Slider0007 authored Apr 19, 2024
1 parent 026c650 commit 869013d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
40 changes: 23 additions & 17 deletions code/components/jomjol_wlan/connect_wlan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,11 +533,23 @@ static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_
WIFIConnected = true;
WIFIReconnectCnt = 0;

ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
esp_ip4addr_ntoa(&event->ip_info.ip, &wlan_config.ipaddress[0], IP4ADDR_STRLEN_MAX);
esp_ip4addr_ntoa(&event->ip_info.netmask, &wlan_config.netmask[0], IP4ADDR_STRLEN_MAX);
esp_ip4addr_ntoa(&event->ip_info.gw, &wlan_config.gateway[0], IP4ADDR_STRLEN_MAX);
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Assigned IP: " + wlan_config.ipaddress);
if (wlan_config.dhcp) {
char buf[20];
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;

wlan_config.ipaddress = std::string(esp_ip4addr_ntoa(&event->ip_info.ip, buf, sizeof(buf)));
wlan_config.netmask = std::string(esp_ip4addr_ntoa(&event->ip_info.netmask, buf, sizeof(buf)));
wlan_config.gateway = std::string(esp_ip4addr_ntoa(&event->ip_info.gw, buf, sizeof(buf)));

esp_netif_dns_info_t dns_info;
esp_netif_get_dns_info(event->esp_netif, ESP_NETIF_DNS_MAIN, &dns_info);
wlan_config.dns = std::string(esp_ip4addr_ntoa((const esp_ip4_addr_t*)&dns_info.ip, buf, sizeof(buf)));
}

LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Assigned IP: " + wlan_config.ipaddress +
", Netmask: " + wlan_config.netmask +
", Gateway: " + wlan_config.gateway +
", DNS: " + wlan_config.dns);

#ifdef ENABLE_MQTT
if (getMQTTisEnabled()) {
Expand All @@ -564,8 +576,6 @@ esp_err_t wifi_init_sta(void)
}

my_sta = esp_netif_create_default_wifi_sta();

esp_netif_dns_info_t dns_info;

if (!wlan_config.ipaddress.empty() && !wlan_config.netmask.empty() && !wlan_config.gateway.empty())
{
Expand All @@ -592,7 +602,8 @@ esp_err_t wifi_init_sta(void)
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "No DNS address set, use gateway address as DNS");
wlan_config.dns = wlan_config.gateway;
}


esp_netif_dns_info_t dns_info;
dns_info.ip.u_addr.ip4.addr = esp_ip4addr_aton(wlan_config.dns.c_str());

retval = esp_netif_set_dns_info(my_sta, ESP_NETIF_DNS_MAIN, &dns_info);
Expand All @@ -601,17 +612,12 @@ esp_err_t wifi_init_sta(void)
return retval;
}

esp_ip4addr_ntoa((const esp_ip4_addr_t*)&dns_info.ip, &wlan_config.dns[0], IP4ADDR_STRLEN_MAX);
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Manual interface config | IP: " + wlan_config.ipaddress +
", Netmask: " + wlan_config.netmask +
", Gateway: " + wlan_config.gateway +
", DNS: " + wlan_config.dns);
wlan_config.dhcp = false;
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Use static network config");
}
else {
wlan_config.dhcp = true;
esp_ip4addr_ntoa((const esp_ip4_addr_t*)&dns_info.ip, &wlan_config.dns[0], IP4ADDR_STRLEN_MAX);

LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Automatic interface config | Use DHCP service");
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Use DHCP provided network config");
}

wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
Expand Down Expand Up @@ -648,7 +654,7 @@ esp_err_t wifi_init_sta(void)

wifi_config.sta.scan_method = WIFI_ALL_CHANNEL_SCAN; // Scan all channels instead of stopping after first match
wifi_config.sta.sort_method = WIFI_CONNECT_AP_BY_SIGNAL; // Sort by signal strength and keep up to 4 best APs
//wifi_config.sta.failure_retry_cnt = 3; // IDF version 5.0 will support this
wifi_config.sta.failure_retry_cnt = 5; // Number of connection retries station will do before moving to next AP

#ifdef WLAN_USE_MESH_ROAMING
wifi_config.sta.rm_enabled = 1; // 802.11k (Radio Resource Management)
Expand Down
4 changes: 2 additions & 2 deletions code/main/server_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ esp_err_t handler_get_info(httpd_req_t *req)
retVal = ESP_FAIL;
if (cJSON_AddStringToObject(cJSONObject, "mac_address", getMac().c_str()) == NULL)
retVal = ESP_FAIL;
if (cJSON_AddStringToObject(cJSONObject, "network_config", getDHCPUsage() ? "DHCP" : "Manual") == NULL)
if (cJSON_AddStringToObject(cJSONObject, "network_config", getDHCPUsage() ? "DHCP" : "Static") == NULL)
retVal = ESP_FAIL;
if (cJSON_AddStringToObject(cJSONObject, "ipv4_address", getIPAddress().c_str()) == NULL)
retVal = ESP_FAIL;
Expand Down Expand Up @@ -294,7 +294,7 @@ esp_err_t handler_get_info(httpd_req_t *req)
return ESP_OK;
}
else if (type.compare("network_config") == 0) {
httpd_resp_sendstr(req, getDHCPUsage() ? "DHCP" : "Manual");
httpd_resp_sendstr(req, getDHCPUsage() ? "DHCP" : "Static");
return ESP_OK;
}
else if (type.compare("ipv4_address") == 0) {
Expand Down
2 changes: 1 addition & 1 deletion docs/API/REST/info.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ The following infos are available:
| `wlan_ssid` | WLAN SSID | `IOTNETWORK`
| `wlan_rssi` | WLAN Signal Strength [dBm] | `-58`
| `mac_address` | Device MAC Address | `41:23:D9:01:5E:AA`
| `network_config` | Network Configuration<br><br>Possible states:<br>- `DHCP`<br>- `Manual`| `DHCP`
| `network_config` | Network Configuration<br><br>Possible states:<br>- `DHCP`<br>- `Static`| `DHCP`
| `ipv4_address` | Device IPv4 Address | `192.168.1.4`
| `netmask_address` | Device Netmask Address | `255.255.255.0`
| `gateway_address` | Device Gateway Address | `192.168.1.1`
Expand Down
4 changes: 2 additions & 2 deletions sd-card/wlan.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
;++++++++++++++++++++++++++++++++++
; AI on the edge - WLAN configuration
; AI on the Edge device - WLAN configuration
;++++++++++++++++++++++++++++++++++
; ssid: Name of WLAN network (mandatory), e.g. "WLAN-SSID"
; password: Password of WLAN network (optional), e.g. "PASSWORD"
Expand All @@ -18,8 +18,8 @@ password = ""
; parameters needs to be configured: ip, gateway, netmask are mandatory, dns optional

;ip = "xxx.xxx.xxx.xxx"
;gateway = "xxx.xxx.xxx.xxx"
;netmask = "xxx.xxx.xxx.xxx"
;gateway = "xxx.xxx.xxx.xxx"

; DNS server (optional, if no DNS is configured, gateway address will be used)

Expand Down

0 comments on commit 869013d

Please sign in to comment.