Skip to content

Commit

Permalink
Added factor reset when holding the button down for 10 seconds, OpenE…
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremypoulter committed May 12, 2018
1 parent 021f1bd commit 507c05c
Showing 1 changed file with 58 additions and 16 deletions.
74 changes: 58 additions & 16 deletions src/wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ unsigned long wifiLedTimeOut = millis();
#define WIFI_BUTTON 0
#endif

#ifndef WIFI_BUTTON_TIMEOUT
#define WIFI_BUTTON_TIMEOUT 5 * 1000
#ifndef WIFI_BUTTON_AP_TIMEOUT
#define WIFI_BUTTON_AP_TIMEOUT (5 * 1000)
#endif

#ifndef WIFI_BUTTON_FACTORY_RESET_TIMEOUT
#define WIFI_BUTTON_FACTORY_RESET_TIMEOUT (10 * 1000)
#endif

#ifndef WIFI_CLIENT_RETRY_TIMEOUT
Expand All @@ -63,6 +67,7 @@ unsigned long wifiLedTimeOut = millis();

int wifiButtonState = HIGH;
unsigned long wifiButtonTimeOut = millis();
bool apMessage = false;

// -------------------------------------------------------------------
// Start Access Point
Expand Down Expand Up @@ -237,7 +242,8 @@ wifi_setup() {
}

void
wifi_loop() {
wifi_loop()
{
Profile_Start(wifi_loop);

bool isClient = wifi_mode_is_sta();
Expand All @@ -257,24 +263,60 @@ wifi_loop() {
}
#endif

// Manage state while connecting
if(isClientOnly && !WiFi.isConnected())
#if defined(WIFI_LED) && WIFI_BUTTON == WIFI_LED
digitalWrite(WIFI_BUTTON, HIGH);
pinMode(WIFI_BUTTON, INPUT_PULLUP);
#endif

// Pressing the boot button for 5 seconds will turn on AP mode, 10 seconds will factory reset
int button = digitalRead(WIFI_BUTTON);

#if defined(WIFI_LED) && WIFI_BUTTON == WIFI_LED
pinMode(WIFI_BUTTON, OUTPUT);
digitalWrite(WIFI_LED, wifiLedState);
#endif

//DBUGF("%lu %d %d", millis() - wifiButtonTimeOut, button, wifiButtonState);
if(wifiButtonState != button)
{
#if !defined(WIFI_LED) || WIFI_BUTTON != WIFI_LED
// Pressing the boot button for 5 seconds while connecting will turn on AP mode
int button = digitalRead(WIFI_BUTTON);
if(wifiButtonState != button) {
wifiButtonState = button;
if(LOW == button) {
wifiButtonTimeOut = millis() + WIFI_BUTTON_TIMEOUT;
wifiButtonState = button;
if(LOW == button) {
DBUGF("Button pressed");
wifiButtonTimeOut = millis();
apMessage = false;
} else {
DBUGF("Button released");
if(millis() > wifiButtonTimeOut + WIFI_BUTTON_AP_TIMEOUT) {
startAP();
}
}
}

if(LOW == wifiButtonState && millis() > wifiButtonTimeOut) {
startAP();
}
#endif
if(LOW == wifiButtonState && millis() > wifiButtonTimeOut + WIFI_BUTTON_FACTORY_RESET_TIMEOUT)
{
lcd_display(F("Factory Reset"), 0, 0, 0, LCD_CLEAR_LINE);
lcd_display(F(""), 0, 1, 10 * 1000, LCD_CLEAR_LINE);
lcd_loop();

delay(1000);

config_reset();
ESP.eraseConfig();

delay(50);
ESP.reset();
}
else if(false == apMessage && LOW == wifiButtonState && millis() > wifiButtonTimeOut + WIFI_BUTTON_AP_TIMEOUT)
{
lcd_display(F("Access Point"), 0, 0, 0, LCD_CLEAR_LINE);
lcd_display(F(""), 0, 1, 10 * 1000, LCD_CLEAR_LINE);
lcd_loop();
apMessage = true;
}

// Manage state while connecting
if(isClientOnly && !WiFi.isConnected())
{
// If we have failed to connect turn on the AP
if(client_disconnects > 2) {
startAP();
Expand Down

0 comments on commit 507c05c

Please sign in to comment.