@@ -32,12 +32,6 @@ const char* ap_default_psk = "esp8266esp8266"; ///< Default PSK.
32
32
// / OTA Update UDP server handle.
33
33
WiFiUDP OTA;
34
34
35
- // / Global WiFi SSID.
36
- String g_ssid = " " ;
37
-
38
- // / Global WiFi PSK.
39
- String g_pass = " " ;
40
-
41
35
42
36
/* *
43
37
* @brief Read WiFi connection information from file system.
@@ -80,10 +74,6 @@ bool loadConfig(String *ssid, String *pass)
80
74
*ssid = content.substring (0 , pos);
81
75
*pass = content.substring (pos + 2 );
82
76
83
- // Print SSID.
84
- Serial.print (" ssid: " );
85
- Serial.println (*ssid);
86
-
87
77
return true ;
88
78
} // loadConfig
89
79
@@ -205,17 +195,23 @@ static inline void ota_handle(void)
205
195
*/
206
196
void setup ()
207
197
{
208
- g_ssid = " " ;
209
- g_pass = " " ;
198
+ String station_ssid = " " ;
199
+ String station_psk = " " ;
210
200
211
201
Serial.begin (115200 );
212
202
213
203
delay (100 );
214
204
215
205
Serial.println (" \r\n " );
216
- Serial.print (" Chip ID: " );
206
+ Serial.print (" Chip ID: 0x " );
217
207
Serial.println (ESP.getChipId (), HEX);
218
208
209
+ // Set Hostname.
210
+ WiFi.hostname (HOSTNAME);
211
+ Serial.print (" hostname: " );
212
+ Serial.println (WiFi.hostname ());
213
+
214
+
219
215
// Initialize file system.
220
216
if (!SPIFFS.begin ())
221
217
{
@@ -224,44 +220,71 @@ void setup()
224
220
}
225
221
226
222
// Load wifi connection information.
227
- if (! loadConfig (&g_ssid , &g_pass ))
223
+ if (! loadConfig (&station_ssid , &station_psk ))
228
224
{
229
- g_ssid = " " ;
230
- g_pass = " " ;
225
+ station_ssid = " " ;
226
+ station_psk = " " ;
231
227
232
228
Serial.println (" No WiFi connection information available." );
233
229
}
234
230
235
- // Set Hostname.
236
- WiFi.hostname (HOSTNAME);
231
+ // Check WiFi connection
232
+ // ... check mode
233
+ if (WiFi.getMode () != WIFI_STA)
234
+ {
235
+ WiFi.mode (WIFI_STA);
236
+ delay (10 );
237
+ }
237
238
238
- Serial.println (" Wait for WiFi connection." );
239
+ // ... Load sdk config.
240
+ String ssid (WiFi.SSID ());
241
+ String psk (WiFi.psk ());
242
+
243
+ // ... Compare fiel config with sdk config.
244
+ if (ssid != station_ssid || psk != station_psk)
245
+ {
246
+ Serial.println (" WiFi config changed." );
239
247
240
- // Try to connect to WiFi AP.
241
- WiFi.mode (WIFI_STA);
242
- delay (10 );
243
- WiFi.begin (g_ssid.c_str (), g_pass.c_str ());
248
+ // ... Try to connect to WiFi station.
249
+ WiFi.begin (station_ssid.c_str (), station_psk.c_str ());
250
+
251
+ // ... Pritn new SSID
252
+ Serial.print (" new SSID: " );
253
+ Serial.println (WiFi.SSID ());
254
+
255
+ // ... Uncomment this for debugging output.
256
+ // WiFi.printDiag(Serial);
257
+ }
258
+ else
259
+ {
260
+ // ... Begin with sdk config.
261
+ WiFi.begin ();
262
+ }
263
+
264
+ Serial.println (" Wait for WiFi connection." );
244
265
245
- // Give ESP 10 seconds to connect to ap .
266
+ // ... Give ESP 10 seconds to connect to station .
246
267
unsigned long startTime = millis ();
247
268
while (WiFi.status () != WL_CONNECTED && millis () - startTime < 10000 )
248
269
{
249
270
Serial.write (' .' );
271
+ // Serial.print(WiFi.status());
250
272
delay (500 );
251
273
}
252
274
Serial.println ();
253
275
254
- // check connection
276
+ // Check connection
255
277
if (WiFi.status () == WL_CONNECTED)
256
278
{
279
+ // ... print IP Address
257
280
Serial.print (" IP address: " );
258
281
Serial.println (WiFi.localIP ());
259
282
}
260
283
else
261
284
{
262
- Serial.println (" Can not connect. Go into AP mode." );
285
+ Serial.println (" Can not connect to WiFi station . Go into AP mode." );
263
286
264
- // Go into AP mode.
287
+ // Go into software AP mode.
265
288
WiFi.mode (WIFI_AP);
266
289
267
290
delay (10 );
0 commit comments