@@ -85,22 +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!" );
88+ DEBUG_WIFI (" [AP] enableAP failed!\n " );
8989 return false ;
9090 }
9191
9292 if (!ssid || *ssid == 0 || strlen (ssid) > 31 ) {
9393 // fail SSID too long or missing!
94- DEBUG_WIFI (" [AP] SSID too long or missing!" );
94+ DEBUG_WIFI (" [AP] SSID too long or missing!\n " );
9595 return false ;
9696 }
9797
9898 if (passphrase && (strlen (passphrase) > 63 || strlen (passphrase) < 8 )) {
9999 // fail passphrase to long or short!
100- DEBUG_WIFI (" [AP] fail passphrase to long or short!" );
100+ DEBUG_WIFI (" [AP] fail passphrase to long or short!\n " );
101101 return false ;
102102 }
103103
104+ bool ret = false ;
105+
104106 struct softap_config conf;
105107 strcpy (reinterpret_cast <char *>(conf.ssid ), ssid);
106108 conf.channel = channel;
@@ -119,24 +121,51 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
119121
120122 struct softap_config conf_current;
121123 wifi_softap_get_config (&conf_current);
122- if (softap_config_equal (conf, conf_current)) {
123- DEBUG_WIFI (" [AP] softap config unchanged" );
124- return true ;
125- }
124+ if (!softap_config_equal (conf, conf_current)) {
126125
127- bool ret;
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+ }
128138
129- ETS_UART_INTR_DISABLE ();
130- if (WiFi._persistent ) {
131- ret = wifi_softap_set_config (&conf);
132139 } else {
133- ret = wifi_softap_set_config_current (&conf );
140+ DEBUG_WIFI ( " [AP] softap config unchanged \n " );
134141 }
135- ETS_UART_INTR_ENABLE ();
136142
137- if (!ret) {
138- DEBUG_WIFI (" [AP] set_config faild!" );
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+ }
150+
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+ }
164+ } else {
165+ DEBUG_WIFI (" [AP] wifi_get_ip_info failed!\n " );
166+ ret = false ;
139167 }
168+
140169 return ret;
141170}
142171
@@ -148,24 +177,76 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
148177 * @param subnet subnet mask
149178 */
150179bool ESP8266WiFiAPClass::softAPConfig (IPAddress local_ip, IPAddress gateway, IPAddress subnet) {
151-
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 ());
152181 if (!WiFi.enableAP (true )) {
153182 // enable AP failed
154- DEBUG_WIFI (" [AP ] enableAP failed!" );
183+ DEBUG_WIFI (" [APConfig ] enableAP failed!\n " );
155184 return false ;
156185 }
186+ bool ret = true ;
157187
158188 struct ip_info info;
159189 info.ip .addr = static_cast <uint32_t >(local_ip);
160190 info.gw .addr = static_cast <uint32_t >(gateway);
161191 info.netmask .addr = static_cast <uint32_t >(subnet);
162- wifi_softap_dhcps_stop ();
163- if (wifi_set_ip_info (SOFTAP_IF, &info)) {
164- 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 ;
200+ }
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+ }
165244 } else {
166- DEBUG_WIFI (" [AP] wifi_set_ip_info failed!" );
245+ DEBUG_WIFI (" [AP] wifi_get_ip_info failed!\n " );
246+ ret = false ;
167247 }
168- return false ;
248+
249+ return ret;
169250}
170251
171252
@@ -188,6 +269,10 @@ bool ESP8266WiFiAPClass::softAPdisconnect(bool wifioff) {
188269 }
189270 ETS_UART_INTR_ENABLE ();
190271
272+ if (!ret) {
273+ DEBUG_WIFI (" [APdisconnect] set_config failed!\n " );
274+ }
275+
191276 if (wifioff) {
192277 ret = WiFi.enableAP (false );
193278 }
0 commit comments