@@ -67,6 +67,13 @@ void handleJSData()
67
67
server.send (200 , " text/plain" , " " );
68
68
}
69
69
70
+
71
+ void stopDriver (){
72
+ analogWrite (PWMA, 0 );
73
+ analogWrite (PWMB, 0 );
74
+ }
75
+
76
+
70
77
struct wifiConfig get_id_pass (String filepath)
71
78
{
72
79
wifiConfig ap;
@@ -119,15 +126,6 @@ void writeToFile(String id, String pass)
119
126
temp.println (pass);
120
127
temp.close ();
121
128
}
122
- // if (filecheck(SAPfile))
123
- // {
124
- // SPIFFS.remove(SAPfile);
125
- // if (filecheck(t_location)) {
126
- //
127
- // SPIFFS.rename(t_location, SAPfile);
128
- // }
129
- //
130
- // }
131
129
SPIFFS.gc ();
132
130
133
131
}
@@ -145,54 +143,54 @@ void getSapdata()
145
143
146
144
}
147
145
148
- void sapHome ()
146
+ void apconnect ()
149
147
{
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)
156
149
{
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
+ {
159
158
159
+ if (WiFi.getMode () != WIFI_STA) {
160
+ server.stop ();
161
+ set_stamode ();
162
+ beginWebserver (1 );
160
163
}
164
+
161
165
}
162
166
163
167
void beginWebserver (bool mode_flag)
164
168
{
165
- if (mode_flag == 1 )
166
- {
167
- server.serveStatic (" /" , SPIFFS, " /home.html" );
168
169
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 ( );
171
172
172
- }
173
- else
174
- {
173
+ server.serveStatic (" /" , SPIFFS, " /home.html" );
175
174
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);
177
177
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" );
180
179
181
- }
182
- server.serveStatic (" /mode/" , SPIFFS, " /mode.html" );
180
+ server.serveStatic (" /ap_home/" , SPIFFS, " /ap_home.html" );
183
181
184
- server.serveStatic (" /ap_home/" , SPIFFS, " /ap_home.html" );
185
- server.serveStatic (" /ap_home/view/" , SPIFFS, " /displayap.html" );
186
182
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" );
190
185
191
186
187
+ server.serveStatic (" /sap_home/" , SPIFFS, " /sap_home.html" );
188
+ server.serveStatic (" /sap_home/form/" , SPIFFS, " /form.html" );
192
189
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);
196
194
197
195
198
196
server.serveStatic (" /control/" , SPIFFS, " /joystick.html" );
@@ -204,11 +202,10 @@ void beginWebserver(bool mode_flag)
204
202
}
205
203
206
204
bool filecheck (String filepath) {
207
- File check = SPIFFS.open (filepath, " r" );
208
- return check;
205
+ return SPIFFS.exists (filepath);
209
206
}
210
207
211
- void STA_mode (struct wifiConfig apSettings)
208
+ void AP_mode (struct wifiConfig apSettings)
212
209
{
213
210
214
211
WiFi.mode (WIFI_OFF);
@@ -230,13 +227,15 @@ void STA_mode(struct wifiConfig apSettings)
230
227
Serial.print (" AP IP address = " );
231
228
Serial.println (WiFi.softAPIP ());
232
229
}
230
+
233
231
void wait_for_dns () {
234
232
while (1 ) {
235
233
delay (1000 );
236
234
Serial.println (" [ERROR] MDNS responder did not setup for ap mode " );
237
235
}
238
236
239
237
}
238
+
240
239
void set_dns ()
241
240
{
242
241
@@ -245,21 +244,91 @@ void set_dns()
245
244
Serial.println (" [ERROR] MDNS responder did not setup for ap mode " );
246
245
wait_for_dns ();
247
246
}
247
+ else Serial.println (" DNS server successfully created for AP mode" );
248
+
248
249
MDNS.addService (" http" , " tcp" , 80 );
249
250
}
250
251
else {
251
252
if (!MDNS.begin (" wirelessmd" )) {
252
253
Serial.println (" [ERROR] MDNS responder did not setup STA mode" );
253
254
wait_for_dns ();
254
255
}
256
+ else Serial.println (" DNS server successfully created for STA mode" );
255
257
}
256
258
}
257
259
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 ()
259
283
{
260
284
struct wifiConfig apSettings;
261
285
bool mode_flag = 0 ;
262
286
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 ;
263
332
WiFi.mode (WIFI_OFF);
264
333
Serial.begin (115200 );
265
334
delay (2000 );
@@ -275,58 +344,14 @@ void setup()
275
344
}
276
345
277
346
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" );
279
348
}
280
349
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
- }
309
350
351
+ mode_flag = set_stamode ();
310
352
}
311
353
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
- }
327
354
328
- set_dns ();
329
- initGpio ();
330
355
beginWebserver (mode_flag);
331
356
}
332
357
0 commit comments