Skip to content

Commit 5e4c2e9

Browse files
SmartBlugearlephilhower
authored andcommitted
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
1 parent ab9d4ad commit 5e4c2e9

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
}

tools/sdk/include/user_interface.h

+2
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,8 @@ uint32 wifi_softap_get_dhcps_lease_time(void);
386386
bool wifi_softap_set_dhcps_lease_time(uint32 minute);
387387
bool wifi_softap_reset_dhcps_lease_time(void);
388388

389+
bool wifi_softap_add_dhcps_lease(uint8 *macaddr); // add static lease on the list, this will be the next available @
390+
389391
enum dhcp_status wifi_softap_dhcps_status(void);
390392
bool wifi_softap_set_dhcps_offer_option(uint8 level, void* optarg);
391393

tools/sdk/lwip/src/app/dhcpserver.c

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ void ICACHE_FLASH_ATTR node_remove_from_list(list_node **phead, list_node* pdele
104104
}
105105
}
106106
}
107+
107108
///////////////////////////////////////////////////////////////////////////////////
108109
/*
109110
* ��DHCP msg��Ϣ�ṹ����������

0 commit comments

Comments
 (0)