Skip to content

Commit

Permalink
Fixed issue with incorrect configuration on first start
Browse files Browse the repository at this point in the history
  • Loading branch information
snakeye committed Apr 9, 2019
1 parent d13d9fc commit 1dc1e67
Showing 1 changed file with 39 additions and 11 deletions.
50 changes: 39 additions & 11 deletions src/WiFiConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ void ConfigManager::handleGetWifi()
}

/**
* Scan networks
* @brief Scan WiFi networks
*
*/
void ConfigManager::handleGetWifiScan()
{
Expand Down Expand Up @@ -172,6 +173,10 @@ void ConfigManager::handleGetWifiScan()
server->send(200, FPSTR(mimeJSON), body);
}

/**
* @brief Connect to the Access Point
*
*/
void ConfigManager::handlePostConnect()
{
bool isJson = server->header("Content-Type") == FPSTR(mimeJSON);
Expand Down Expand Up @@ -320,43 +325,66 @@ bool ConfigManager::wifiConnected()

void ConfigManager::setup()
{
char magic[2];
char ssid[32];
char password[64];
char magic[2] = {0};
char ssid[32] = {0};
char password[64] = {0};

//
Serial.println(F("Reading saved configuration"));

EEPROM.get(0, magic);
EEPROM.get(WIFI_OFFSET, ssid);
EEPROM.get(WIFI_OFFSET + 32, password);

readConfig();

if (memcmp(magic, magicBytes, 2) == 0)
{
// config read sucessfully
EEPROM.get(WIFI_OFFSET, ssid);
EEPROM.get(WIFI_OFFSET + 32, password);

readConfig();

Serial.println(F("Config read successfully"));
}
else
{
// config is incorrect, save default one
EEPROM.put(0, magicBytes);
EEPROM.put(WIFI_OFFSET, ssid);
EEPROM.put(WIFI_OFFSET + 32, password);

writeConfig();

Serial.println(F("Config incorrect, overwriting"));
}

// try to connect to access point
if (ssid[0] != '\0')
{
WiFi.begin(ssid, password[0] == '\0' ? NULL : password);

delay(1000);

if (wifiConnected())
{
Serial.print(F("Connected to "));
Serial.print(ssid);
Serial.print(F(" with "));
Serial.print(F(" with IP "));
Serial.println(WiFi.localIP());

WiFi.mode(WIFI_STA);

startApi();
}
}
else

// could not connect to AP or there is no configuration for it
if(WiFi.status() != WL_CONNECTED)
{
// We are at a cold start, don't bother timeing out.
apTimeout = 0;

startAP();
}

//
startWebServer();
}

Expand Down

0 comments on commit 1dc1e67

Please sign in to comment.