diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp index 136a60300c..43f44153ac 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp @@ -195,6 +195,14 @@ wl_status_t ESP8266WiFiSTAClass::begin() { return status(); } +static void +swap(IPAddress &lhs, IPAddress &rhs) +{ + IPAddress tmp = lhs; + lhs = rhs; + rhs = tmp; +} + /** * Change IP configuration settings disabling the dhcp client @@ -210,6 +218,22 @@ bool ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddres return false; } + //Arduino has a different arg order: ip, dns, gateway, subnet. To allow compatibility, check first octet of 3rd arg. If 255, interpret as ESP order, otherwise Arduino order. + if(subnet[0] != 255) + { + //octet is not 255 => interpret as Arduino order + + if(dns1[0] == 0) + { + //arg order is arduino and 4th arg not given => assign it arduino default + dns1 = IPAddress(255,255,255,0); + } + + //current order is arduino: ip-dns-gway-subnet + swap(gateway, subnet); //after this, order is ip-gway-dns-subnet + swap(subnet, dns1); //after this, order is ip-gway-subnet-dns (correct ESP order) + } + struct ip_info info; info.ip.addr = static_cast(local_ip); info.gw.addr = static_cast(gateway); diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h index 4e87f8826e..3de34f353e 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h @@ -39,6 +39,9 @@ class ESP8266WiFiSTAClass { wl_status_t begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true); wl_status_t begin(); + //The argument order for ESP is not the same as for Arduino. However, there is compatibility code under the hood + //to detect Arduino arg order, and handle it correctly. Be aware that the Arduino default value handling doesn't + //work here (see Arduino docs for gway/subnet defaults). In other words: at least 3 args must always be given. bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000); bool reconnect();