Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

Commit

Permalink
v.1.0.2
Browse files Browse the repository at this point in the history
### Releases v1.0.2

1. Fix bug by left-over cpp file.
2. Fix bug in example.
3. Enhance README.md
  • Loading branch information
khoih-prog authored Apr 10, 2020
1 parent 153ad61 commit 772c0e1
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 107 deletions.
43 changes: 32 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,48 @@

[![arduino-library-badge](https://www.ardu-badge.com/badge/ESP_DoubleResetDetector.svg?)](https://www.ardu-badge.com/ESP_DoubleResetDetector)

### Releases v1.0.2

1. Fix bug by left-over cpp file.
2. Fix bug in example.
3. Enhance README.md

This library is based on, modified, bug-fixed and improved from [`DataCute`](https://github.com/datacute/DoubleResetDetector) to add support for ESP32.

Using this library to detect a double reset, using

1. RTC Memory, EEPROM or SPIFFS for ESP8266
2. EEPROM and SPIFFS for ESP32.

It is tested and working with
1. [`ESP8266 Core 2.6.2 or newer`](https://github.com/esp8266/Arduino)
2. [`ESP32 Core 1.0.4 or newer`](https://github.com/espressif/arduino-esp32)
## Prerequisite
1. [`Arduino IDE 1.8.12 or later` for Arduino](https://www.arduino.cc/en/Main/Software)
2. [`ESP32 core 1.0.4 or later`](https://github.com/espressif/arduino-esp32/releases) for ESP32 (Use Arduino Board Manager)
3. [`ESP8266 core 2.6.3 or later`](https://github.com/esp8266/Arduino/releases) for ES82662 (Use Arduino Board Manager)

### Quick Start

#### Use Arduino Library Manager
#### Installing use Arduino Library Manager

The easiest way is to use `Arduino Library Manager`. Search for `ESP_DoubleResetDetector`, then select / install the latest version.
1. The easiest way is to use `Arduino Library Manager`. Search for `ESP_DoubleResetDetector`, then select / install the latest version.
2. More detailed instructions at [![arduino-library-badge](https://www.ardu-badge.com/badge/ESP_DoubleResetDetector.svg?)](https://www.ardu-badge.com/ESP_DoubleResetDetector)

#### Manual Install

The suggested way to install manually is to:

1. Navigate to [ESP_DoubleResetDetector](https://github.com/khoih-prog/ESP_DoubleResetDetector) page.
2. Download the latest release `ESP_DoubleResetDetector-master.zip`.
3. Extract the zip file to `ESP_DoubleResetDetector-master` directory
4. Copy whole `ESP_DoubleResetDetector-master` folder to Arduino libraries' directory such as `~/Arduino/libraries`.
4. Copy whole
- `ESP_DoubleResetDetector-master` folder to Arduino libraries directory such as `~/Arduino/libraries`.

### Releases
#### New in v1.0.1

#### Releases v1.0.2

1. Fix bug by left-over cpp file.
2. Fix bug in example.
3. Enhance README.md

#### Releases v1.0.1

1. Add EEPROM and SPIFFS support, besides RTC memory, for ESP8266
2. Add SPIFFS support, besides EEPROM, for ESP32
Expand Down Expand Up @@ -98,19 +112,26 @@ void loop()
drd->loop();
}
```

Also see examples:
1. [ConfigOnDoubleReset](examples/ConfigOnDoubleReset)
2. [minimal](examples/minimal)

### TO DO

1. Search for bug and improvement.
2. Similar features for Arduino (UNO, Mega, etc...)

### Contributing
### Contributions and thanks

1. Thanks to [zobix](https://github.com/zobix) for report the bug in [Isssue 2](https://github.com/khoih-prog/ESP_DoubleResetDetector/issues/2)

### Contributing
If you want to contribute to this project:
- Report bugs and errors
- Ask for enhancements
- Create issues and pull requests
- Tell other people about this library

### Copyright

Copyright 2019- Khoi Hoang
179 changes: 88 additions & 91 deletions examples/ConfigOnDoubleReset/ConfigOnDoubleReset.ino
Original file line number Diff line number Diff line change
@@ -1,71 +1,68 @@
/****************************************************************************************************************************
* ConfigOnDoubleReset.ino
* For ESP8266 / ESP32 boards
*
* ESP_WiFiManager is a library for the ESP8266/ESP32 platform (https://github.com/esp8266/Arduino) to enable easy
* configuration and reconfiguration of WiFi credentials using a Captive Portal. Inspired by:
* http://www.esp8266.com/viewtopic.php?f=29&t=2520
* https://github.com/chriscook8/esp-arduino-apboot
* https://github.com/esp8266/Arduino/blob/master/libraries/DNSServer/examples/CaptivePortalAdvanced/
*
* Forked from Tzapu https://github.com/tzapu/WiFiManager
* and from Ken Taylor https://github.com/kentaylor
*
* Built by Khoi Hoang https://github.com/khoih-prog/ESP_WiFiManager
* Licensed under MIT license
* Version: 1.0.1
*
* Version Modified By Date Comments
* ------- ----------- ---------- -----------
* 1.0.0 K Hoang 07/10/2019 Initial coding
* 1.0.1 K Hoang 13/12/2019 Fix bug. Add features. Add support for ESP32
ConfigOnDoubleReset.ino
For ESP8266 / ESP32 boards
ESP_DoubleResetDetector is a library for the ESP8266/Arduino platform
to enable trigger configure mode by resetting ESP32 / ESP8266 twice.
Forked from DataCute https://github.com/datacute/DoubleResetDetector
Built by Khoi Hoang https://github.com/khoih-prog/ESP_DoubleResetDetector
Licensed under MIT license
Version: 1.0.2
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 15/12/2019 Initial coding
1.0.1 K Hoang 30/12/2019 Now can use EEPROM or SPIFFS for both ESP8266 and ESP32. RTC still OK for ESP8266
1.0.2 K Hoang 10/04/2020 Fix bug by left-over cpp file and in example.
*****************************************************************************************************************************/
/****************************************************************************************************************************
* This example will open a configuration portal when the reset button is pressed twice.
* This method works well on Wemos boards which have a single reset button on board. It avoids using a pin for launching the configuration portal.
*
* How It Works
* 1) ESP8266
* Save data in RTC memory, EPPROM or SPIFFS
* 2) ESP32
* Save data in
* a) EEPROM from address 256, size 512 bytes (both configurable)
* b) SPIFFS, file name "/drd.dat"
*
* So when the device starts up it checks this region of ram for a flag to see if it has been recently reset.
* If so it launches a configuration portal, if not it sets the reset flag. After running for a while this flag is cleared so that
* it will only launch the configuration portal in response to closely spaced resets.
*
* Settings
* There are two values to be set in the sketch.
*
* DRD_TIMEOUT - Number of seconds to wait for the second reset. Set to 10 in the example.
* DRD_ADDRESS - The address in ESP8266 RTC RAM to store the flag. This memory must not be used for other purposes in the same sketch. Set to 0 in the example.
*
* This example, originally relied on the Double Reset Detector library from https://github.com/datacute/DoubleResetDetector
* To support ESP32, use ESP_DoubleResetDetector library from //https://github.com/khoih-prog/ESP_DoubleResetDetector
This example will open a configuration portal when the reset button is pressed twice.
This method works well on Wemos boards which have a single reset button on board. It avoids using a pin for launching the configuration portal.
How It Works
1) ESP8266
Save data in RTC memory, EPPROM or SPIFFS
2) ESP32
Save data in
a) EEPROM from address 256, size 512 bytes (both configurable)
b) SPIFFS, file name "/drd.dat"
So when the device starts up it checks this region of ram for a flag to see if it has been recently reset.
If so it launches a configuration portal, if not it sets the reset flag. After running for a while this flag is cleared so that
it will only launch the configuration portal in response to closely spaced resets.
Settings
There are two values to be set in the sketch.
DRD_TIMEOUT - Number of seconds to wait for the second reset. Set to 10 in the example.
DRD_ADDRESS - The address in ESP8266 RTC RAM to store the flag. This memory must not be used for other purposes in the same sketch. Set to 0 in the example.
This example, originally relied on the Double Reset Detector library from https://github.com/datacute/DoubleResetDetector
To support ESP32, use ESP_DoubleResetDetector library from //https://github.com/khoih-prog/ESP_DoubleResetDetector
*****************************************************************************************************************************/

//Ported to ESP32
#ifdef ESP32
#include <esp_wifi.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <esp_wifi.h>
#include <WiFi.h>
#include <WiFiClient.h>

#define ESP_getChipId() ((uint32_t)ESP.getEfuseMac())
#define ESP_getChipId() ((uint32_t)ESP.getEfuseMac())

#define LED_ON HIGH
#define LED_OFF LOW
#define LED_ON HIGH
#define LED_OFF LOW
#else
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>

#define ESP_getChipId() (ESP.getChipId())
#define ESP_getChipId() (ESP.getChipId())

#define LED_ON LOW
#define LED_OFF HIGH
#define LED_ON LOW
#define LED_OFF HIGH
#endif

// SSID and PW for Config Portal
Expand All @@ -87,14 +84,14 @@ String Router_Pass;
#define ESP_DRD_USE_SPIFFS true //false

#ifdef ESP8266
#define ESP8266_DRD_USE_RTC false //true
#define ESP8266_DRD_USE_RTC false //true
#endif

#define DOUBLERESETDETECTOR_DEBUG true //false

#include <ESP_DoubleResetDetector.h> //https://github.com/khoih-prog/ESP_DoubleResetDetector

// Number of seconds after reset during which a
// Number of seconds after reset during which a
// subseqent reset will be considered a double reset.
#define DRD_TIMEOUT 10

Expand All @@ -118,23 +115,23 @@ void heartBeatPrint(void)
Serial.print("H"); // H means connected to WiFi
else
Serial.print("F"); // F means not connected to WiFi
if (num == 80)

if (num == 80)
{
Serial.println();
num = 1;
}
else if (num++ % 10 == 0)
else if (num++ % 10 == 0)
{
Serial.print(" ");
}
}
}

void check_status()
{
static ulong checkstatus_timeout = 0;

#define HEARTBEAT_INTERVAL 10000L
#define HEARTBEAT_INTERVAL 10000L
// Print hearbeat every HEARTBEAT_INTERVAL (10) seconds.
if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
{
Expand All @@ -143,7 +140,7 @@ void check_status()
}
}

void setup()
void setup()
{
// put your setup code here, to run once:
// initialize the LED digital pin as an output.
Expand All @@ -153,21 +150,21 @@ void setup()

drd = new DoubleResetDetector(DRD_TIMEOUT, DRD_ADDRESS);

//Local intialization. Once its business is done, there is no need to keep it around
//Local intialization. Once its business is done, there is no need to keep it around
ESP_WiFiManager ESP_wifiManager;

// We can't use WiFi.SSID() in ESP32 as it's only valid after connected.
// We can't use WiFi.SSID() in ESP32 as it's only valid after connected.
// SSID and Password stored in ESP32 wifi_ap_record_t and wifi_config_t are also cleared in reboot
// Have to create a new function to store in EEPROM/SPIFFS for this purpose
Router_SSID = ESP_wifiManager.WiFi_SSID();
Router_Pass = ESP_wifiManager.WiFi_Pass();

//Remove this line if you do not want to see WiFi password printed
Serial.println("Stored: SSID = " + Router_SSID + ", Pass = " + Router_Pass);

// SSID to uppercase
// SSID to uppercase
ssid.toUpperCase();

if (Router_SSID != "")
{
ESP_wifiManager.setConfigPortalTimeout(60); //If no access point name has been previously entered disable timeout.
Expand All @@ -178,14 +175,14 @@ void setup()
Serial.println("No stored Credentials. No timeout");
initialConfig = true;
}
if (drd->detectDoubleReset())

if (drd->detectDoubleReset())
{
Serial.println("Double Reset Detected");
initialConfig = true;
}
if (initialConfig)

if (initialConfig)
{
Serial.println("Starting configuration portal.");
digitalWrite(PIN_LED, LED_ON); // turn the LED on by making the voltage LOW to tell us we are in configuration mode.
Expand All @@ -195,42 +192,42 @@ void setup()
//switched off via webserver or device is restarted.
//ESP_wifiManager.setConfigPortalTimeout(600);

//it starts an access point
//it starts an access point
//and goes into a blocking loop awaiting configuration
if (!ESP_wifiManager.startConfigPortal((const char *) ssid.c_str(), password))
if (!ESP_wifiManager.startConfigPortal((const char *) ssid.c_str(), password))
Serial.println("Not connected to WiFi but continuing anyway.");
else
Serial.println("WiFi connected...yeey :)");
else
Serial.println("WiFi connected...yeey :)");
}

digitalWrite(PIN_LED, LED_OFF); // Turn led off as we are not in configuration mode.
#define WIFI_CONNECT_TIMEOUT 30000L
#define WHILE_LOOP_DELAY 200L
#define WHILE_LOOP_STEPS (WIFI_CONNECT_TIMEOUT / ( 3 * WHILE_LOOP_DELAY ))

#define WIFI_CONNECT_TIMEOUT 30000L
#define WHILE_LOOP_DELAY 200L
#define WHILE_LOOP_STEPS (WIFI_CONNECT_TIMEOUT / ( 3 * WHILE_LOOP_DELAY ))

unsigned long startedAt = millis();

while ( (WiFi.status() != WL_CONNECTED) && (millis() - startedAt < WIFI_CONNECT_TIMEOUT ) )
{
{
WiFi.mode(WIFI_STA);
WiFi.persistent (true);
// We start by connecting to a WiFi network

Serial.print("Connecting to ");
Serial.println(Router_SSID);

WiFi.begin(Router_SSID.c_str(), Router_Pass.c_str());

int i = 0;
while((!WiFi.status() || WiFi.status() >= WL_DISCONNECTED) && i++ < WHILE_LOOP_STEPS)
while ((!WiFi.status() || WiFi.status() >= WL_DISCONNECTED) && i++ < WHILE_LOOP_STEPS)
{
delay(WHILE_LOOP_DELAY);
}
}
}

Serial.print("After waiting ");
Serial.print((millis()- startedAt) / 1000);
Serial.print((millis() - startedAt) / 1000);
Serial.print(" secs more in setup(), connection result is ");

if (WiFi.status() == WL_CONNECTED)
Expand All @@ -242,7 +239,7 @@ void setup()
Serial.println(ESP_wifiManager.getStatus(WiFi.status()));
}

void loop()
void loop()
{
// Call the double reset detector loop method every so often,
// so that it can recognise when the timeout expires.
Expand All @@ -252,5 +249,5 @@ void loop()

// put your main code here, to run repeatedly
check_status();

}
Loading

0 comments on commit 772c0e1

Please sign in to comment.