Skip to content

Commit 4107424

Browse files
committed
Merge pull request #1389 from Links2004/debug
add more debug to WiFi and rework AP config
2 parents b5341bb + 29bb74b commit 4107424

File tree

3 files changed

+121
-16
lines changed

3 files changed

+121
-16
lines changed

libraries/ESP8266WiFi/src/ESP8266WiFi.h

+11
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ extern "C" {
4040
#include "WiFiServer.h"
4141
#include "WiFiClientSecure.h"
4242

43+
#ifdef DEBUG_ESP_WIFI
44+
#ifdef DEBUG_ESP_PORT
45+
#define DEBUG_WIFI(...) DEBUG_ESP_PORT.printf( __VA_ARGS__ )
46+
#endif
47+
#endif
48+
49+
#ifndef DEBUG_WIFI
50+
#define DEBUG_WIFI(...)
51+
#endif
52+
53+
4354
class ESP8266WiFiClass : public ESP8266WiFiGenericClass, public ESP8266WiFiSTAClass, public ESP8266WiFiScanClass, public ESP8266WiFiAPClass {
4455
public:
4556

libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp

+108-14
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,24 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
8585

8686
if(!WiFi.enableAP(true)) {
8787
// enable AP failed
88+
DEBUG_WIFI("[AP] enableAP failed!\n");
8889
return false;
8990
}
9091

9192
if(!ssid || *ssid == 0 || strlen(ssid) > 31) {
9293
// fail SSID too long or missing!
94+
DEBUG_WIFI("[AP] SSID too long or missing!\n");
9395
return false;
9496
}
9597

9698
if(passphrase && (strlen(passphrase) > 63 || strlen(passphrase) < 8)) {
9799
// fail passphrase to long or short!
100+
DEBUG_WIFI("[AP] fail passphrase to long or short!\n");
98101
return false;
99102
}
100103

104+
bool ret = false;
105+
101106
struct softap_config conf;
102107
strcpy(reinterpret_cast<char*>(conf.ssid), ssid);
103108
conf.channel = channel;
@@ -116,20 +121,50 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
116121

117122
struct softap_config conf_current;
118123
wifi_softap_get_config(&conf_current);
119-
if(softap_config_equal(conf, conf_current)) {
120-
DEBUGV("softap config unchanged");
121-
return true;
124+
if(!softap_config_equal(conf, conf_current)) {
125+
126+
ETS_UART_INTR_DISABLE();
127+
if(WiFi._persistent) {
128+
ret = wifi_softap_set_config(&conf);
129+
} else {
130+
ret = wifi_softap_set_config_current(&conf);
131+
}
132+
ETS_UART_INTR_ENABLE();
133+
134+
if(!ret) {
135+
DEBUG_WIFI("[AP] set_config failed!\n");
136+
return false;
137+
}
138+
139+
} else {
140+
DEBUG_WIFI("[AP] softap config unchanged\n");
122141
}
123142

124-
bool ret;
143+
if(wifi_softap_dhcps_status() != DHCP_STARTED) {
144+
DEBUG_WIFI("[AP] DHCP not started, starting...\n");
145+
if(!wifi_softap_dhcps_start()) {
146+
DEBUG_WIFI("[AP] wifi_softap_dhcps_start failed!\n");
147+
ret = false;
148+
}
149+
}
125150

126-
ETS_UART_INTR_DISABLE();
127-
if(WiFi._persistent) {
128-
ret = wifi_softap_set_config(&conf);
151+
// check IP config
152+
struct ip_info ip;
153+
if(wifi_get_ip_info(SOFTAP_IF, &ip)) {
154+
if(ip.ip.addr == 0x00000000) {
155+
// Invalid config
156+
DEBUG_WIFI("[AP] IP config Invalid resetting...\n");
157+
//192.168.244.1 , 192.168.244.1 , 255.255.255.0
158+
ret = softAPConfig(0x01F4A8C0, 0x01F4A8C0, 0x00FFFFFF);
159+
if(!ret) {
160+
DEBUG_WIFI("[AP] softAPConfig failed!\n");
161+
ret = false;
162+
}
163+
}
129164
} else {
130-
ret = wifi_softap_set_config_current(&conf);
165+
DEBUG_WIFI("[AP] wifi_get_ip_info failed!\n");
166+
ret = false;
131167
}
132-
ETS_UART_INTR_ENABLE();
133168

134169
return ret;
135170
}
@@ -142,21 +177,76 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
142177
* @param subnet subnet mask
143178
*/
144179
bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) {
145-
180+
DEBUG_WIFI("[APConfig] local_ip: %s gateway: %s subnet: %s\n", local_ip.toString().c_str(), gateway.toString().c_str(), subnet.toString().c_str());
146181
if(!WiFi.enableAP(true)) {
147182
// enable AP failed
183+
DEBUG_WIFI("[APConfig] enableAP failed!\n");
148184
return false;
149185
}
186+
bool ret = true;
150187

151188
struct ip_info info;
152189
info.ip.addr = static_cast<uint32_t>(local_ip);
153190
info.gw.addr = static_cast<uint32_t>(gateway);
154191
info.netmask.addr = static_cast<uint32_t>(subnet);
155-
wifi_softap_dhcps_stop();
156-
if(wifi_set_ip_info(SOFTAP_IF, &info)) {
157-
return wifi_softap_dhcps_start();
192+
193+
if(!wifi_softap_dhcps_stop()) {
194+
DEBUG_WIFI("[APConfig] wifi_softap_dhcps_stop failed!\n");
195+
}
196+
197+
if(!wifi_set_ip_info(SOFTAP_IF, &info)) {
198+
DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n");
199+
ret = false;
158200
}
159-
return false;
201+
202+
struct dhcps_lease dhcp_lease;
203+
IPAddress ip = local_ip;
204+
ip[3] += 99;
205+
dhcp_lease.start_ip.addr = static_cast<uint32_t>(ip);
206+
DEBUG_WIFI("[APConfig] DHCP IP start: %s\n", ip.toString().c_str());
207+
208+
ip[3] += 100;
209+
dhcp_lease.end_ip.addr = static_cast<uint32_t>(ip);
210+
DEBUG_WIFI("[APConfig] DHCP IP end: %s\n", ip.toString().c_str());
211+
212+
if(!wifi_softap_set_dhcps_lease(&dhcp_lease)) {
213+
DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n");
214+
ret = false;
215+
}
216+
217+
// set lease time to 720min --> 12h
218+
if(!wifi_softap_set_dhcps_lease_time(720)) {
219+
DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_lease_time failed!\n");
220+
ret = false;
221+
}
222+
223+
uint8 mode = 1;
224+
if(!wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &mode)) {
225+
DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_offer_option failed!\n");
226+
ret = false;
227+
}
228+
229+
if(!wifi_softap_dhcps_start()) {
230+
DEBUG_WIFI("[APConfig] wifi_softap_dhcps_start failed!\n");
231+
ret = false;
232+
}
233+
234+
// check config
235+
if(wifi_get_ip_info(SOFTAP_IF, &info)) {
236+
if(info.ip.addr == 0x00000000) {
237+
DEBUG_WIFI("[AP] IP config Invalid?!\n");
238+
ret = false;
239+
} else if(local_ip != info.ip.addr) {
240+
ip = info.ip.addr;
241+
DEBUG_WIFI("[AP] IP config not set correct?! new IP: %s\n", ip.toString().c_str());
242+
ret = false;
243+
}
244+
} else {
245+
DEBUG_WIFI("[AP] wifi_get_ip_info failed!\n");
246+
ret = false;
247+
}
248+
249+
return ret;
160250
}
161251

162252

@@ -179,6 +269,10 @@ bool ESP8266WiFiAPClass::softAPdisconnect(bool wifioff) {
179269
}
180270
ETS_UART_INTR_ENABLE();
181271

272+
if(!ret) {
273+
DEBUG_WIFI("[APdisconnect] set_config failed!\n");
274+
}
275+
182276
if(wifioff) {
183277
ret = WiFi.enableAP(false);
184278
}

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ void ESP8266WiFiGenericClass::removeEvent(WiFiEventCb cbEvent, WiFiEvent_t event
103103
*/
104104
void ESP8266WiFiGenericClass::_eventCallback(void* arg) {
105105
System_Event_t* event = reinterpret_cast<System_Event_t*>(arg);
106-
DEBUGV("wifi evt: %d\n", event->event);
106+
DEBUG_WIFI("wifi evt: %d\n", event->event);
107107

108108
if(event->event == EVENT_STAMODE_DISCONNECTED) {
109-
DEBUGV("STA disconnect: %d\n", event->event_info.disconnected.reason);
109+
DEBUG_WIFI("STA disconnect: %d\n", event->event_info.disconnected.reason);
110110
WiFiClient::stopAll();
111111
}
112112

0 commit comments

Comments
 (0)