mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-07 16:23:38 +03:00
compare wifi settings from config file with sdk config
This commit is contained in:
parent
b13095763d
commit
ec1c43b5f7
@ -32,12 +32,6 @@ const char* ap_default_psk = "esp8266esp8266"; ///< Default PSK.
|
|||||||
/// OTA Update UDP server handle.
|
/// OTA Update UDP server handle.
|
||||||
WiFiUDP OTA;
|
WiFiUDP OTA;
|
||||||
|
|
||||||
/// Global WiFi SSID.
|
|
||||||
String g_ssid = "";
|
|
||||||
|
|
||||||
/// Global WiFi PSK.
|
|
||||||
String g_pass = "";
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Read WiFi connection information from file system.
|
* @brief Read WiFi connection information from file system.
|
||||||
@ -80,10 +74,6 @@ bool loadConfig(String *ssid, String *pass)
|
|||||||
*ssid = content.substring(0, pos);
|
*ssid = content.substring(0, pos);
|
||||||
*pass = content.substring(pos + 2);
|
*pass = content.substring(pos + 2);
|
||||||
|
|
||||||
// Print SSID.
|
|
||||||
Serial.print("ssid: ");
|
|
||||||
Serial.println(*ssid);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} // loadConfig
|
} // loadConfig
|
||||||
|
|
||||||
@ -205,17 +195,23 @@ static inline void ota_handle(void)
|
|||||||
*/
|
*/
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
g_ssid = "";
|
String station_ssid = "";
|
||||||
g_pass = "";
|
String station_psk = "";
|
||||||
|
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
delay(100);
|
delay(100);
|
||||||
|
|
||||||
Serial.println("\r\n");
|
Serial.println("\r\n");
|
||||||
Serial.print("Chip ID: ");
|
Serial.print("Chip ID: 0x");
|
||||||
Serial.println(ESP.getChipId(), HEX);
|
Serial.println(ESP.getChipId(), HEX);
|
||||||
|
|
||||||
|
// Set Hostname.
|
||||||
|
WiFi.hostname(HOSTNAME);
|
||||||
|
Serial.print("hostname: ");
|
||||||
|
Serial.println(WiFi.hostname());
|
||||||
|
|
||||||
|
|
||||||
// Initialize file system.
|
// Initialize file system.
|
||||||
if (!SPIFFS.begin())
|
if (!SPIFFS.begin())
|
||||||
{
|
{
|
||||||
@ -224,44 +220,71 @@ void setup()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Load wifi connection information.
|
// Load wifi connection information.
|
||||||
if (! loadConfig(&g_ssid, &g_pass))
|
if (! loadConfig(&station_ssid, &station_psk))
|
||||||
{
|
{
|
||||||
g_ssid = "";
|
station_ssid = "";
|
||||||
g_pass = "";
|
station_psk = "";
|
||||||
|
|
||||||
Serial.println("No WiFi connection information available.");
|
Serial.println("No WiFi connection information available.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set Hostname.
|
// Check WiFi connection
|
||||||
WiFi.hostname(HOSTNAME);
|
// ... check mode
|
||||||
|
if (WiFi.getMode() != WIFI_STA)
|
||||||
|
{
|
||||||
|
WiFi.mode(WIFI_STA);
|
||||||
|
delay(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ... Load sdk config.
|
||||||
|
String ssid(WiFi.SSID());
|
||||||
|
String psk(WiFi.psk());
|
||||||
|
|
||||||
|
// ... Compare fiel config with sdk config.
|
||||||
|
if (ssid != station_ssid || psk != station_psk)
|
||||||
|
{
|
||||||
|
Serial.println("WiFi config changed.");
|
||||||
|
|
||||||
|
// ... Try to connect to WiFi station.
|
||||||
|
WiFi.begin(station_ssid.c_str(), station_psk.c_str());
|
||||||
|
|
||||||
|
// ... Pritn new SSID
|
||||||
|
Serial.print("new SSID: ");
|
||||||
|
Serial.println(WiFi.SSID());
|
||||||
|
|
||||||
|
// ... Uncomment this for debugging output.
|
||||||
|
//WiFi.printDiag(Serial);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// ... Begin with sdk config.
|
||||||
|
WiFi.begin();
|
||||||
|
}
|
||||||
|
|
||||||
Serial.println("Wait for WiFi connection.");
|
Serial.println("Wait for WiFi connection.");
|
||||||
|
|
||||||
// Try to connect to WiFi AP.
|
// ... Give ESP 10 seconds to connect to station.
|
||||||
WiFi.mode(WIFI_STA);
|
|
||||||
delay(10);
|
|
||||||
WiFi.begin(g_ssid.c_str(), g_pass.c_str());
|
|
||||||
|
|
||||||
// Give ESP 10 seconds to connect to ap.
|
|
||||||
unsigned long startTime = millis();
|
unsigned long startTime = millis();
|
||||||
while (WiFi.status() != WL_CONNECTED && millis() - startTime < 10000)
|
while (WiFi.status() != WL_CONNECTED && millis() - startTime < 10000)
|
||||||
{
|
{
|
||||||
Serial.write('.');
|
Serial.write('.');
|
||||||
|
//Serial.print(WiFi.status());
|
||||||
delay(500);
|
delay(500);
|
||||||
}
|
}
|
||||||
Serial.println();
|
Serial.println();
|
||||||
|
|
||||||
// check connection
|
// Check connection
|
||||||
if(WiFi.status() == WL_CONNECTED)
|
if(WiFi.status() == WL_CONNECTED)
|
||||||
{
|
{
|
||||||
|
// ... print IP Address
|
||||||
Serial.print("IP address: ");
|
Serial.print("IP address: ");
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Serial.println("Can not connect. Go into AP mode.");
|
Serial.println("Can not connect to WiFi station. Go into AP mode.");
|
||||||
|
|
||||||
// Go into AP mode.
|
// Go into software AP mode.
|
||||||
WiFi.mode(WIFI_AP);
|
WiFi.mode(WIFI_AP);
|
||||||
|
|
||||||
delay(10);
|
delay(10);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user