Skip to content

Commit

Permalink
compare wifi settings from config file with sdk config
Browse files Browse the repository at this point in the history
  • Loading branch information
pgollor committed Sep 28, 2015
1 parent e179a01 commit 5cbff32
Showing 1 changed file with 50 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ const char* ap_default_psk = "esp8266esp8266"; ///< Default PSK.
/// OTA Update UDP server handle.
WiFiUDP OTA;

/// Global WiFi SSID.
String g_ssid = "";

/// Global WiFi PSK.
String g_pass = "";


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

// Print SSID.
Serial.print("ssid: ");
Serial.println(*ssid);

return true;
} // loadConfig

Expand Down Expand Up @@ -205,17 +195,23 @@ static inline void ota_handle(void)
*/
void setup()
{
g_ssid = "";
g_pass = "";
String station_ssid = "";
String station_psk = "";

Serial.begin(115200);

delay(100);

Serial.println("\r\n");
Serial.print("Chip ID: ");
Serial.print("Chip ID: 0x");
Serial.println(ESP.getChipId(), HEX);

// Set Hostname.
WiFi.hostname(HOSTNAME);
Serial.print("hostname: ");
Serial.println(WiFi.hostname());


// Initialize file system.
if (!SPIFFS.begin())
{
Expand All @@ -224,44 +220,71 @@ void setup()
}

// Load wifi connection information.
if (! loadConfig(&g_ssid, &g_pass))
if (! loadConfig(&station_ssid, &station_psk))
{
g_ssid = "";
g_pass = "";
station_ssid = "";
station_psk = "";

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

// Set Hostname.
WiFi.hostname(HOSTNAME);
// Check WiFi connection
// ... check mode
if (WiFi.getMode() != WIFI_STA)
{
WiFi.mode(WIFI_STA);
delay(10);
}

Serial.println("Wait for WiFi connection.");
// ... 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 AP.
WiFi.mode(WIFI_STA);
delay(10);
WiFi.begin(g_ssid.c_str(), g_pass.c_str());
// ... 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.");

// Give ESP 10 seconds to connect to ap.
// ... Give ESP 10 seconds to connect to station.
unsigned long startTime = millis();
while (WiFi.status() != WL_CONNECTED && millis() - startTime < 10000)
{
Serial.write('.');
//Serial.print(WiFi.status());
delay(500);
}
Serial.println();

// check connection
// Check connection
if(WiFi.status() == WL_CONNECTED)
{
// ... print IP Address
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
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);

delay(10);
Expand Down

0 comments on commit 5cbff32

Please sign in to comment.