From 17186286e6fd6122f8193c98a6fed26a11ebb131 Mon Sep 17 00:00:00 2001 From: Juraj Andrassy Date: Wed, 3 Jul 2024 20:22:01 +0200 Subject: [PATCH] Ethernet and WiFi - fix DNS for static IP interface name is only valid after first net_if.connect --- libraries/Ethernet/src/Ethernet.cpp | 4 +++- libraries/WiFi/src/WiFi.cpp | 10 ++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/libraries/Ethernet/src/Ethernet.cpp b/libraries/Ethernet/src/Ethernet.cpp index 0f6450a7a..ab720fcaf 100644 --- a/libraries/Ethernet/src/Ethernet.cpp +++ b/libraries/Ethernet/src/Ethernet.cpp @@ -55,11 +55,13 @@ int arduino::EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPA eth_if->set_dhcp(false); eth_if->set_network(_ip, _netmask, _gateway); + + auto ret = _begin(mac, timeout, responseTimeout); + char if_name[5]; eth_if->get_interface_name(if_name); eth_if->add_dns_server(_dnsServer1, if_name); - auto ret = _begin(mac, timeout, responseTimeout); return ret; } diff --git a/libraries/WiFi/src/WiFi.cpp b/libraries/WiFi/src/WiFi.cpp index 09e8c6e56..7ea892876 100644 --- a/libraries/WiFi/src/WiFi.cpp +++ b/libraries/WiFi/src/WiFi.cpp @@ -66,16 +66,18 @@ int arduino::WiFiClass::begin(const char* ssid, const char* passphrase, wl_enc_t wifi_if->set_dhcp(!_useStaticIP); if (_useStaticIP) { wifi_if->set_network(_ip, _netmask, _gateway); - char if_name[5]; - wifi_if->get_interface_name(if_name); - wifi_if->add_dns_server(_dnsServer2, if_name); - wifi_if->add_dns_server(_dnsServer1, if_name); // pushes dnsServer2 at index 1 } nsapi_error_t result = wifi_if->connect(ssid, passphrase, _security); if(result == NSAPI_ERROR_IS_CONNECTED) { wifi_if->disconnect(); + } else + if (_useStaticIP) { + char if_name[5]; + wifi_if->get_interface_name(if_name); + wifi_if->add_dns_server(_dnsServer2, if_name); + wifi_if->add_dns_server(_dnsServer1, if_name); // pushes dnsServer2 at index 1 } _currentNetworkStatus = (result == NSAPI_ERROR_OK && setSSID(ssid)) ? WL_CONNECTED : WL_CONNECT_FAILED;