@@ -67,6 +67,13 @@ void handleJSData()
6767 server.send (200 , " text/plain" , " " );
6868}
6969
70+
71+ void stopDriver (){
72+ analogWrite (PWMA, 0 );
73+ analogWrite (PWMB, 0 );
74+ }
75+
76+
7077struct wifiConfig get_id_pass (String filepath)
7178{
7279 wifiConfig ap;
@@ -119,15 +126,6 @@ void writeToFile(String id, String pass)
119126 temp.println (pass);
120127 temp.close ();
121128 }
122- // if (filecheck(SAPfile))
123- // {
124- // SPIFFS.remove(SAPfile);
125- // if (filecheck(t_location)) {
126- //
127- // SPIFFS.rename(t_location, SAPfile);
128- // }
129- //
130- // }
131129 SPIFFS.gc ();
132130
133131}
@@ -145,54 +143,54 @@ void getSapdata()
145143
146144}
147145
148- void sapHome ()
146+ void apconnect ()
149147{
150-
151- struct wifiConfig wifiSettings;
152- String reset_controller = server.arg (0 );
153-
154-
155- if (reset_controller == " yes" )
148+ if (WiFi.getMode () != WIFI_AP)
156149 {
157- server.send (200 , " text/plain" , " " );
158- ESP.restart ();
150+ server.stop ();
151+ set_apmode ();
152+ beginWebserver (1 );
153+ }
154+ }
155+
156+ void staconnect ()
157+ {
159158
159+ if (WiFi.getMode () != WIFI_STA) {
160+ server.stop ();
161+ set_stamode ();
162+ beginWebserver (1 );
160163 }
164+
161165}
162166
163167void beginWebserver (bool mode_flag)
164168{
165- if (mode_flag == 1 )
166- {
167- server.serveStatic (" /" , SPIFFS, " /home.html" );
168169
169- // call handleJSData function when this URL is accessed by the js in the html file
170- server. on ( " /home.html " , reset_controller );
170+ set_dns ();
171+ initGpio ( );
171172
172- }
173- else
174- {
173+ server.serveStatic (" /" , SPIFFS, " /home.html" );
175174
176- server.serveStatic (" /" , SPIFFS, " /homeap.html" );
175+ // call handleJSData function when this URL is accessed by the js in the html file
176+ server.on (" /home.html" , reset_controller);
177177
178- // call handleJSData function when this URL is accessed by the js in the html file
179- server.on (" /homeap.html" , reset_controller);
178+ server.serveStatic (" /mode/" , SPIFFS, " /mode.html" );
180179
181- }
182- server.serveStatic (" /mode/" , SPIFFS, " /mode.html" );
180+ server.serveStatic (" /ap_home/" , SPIFFS, " /ap_home.html" );
183181
184- server.serveStatic (" /ap_home/" , SPIFFS, " /ap_home.html" );
185- server.serveStatic (" /ap_home/view/" , SPIFFS, " /displayap.html" );
186182
187- server.serveStatic (" /sap_home/" , SPIFFS, " /sap_home.html" );
188- server.serveStatic (" /sap_home/form/" , SPIFFS, " /form.html" );
189- server.serveStatic (" /sap_home/view/" , SPIFFS, " /displaysap.html" );
183+ server.serveStatic (" /ap_home/view/" , SPIFFS, " /displayap.html" );
184+ server.serveStatic (" /sap_home/view/" , SPIFFS, " /displaysap.html" );
190185
191186
187+ server.serveStatic (" /sap_home/" , SPIFFS, " /sap_home.html" );
188+ server.serveStatic (" /sap_home/form/" , SPIFFS, " /form.html" );
192189
193- server.on (" /ap_home/ap_home.html" , sapHome);
194- server.on (" /sap_home/form/form.html" , getSapdata);
195- server.on (" /sap_home/sap_home.html" , sapHome);
190+
191+ server.on (" /ap_home/ap_home.html" , apconnect);
192+ server.on (" /sap_home/form/form.html" , getSapdata);
193+ server.on (" /sap_home/sap_home.html" , staconnect);
196194
197195
198196 server.serveStatic (" /control/" , SPIFFS, " /joystick.html" );
@@ -204,11 +202,10 @@ void beginWebserver(bool mode_flag)
204202}
205203
206204bool filecheck (String filepath) {
207- File check = SPIFFS.open (filepath, " r" );
208- return check;
205+ return SPIFFS.exists (filepath);
209206}
210207
211- void STA_mode (struct wifiConfig apSettings)
208+ void AP_mode (struct wifiConfig apSettings)
212209{
213210
214211 WiFi.mode (WIFI_OFF);
@@ -230,13 +227,15 @@ void STA_mode(struct wifiConfig apSettings)
230227 Serial.print (" AP IP address = " );
231228 Serial.println (WiFi.softAPIP ());
232229}
230+
233231void wait_for_dns () {
234232 while (1 ) {
235233 delay (1000 );
236234 Serial.println (" [ERROR] MDNS responder did not setup for ap mode " );
237235 }
238236
239237}
238+
240239void set_dns ()
241240{
242241
@@ -245,21 +244,91 @@ void set_dns()
245244 Serial.println (" [ERROR] MDNS responder did not setup for ap mode " );
246245 wait_for_dns ();
247246 }
247+ else Serial.println (" DNS server successfully created for AP mode" );
248+
248249 MDNS.addService (" http" , " tcp" , 80 );
249250 }
250251 else {
251252 if (!MDNS.begin (" wirelessmd" )) {
252253 Serial.println (" [ERROR] MDNS responder did not setup STA mode" );
253254 wait_for_dns ();
254255 }
256+ else Serial.println (" DNS server successfully created for STA mode" );
255257 }
256258}
257259
258- void setup ()
260+
261+ void set_apmode ()
262+ {
263+ struct wifiConfig apSettings;
264+
265+ if (!filecheck (APfile) )
266+ {
267+
268+ Serial.println (" failed to open AP and SAP settings defaulting ssid and password" );
269+ apSettings.ssid = " ESP8266" ;
270+ apSettings.password = " Helloworld" ;
271+ AP_mode (apSettings);
272+ }
273+ else
274+ {
275+ apSettings = get_id_pass (APfile);
276+ AP_mode (apSettings);
277+ }
278+
279+ }
280+
281+
282+ bool set_stamode ()
259283{
260284 struct wifiConfig apSettings;
261285 bool mode_flag = 0 ;
262286 int retries = 30 ;
287+
288+ Serial.println (" running ESP in SoftAP mode" );
289+ apSettings = get_id_pass (SAPfile);
290+ Serial.print (" connecting ESP to:" );
291+ Serial.print (apSettings.ssid );
292+ Serial.print (" PASSWORD:" );
293+ Serial.print (apSettings.password );
294+
295+ delay (2000 );
296+
297+ WiFi.mode (WIFI_STA);
298+
299+ WiFi.setAutoConnect (true );
300+ WiFi.setAutoReconnect (true ); // if wifi attempts to (re)connect to a previous router it kills the access point
301+
302+
303+ // WiFi.begin(id,pass); //Connect to your WiFi router
304+ WiFi.begin (apSettings.ssid .c_str (), apSettings.password .c_str ()); // Connect to your WiFi router
305+ while (WiFi.status () != WL_CONNECTED && retries > 0 ) {
306+ delay (1000 );
307+ Serial.print (" ." );
308+ retries -= 1 ;
309+ }
310+ Serial.println ();
311+ if (retries == 0 ) {
312+ Serial.println (" failed to connect to network in SoftAp mode chnaging to ap mode" );
313+ }
314+ else {
315+ mode_flag = 1 ;
316+ Serial.println (" WiFi connected" );
317+ Serial.println (" IP address: " );
318+ Serial.println (WiFi.localIP ());
319+ }
320+ // set ap mode if sta mode is failed
321+ if (WiFi.status () != WL_CONNECTED) {
322+ set_apmode ();
323+ }
324+
325+ return mode_flag;
326+ }
327+
328+
329+ void setup ()
330+ {
331+ bool mode_flag = 0 ;
263332 WiFi.mode (WIFI_OFF);
264333 Serial.begin (115200 );
265334 delay (2000 );
@@ -275,58 +344,14 @@ void setup()
275344 }
276345
277346 if (!filecheck (SAPfile)) {
278- Serial.println (" failed to open SoftAP chnaging mode to AP" );
347+ Serial.println (" failed to open SoftAP changing mode to AP" );
279348 }
280349 else {
281- Serial.println (" running ESP in SoftAP mode" );
282- apSettings = get_id_pass (SAPfile);
283- Serial.print (" connecting ESP to:" );
284- Serial.print (apSettings.ssid );
285- Serial.print (" PASSWORD:" );
286- Serial.print (apSettings.password );
287-
288- delay (2000 );
289-
290- WiFi.mode (WIFI_STA);
291-
292- // WiFi.begin(id,pass); //Connect to your WiFi router
293- WiFi.begin (apSettings.ssid .c_str (), apSettings.password .c_str ()); // Connect to your WiFi router
294- while (WiFi.status () != WL_CONNECTED && retries > 0 ) {
295- delay (1000 );
296- Serial.print (" ." );
297- retries -= 1 ;
298- }
299- Serial.println ();
300- if (retries == 0 ) {
301- Serial.println (" failed to connect to network in SoftAp mode chnaging to ap mode" );
302- }
303- else {
304- mode_flag = 1 ;
305- Serial.println (" WiFi connected" );
306- Serial.println (" IP address: " );
307- Serial.println (WiFi.localIP ());
308- }
309350
351+ mode_flag = set_stamode ();
310352 }
311353
312- if (WiFi.status () != WL_CONNECTED) {
313- if (!filecheck (APfile) )
314- {
315-
316- Serial.println (" failed to open AP and SAP settings defaulting ssid and password" );
317- apSettings.ssid = " ESP8266" ;
318- apSettings.password = " Helloworld" ;
319- STA_mode (apSettings);
320- }
321- else
322- {
323- apSettings = get_id_pass (APfile);
324- STA_mode (apSettings);
325- }
326- }
327354
328- set_dns ();
329- initGpio ();
330355 beginWebserver (mode_flag);
331356}
332357
0 commit comments