@@ -85,19 +85,24 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
85
85
86
86
if (!WiFi.enableAP (true )) {
87
87
// enable AP failed
88
+ DEBUG_WIFI (" [AP] enableAP failed!\n " );
88
89
return false ;
89
90
}
90
91
91
92
if (!ssid || *ssid == 0 || strlen (ssid) > 31 ) {
92
93
// fail SSID too long or missing!
94
+ DEBUG_WIFI (" [AP] SSID too long or missing!\n " );
93
95
return false ;
94
96
}
95
97
96
98
if (passphrase && (strlen (passphrase) > 63 || strlen (passphrase) < 8 )) {
97
99
// fail passphrase to long or short!
100
+ DEBUG_WIFI (" [AP] fail passphrase to long or short!\n " );
98
101
return false ;
99
102
}
100
103
104
+ bool ret = true ;
105
+
101
106
struct softap_config conf;
102
107
strcpy (reinterpret_cast <char *>(conf.ssid ), ssid);
103
108
conf.channel = channel;
@@ -116,20 +121,50 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
116
121
117
122
struct softap_config conf_current;
118
123
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 " );
122
141
}
123
142
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
+ }
125
150
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
+ }
129
164
} else {
130
- ret = wifi_softap_set_config_current (&conf);
165
+ DEBUG_WIFI (" [AP] wifi_get_ip_info failed!\n " );
166
+ ret = false ;
131
167
}
132
- ETS_UART_INTR_ENABLE ();
133
168
134
169
return ret;
135
170
}
@@ -142,21 +177,76 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
142
177
* @param subnet subnet mask
143
178
*/
144
179
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 ());
146
181
if (!WiFi.enableAP (true )) {
147
182
// enable AP failed
183
+ DEBUG_WIFI (" [APConfig] enableAP failed!\n " );
148
184
return false ;
149
185
}
186
+ bool ret = true ;
150
187
151
188
struct ip_info info;
152
189
info.ip .addr = static_cast <uint32_t >(local_ip);
153
190
info.gw .addr = static_cast <uint32_t >(gateway);
154
191
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 ;
158
200
}
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 (" [APConfig] IP config Invalid?!\n " );
238
+ ret = false ;
239
+ } else if (local_ip != info.ip .addr ) {
240
+ ip = info.ip .addr ;
241
+ DEBUG_WIFI (" [APConfig] IP config not set correct?! new IP: %s\n " , ip.toString ().c_str ());
242
+ ret = false ;
243
+ }
244
+ } else {
245
+ DEBUG_WIFI (" [APConfig] wifi_get_ip_info failed!\n " );
246
+ ret = false ;
247
+ }
248
+
249
+ return ret;
160
250
}
161
251
162
252
@@ -179,6 +269,10 @@ bool ESP8266WiFiAPClass::softAPdisconnect(bool wifioff) {
179
269
}
180
270
ETS_UART_INTR_ENABLE ();
181
271
272
+ if (!ret) {
273
+ DEBUG_WIFI (" [APdisconnect] set_config failed!\n " );
274
+ }
275
+
182
276
if (wifioff) {
183
277
ret = WiFi.enableAP (false );
184
278
}
0 commit comments