diff --git a/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino b/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino index fa183d0fbe..baccbbec24 100644 --- a/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino +++ b/libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino @@ -23,6 +23,10 @@ void loop() { const char *ssid = "ESPap"; const char *password = "thereisnospoon"; +/* List of mac address for static lease */ +uint8 mac_CAM[6] = { 0x00, 0x0C, 0x43, 0x01, 0x60, 0x15 }; +uint8 mac_PC[6] = { 0xb4, 0x52, 0x7e, 0x9a, 0x19, 0xa5 }; + ESP8266WebServer server(80); /* Set the IP Address you want for your AP */ @@ -44,7 +48,6 @@ void handleRoot() { result += String(number_client); result += "
"; while (stat_info != NULL) { - result += "Client "; result += String(i); result += " = "; @@ -53,7 +56,6 @@ void handleRoot() { sprintf(wifiClientMac, "%02X:%02X:%02X:%02X:%02X:%02X", MAC2STR(stat_info->bssid)); result += wifiClientMac; result += "
"; - stat_info = STAILQ_NEXT(stat_info, next); i++; } @@ -63,10 +65,6 @@ void handleRoot() { } void setup() { - /* List of mac address for static lease */ - uint8 mac_CAM[6] = { 0x00, 0x0C, 0x43, 0x01, 0x60, 0x15 }; - uint8 mac_PC[6] = { 0xb4, 0x52, 0x7e, 0x9a, 0x19, 0xa5 }; - Serial.begin(115200); Serial.println(); Serial.println("Configuring access point..."); @@ -75,15 +73,18 @@ void setup() { WiFi.persistent(false); WiFi.mode(WIFI_AP); + /* Configure AP with IP = 192.168.0.1 / Gateway = 192.168.0.1 / Subnet = 255.255.255.0 if you specify the ESP8266's IP-address with 192.168.0.1, the function softAPConfig() sets the DHCP-range as 192.168.0.100 - 192.168.0.200 */ WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); - /* Setup your static leases. - As it depend from your first address, and need to be done BEFORE any request from client, - this need to be specified after WiFi.softAPConfig() and before WiFi.softAP(). + /* Start Access Point. You can remove the password parameter if you want the AP to be open. */ + WiFi.softAP(ssid, password); + /* Setup your static leases. + As it depend from your first address, and need to be done BEFORE any request from client, + this need to be specified after WiFi.softAP(). first call to wifi_softap_add_dhcps_lease() will setup first IP address of the range second call to wifi_softap_add_dhcps_lease() will setup second IP address of the range ... @@ -91,10 +92,10 @@ void setup() { */ wifi_softap_add_dhcps_lease(mac_CAM); // always 192.168.0.100 wifi_softap_add_dhcps_lease(mac_PC); // always 192.168.0.101 - /* Start Access Point. You can remove the password parameter if you want the AP to be open. */ - WiFi.softAP(ssid, password); + Serial.print("AP IP address: "); Serial.println(WiFi.softAPIP()); + /* Setup HTTP Server */ server.on("/", handleRoot); server.begin(); diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index 692b214fad..0a2c393466 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -25,6 +25,13 @@ #include "ESP8266WiFi.h" #include "ESP8266WiFiGeneric.h" #include "ESP8266WiFiAP.h" +#include "lwip/def.h" + +#if LWIP_VERSION_MAJOR == 1 +#include "lwip/app/dhcpserver.h" +#else +#include "dhcpserver.h" +#endif extern "C" { #include "c_types.h" @@ -37,8 +44,6 @@ extern "C" { #include "debug.h" - - // ----------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------- Private functions ------------------------------------------------ // ----------------------------------------------------------------------------------------------------------------------- @@ -194,8 +199,10 @@ bool ESP8266WiFiAPClass::softAP(const String& ssid, const String& passphrase, in * @param local_ip access point IP * @param gateway gateway IP (0.0.0.0 to disable) * @param subnet subnet mask + * @param dhcp_start first IP assigned by DHCP + * @param dhcp_end last IP assigned by DHCP */ -bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { +bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dhcp_start, IPAddress dhcp_end) { DEBUG_WIFI("[APConfig] local_ip: %s gateway: %s subnet: %s\n", local_ip.toString().c_str(), gateway.toString().c_str(), subnet.toString().c_str()); if(!WiFi.enableAP(true)) { // enable AP failed @@ -206,6 +213,8 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA if ( !local_ip.isV4() || !subnet.isV4() + || !dhcp_start.isV4() + || !dhcp_end.isV4() #if LWIP_IPV6 // uninitialized gateway is valid || gateway.isV6() @@ -213,10 +222,39 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA ) { return false; } + + if( subnet != IPAddress(255,255,255,0)) { + DEBUG_WIFI("[APConfig] invalid netmask. only netmask \"255.255.255.0\" is valid!\n"); + return false; + } + struct ip_info info; + info.ip.addr = local_ip.v4(); info.gw.addr = gateway.v4(); info.netmask.addr = subnet.v4(); + + uint32_t softap_ip = htonl(info.ip.addr); + uint32_t start_ip = htonl(dhcp_start.v4()); + uint32_t end_ip = htonl(dhcp_end.v4()); + + if ((start_ip <= softap_ip) + || ( end_ip <= softap_ip)) { + DEBUG_WIFI("[APConfig] config ip information can't contain local ip!\n"); + return false; + } + + if ((end_ip - start_ip) > DHCPS_MAX_LEASE) { + DEBUG_WIFI("[APConfig] the value of dhcp lease is greater than \"DHCPS_MAX_LEASE\"!\n"); + return false; + } + + softap_ip >>= 8; + if ((start_ip >> 8 != softap_ip) + || (end_ip >> 8 != softap_ip)) { + DEBUG_WIFI("[APConfig] config ip information must be in the same segment as the local ip!\n"); + return false; + } if(!wifi_softap_dhcps_stop()) { DEBUG_WIFI("[APConfig] wifi_softap_dhcps_stop failed!\n"); @@ -228,13 +266,14 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA } struct dhcps_lease dhcp_lease; - IPAddress ip = local_ip; - ip[3] += 99; - dhcp_lease.start_ip.addr = ip.v4(); + dhcp_lease.enable = true; + + IPAddress ip = dhcp_start; + dhcp_lease.start_ip.addr = dhcp_start.v4(); DEBUG_WIFI("[APConfig] DHCP IP start: %s\n", ip.toString().c_str()); - ip[3] += 100; - dhcp_lease.end_ip.addr = ip.v4(); + ip = dhcp_end; + dhcp_lease.end_ip.addr = dhcp_end.v4(); DEBUG_WIFI("[APConfig] DHCP IP end: %s\n", ip.toString().c_str()); if(!wifi_softap_set_dhcps_lease(&dhcp_lease)) { @@ -277,7 +316,24 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA return ret; } +/** + * Configure access point + * @param local_ip access point IP + * @param gateway gateway IP + * @param subnet subnet mask + */ +bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { + IPAddress dhcp_start; + IPAddress dhcp_end; + + dhcp_start = local_ip; + dhcp_start[3] += 1; + dhcp_end = dhcp_start; + dhcp_end[3] += 100; + bool ret = softAPConfig(local_ip, gateway, subnet, dhcp_start, dhcp_end); + return ret; +} /** * Disconnect from the network (close AP) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h index 599c8e0e11..8781c5c57f 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h @@ -38,6 +38,7 @@ class ESP8266WiFiAPClass { bool softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0, int max_connection = 4); bool softAP(const String& ssid,const String& passphrase = emptyString,int channel = 1,int ssid_hidden = 0,int max_connection = 4); + bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dhcp_start, IPAddress dhcp_end); bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet); bool softAPdisconnect(bool wifioff = false);