mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-04 18:03:20 +03:00
Add capability to have light static DHCP lease (#5594)
* Add capability to have light static DHCP lease * added ESP8266WiFi StaticLease sample * Update StaticLease to IPv4
This commit is contained in:
parent
ab9d4ad57b
commit
5e4c2e9750
95
libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino
Normal file
95
libraries/ESP8266WiFi/examples/StaticLease/StaticLease.ino
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
/* Create a WiFi access point and provide static lease */
|
||||||
|
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <WiFiClient.h>
|
||||||
|
#include <ESP8266WebServer.h>
|
||||||
|
|
||||||
|
/* Set these to your desired credentials. */
|
||||||
|
const char *ssid = "ESPap";
|
||||||
|
const char *password = "thereisnospoon";
|
||||||
|
|
||||||
|
ESP8266WebServer server(80);
|
||||||
|
|
||||||
|
/* Set the IP Address you want for your AP */
|
||||||
|
IPAddress apIP(192, 168, 0, 1);
|
||||||
|
|
||||||
|
/* Go to http://192.168.0.1 in a web browser to see current lease */
|
||||||
|
void handleRoot() {
|
||||||
|
String result;
|
||||||
|
char wifiClientMac[18];
|
||||||
|
unsigned char number_client;
|
||||||
|
struct station_info *stat_info;
|
||||||
|
struct ip4_addr *IPaddress;
|
||||||
|
|
||||||
|
IPAddress address;
|
||||||
|
int i = 1;
|
||||||
|
|
||||||
|
number_client = wifi_softap_get_station_num();
|
||||||
|
stat_info = wifi_softap_get_station_info();
|
||||||
|
|
||||||
|
result = "<html><body><h1>Total Connected Clients : ";
|
||||||
|
result += String(number_client);
|
||||||
|
result += "</h1></br>";
|
||||||
|
while (stat_info != NULL) {
|
||||||
|
IPaddress = &stat_info->ip;
|
||||||
|
address = IPaddress->addr;
|
||||||
|
|
||||||
|
result += "Client ";
|
||||||
|
result += String(i);
|
||||||
|
result += " = ";
|
||||||
|
result += String(address.toString());
|
||||||
|
result += " - ";
|
||||||
|
sprintf(wifiClientMac, "%02X:%02X:%02X:%02X:%02X:%02X", MAC2STR(stat_info->bssid));
|
||||||
|
result += wifiClientMac;
|
||||||
|
result += "</br>";
|
||||||
|
|
||||||
|
stat_info = STAILQ_NEXT(stat_info, next);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
result = result + "</body></html>";
|
||||||
|
|
||||||
|
server.send(200, "text/html", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
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...");
|
||||||
|
|
||||||
|
/* Disable the WiFi persistence to avoid any re-configuration that may erase static lease when starting softAP */
|
||||||
|
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().
|
||||||
|
|
||||||
|
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
|
||||||
|
...
|
||||||
|
any client not listed will use next IP address available from the range (here 192.168.0.102 and more)
|
||||||
|
*/
|
||||||
|
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();
|
||||||
|
Serial.println("HTTP server started");
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
server.handleClient();
|
||||||
|
}
|
@ -386,6 +386,8 @@ uint32 wifi_softap_get_dhcps_lease_time(void);
|
|||||||
bool wifi_softap_set_dhcps_lease_time(uint32 minute);
|
bool wifi_softap_set_dhcps_lease_time(uint32 minute);
|
||||||
bool wifi_softap_reset_dhcps_lease_time(void);
|
bool wifi_softap_reset_dhcps_lease_time(void);
|
||||||
|
|
||||||
|
bool wifi_softap_add_dhcps_lease(uint8 *macaddr); // add static lease on the list, this will be the next available @
|
||||||
|
|
||||||
enum dhcp_status wifi_softap_dhcps_status(void);
|
enum dhcp_status wifi_softap_dhcps_status(void);
|
||||||
bool wifi_softap_set_dhcps_offer_option(uint8 level, void* optarg);
|
bool wifi_softap_set_dhcps_offer_option(uint8 level, void* optarg);
|
||||||
|
|
||||||
|
@ -104,6 +104,7 @@ void ICACHE_FLASH_ATTR node_remove_from_list(list_node **phead, list_node* pdele
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////
|
||||||
/*
|
/*
|
||||||
* <EFBFBD><EFBFBD>DHCP msg<EFBFBD><EFBFBD>Ϣ<EFBFBD>ṹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
* <EFBFBD><EFBFBD>DHCP msg<EFBFBD><EFBFBD>Ϣ<EFBFBD>ṹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user