Skip to content

Commit 5cbff32

Browse files
committed
compare wifi settings from config file with sdk config
1 parent e179a01 commit 5cbff32

File tree

1 file changed

+50
-27
lines changed

1 file changed

+50
-27
lines changed

hardware/esp8266com/esp8266/libraries/ESP8266mDNS/examples/OTA-mDNS-SPIFFS/OTA-mDNS-SPIFFS.ino

+50-27
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ const char* ap_default_psk = "esp8266esp8266"; ///< Default PSK.
3232
/// OTA Update UDP server handle.
3333
WiFiUDP OTA;
3434

35-
/// Global WiFi SSID.
36-
String g_ssid = "";
37-
38-
/// Global WiFi PSK.
39-
String g_pass = "";
40-
4135

4236
/**
4337
* @brief Read WiFi connection information from file system.
@@ -80,10 +74,6 @@ bool loadConfig(String *ssid, String *pass)
8074
*ssid = content.substring(0, pos);
8175
*pass = content.substring(pos + 2);
8276

83-
// Print SSID.
84-
Serial.print("ssid: ");
85-
Serial.println(*ssid);
86-
8777
return true;
8878
} // loadConfig
8979

@@ -205,17 +195,23 @@ static inline void ota_handle(void)
205195
*/
206196
void setup()
207197
{
208-
g_ssid = "";
209-
g_pass = "";
198+
String station_ssid = "";
199+
String station_psk = "";
210200

211201
Serial.begin(115200);
212202

213203
delay(100);
214204

215205
Serial.println("\r\n");
216-
Serial.print("Chip ID: ");
206+
Serial.print("Chip ID: 0x");
217207
Serial.println(ESP.getChipId(), HEX);
218208

209+
// Set Hostname.
210+
WiFi.hostname(HOSTNAME);
211+
Serial.print("hostname: ");
212+
Serial.println(WiFi.hostname());
213+
214+
219215
// Initialize file system.
220216
if (!SPIFFS.begin())
221217
{
@@ -224,44 +220,71 @@ void setup()
224220
}
225221

226222
// Load wifi connection information.
227-
if (! loadConfig(&g_ssid, &g_pass))
223+
if (! loadConfig(&station_ssid, &station_psk))
228224
{
229-
g_ssid = "";
230-
g_pass = "";
225+
station_ssid = "";
226+
station_psk = "";
231227

232228
Serial.println("No WiFi connection information available.");
233229
}
234230

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+
}
237238

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.");
239247

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.");
244265

245-
// Give ESP 10 seconds to connect to ap.
266+
// ... Give ESP 10 seconds to connect to station.
246267
unsigned long startTime = millis();
247268
while (WiFi.status() != WL_CONNECTED && millis() - startTime < 10000)
248269
{
249270
Serial.write('.');
271+
//Serial.print(WiFi.status());
250272
delay(500);
251273
}
252274
Serial.println();
253275

254-
// check connection
276+
// Check connection
255277
if(WiFi.status() == WL_CONNECTED)
256278
{
279+
// ... print IP Address
257280
Serial.print("IP address: ");
258281
Serial.println(WiFi.localIP());
259282
}
260283
else
261284
{
262-
Serial.println("Can not connect. Go into AP mode.");
285+
Serial.println("Can not connect to WiFi station. Go into AP mode.");
263286

264-
// Go into AP mode.
287+
// Go into software AP mode.
265288
WiFi.mode(WIFI_AP);
266289

267290
delay(10);

0 commit comments

Comments
 (0)