|
| 1 | +/* Create a WiFi access point and provide static lease */ |
| 2 | + |
| 3 | +#include <ESP8266WiFi.h> |
| 4 | +#include <WiFiClient.h> |
| 5 | +#include <ESP8266WebServer.h> |
| 6 | + |
| 7 | +/* Set these to your desired credentials. */ |
| 8 | +const char *ssid = "ESPap"; |
| 9 | +const char *password = "thereisnospoon"; |
| 10 | + |
| 11 | +ESP8266WebServer server(80); |
| 12 | + |
| 13 | +/* Set the IP Address you want for your AP */ |
| 14 | +IPAddress apIP(192, 168, 0, 1); |
| 15 | + |
| 16 | +/* Go to http://192.168.0.1 in a web browser to see current lease */ |
| 17 | +void handleRoot() { |
| 18 | + String result; |
| 19 | + char wifiClientMac[18]; |
| 20 | + unsigned char number_client; |
| 21 | + struct station_info *stat_info; |
| 22 | + struct ip4_addr *IPaddress; |
| 23 | + |
| 24 | + IPAddress address; |
| 25 | + int i = 1; |
| 26 | + |
| 27 | + number_client = wifi_softap_get_station_num(); |
| 28 | + stat_info = wifi_softap_get_station_info(); |
| 29 | + |
| 30 | + result = "<html><body><h1>Total Connected Clients : "; |
| 31 | + result += String(number_client); |
| 32 | + result += "</h1></br>"; |
| 33 | + while (stat_info != NULL) { |
| 34 | + IPaddress = &stat_info->ip; |
| 35 | + address = IPaddress->addr; |
| 36 | + |
| 37 | + result += "Client "; |
| 38 | + result += String(i); |
| 39 | + result += " = "; |
| 40 | + result += String(address.toString()); |
| 41 | + result += " - "; |
| 42 | + sprintf(wifiClientMac, "%02X:%02X:%02X:%02X:%02X:%02X", MAC2STR(stat_info->bssid)); |
| 43 | + result += wifiClientMac; |
| 44 | + result += "</br>"; |
| 45 | + |
| 46 | + stat_info = STAILQ_NEXT(stat_info, next); |
| 47 | + i++; |
| 48 | + } |
| 49 | + result = result + "</body></html>"; |
| 50 | + |
| 51 | + server.send(200, "text/html", result); |
| 52 | +} |
| 53 | + |
| 54 | +void setup() { |
| 55 | + /* List of mac address for static lease */ |
| 56 | + uint8 mac_CAM[6] = { 0x00, 0x0C, 0x43, 0x01, 0x60, 0x15 }; |
| 57 | + uint8 mac_PC[6] = { 0xb4, 0x52, 0x7e, 0x9a, 0x19, 0xa5 }; |
| 58 | + |
| 59 | + Serial.begin(115200); |
| 60 | + Serial.println(); |
| 61 | + Serial.println("Configuring access point..."); |
| 62 | + |
| 63 | + /* Disable the WiFi persistence to avoid any re-configuration that may erase static lease when starting softAP */ |
| 64 | + WiFi.persistent(false); |
| 65 | + |
| 66 | + WiFi.mode(WIFI_AP); |
| 67 | + /* Configure AP with IP = 192.168.0.1 / Gateway = 192.168.0.1 / Subnet = 255.255.255.0 |
| 68 | + 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 |
| 69 | + */ |
| 70 | + WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); |
| 71 | + /* Setup your static leases. |
| 72 | +
|
| 73 | + As it depend from your first address, and need to be done BEFORE any request from client, |
| 74 | + this need to be specified after WiFi.softAPConfig() and before WiFi.softAP(). |
| 75 | +
|
| 76 | + first call to wifi_softap_add_dhcps_lease() will setup first IP address of the range |
| 77 | + second call to wifi_softap_add_dhcps_lease() will setup second IP address of the range |
| 78 | + ... |
| 79 | + any client not listed will use next IP address available from the range (here 192.168.0.102 and more) |
| 80 | + */ |
| 81 | + wifi_softap_add_dhcps_lease(mac_CAM); // always 192.168.0.100 |
| 82 | + wifi_softap_add_dhcps_lease(mac_PC); // always 192.168.0.101 |
| 83 | + /* Start Access Point. You can remove the password parameter if you want the AP to be open. */ |
| 84 | + WiFi.softAP(ssid, password); |
| 85 | + Serial.print("AP IP address: "); |
| 86 | + Serial.println(WiFi.softAPIP()); |
| 87 | + /* Setup HTTP Server */ |
| 88 | + server.on("/", handleRoot); |
| 89 | + server.begin(); |
| 90 | + Serial.println("HTTP server started"); |
| 91 | +} |
| 92 | + |
| 93 | +void loop() { |
| 94 | + server.handleClient(); |
| 95 | +} |
0 commit comments