Skip to content

Commit

Permalink
Ethernet and WiFi - fix DNS for static IP
Browse files Browse the repository at this point in the history
interface name is only valid after first net_if.connect
  • Loading branch information
JAndrassy committed Jul 3, 2024
1 parent 2ece915 commit 1718628
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 3 additions & 1 deletion libraries/Ethernet/src/Ethernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
10 changes: 6 additions & 4 deletions libraries/WiFi/src/WiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 1718628

Please sign in to comment.