- Why do we need this ESP_DoubleResetDetector library
- Changelog
- Prerequisites
- Installation
- HOWTO Usage
- Examples
- Examples from other libraries
- 1. ESP_WiFiManager Library
- 2. ESPAsync_WiFiManager Library
- Many other libraries are depending on this library's DRD feature
- Example checkWaitingDRD
- Debug Terminal Output Samples
- Libraries using ESP_DoubleResetDetector or DoubleResetDetector_Generic library
- Debug
- Troubleshooting
- Issues
- TO DO
- DONE
- Contributions and Thanks
- Contributing
- License
- Copyright
Why do we need this ESP_DoubleResetDetector library
ESP_DoubleResetDetector is a library for the ESP8266 and ESP32 boards to detects a double reset
, within configurable timeout (default 10s) seconds, so that an alternative start-up mode can be used. Example use cases are to allow re-configuration of a device's WiFi / MQTT / Blynk credentials.
This library is based on, modified, bug-fixed and improved from Stephen Denne's DoubleResetDetector
to add support to ESP8266 and ESP32 using EEPROM
, SPIFFS
or LittleFS
besides original RTC
.
Currently, DoubleResetDetector
only supports ESP8266 using RTC memory.
This library can be used to detect a double reset within a predetermined time
to force the program to enter a special operation such as Config Portal, Clear Default Data, etc., using :
EEPROM
,SPIFFS
orLittleFS
for ESP8266 and ESP32 boards.RTC
memory for ESP8266 boards (unadvised).
This ESP_DoubleResetDetector library currently supports these following boards:
- ESP32, ESP32_C3, ESP32_S2 and ESP32_S3 boards, using EEPROM, SPIFFS or LittleFS.
- ESP8266 boards RTC memory, EEPROM, SPIFFS or LittleFS
Arduino IDE 1.8.19+
for Arduino.ESP32 Core 2.0.5+
for ESP32-based boards.ESP8266 Core 3.0.2+
for ESP8266-based boards. . SPIFFS is deprecated from ESP8266 core 2.7.1+, to use LittleFS.LittleFS_esp32 v1.0.6+
for ESP32-based boards using LittleFS with ESP32 core v1.0.5-. To install, check . Notice: ThisLittleFS_esp32 library
has been integrated to Arduino ESP32 core v1.0.6+ and you don't need to install it if using ESP32 core v1.0.6+
The best and easiest way is to use Arduino Library Manager
. Search for ESP_DoubleResetDetector
, then select / install the latest version. You can also use this link for more detailed instructions.
- Navigate to ESP_DoubleResetDetector page.
- Download the latest release
ESP_DoubleResetDetector-master.zip
. - Extract the zip file to
ESP_DoubleResetDetector-master
directory - Copy the whole
ESP_DoubleResetDetector-master
folder to Arduino libraries' directory such as~/Arduino/libraries/
.
- Install VS Code
- Install PlatformIO
- Install ESP_DoubleResetDetector library by using Library Manager. Search for ESP_DoubleResetDetector in Platform.io Author's Libraries
- Use included platformio.ini file from examples to ensure that all dependent libraries will installed automatically. Please visit documentation for the other options and examples at Project Configuration File
How to use
// These defines must be put before #include <ESP_DoubleResetDetector.h>
// to select where to store DoubleResetDetector's variable.
// For ESP32, You must select one to be true (EEPROM or SPIFFS)
// For ESP8266, You must select one to be true (RTC, EEPROM, LITTLEFS or SPIFFS)
// Otherwise, library will use default EEPROM storage
#ifdef ESP8266
#define ESP8266_DRD_USE_RTC false //true
#define ESP_DRD_USE_LITTLEFS true //false
#endif
#define ESP_DRD_USE_EEPROM false
#define ESP_DRD_USE_SPIFFS true
#define DOUBLERESETDETECTOR_DEBUG true //false
#include <ESP_DoubleResetDetector.h> //https://github.com/khoih-prog/ESP_DoubleResetDetector
// Number of seconds after reset during which a
// subsequent reset will be considered a double reset.
#define DRD_TIMEOUT 10
// RTC Memory Address for the DoubleResetDetector to use
#define DRD_ADDRESS 0
DoubleResetDetector* drd;
#ifdef ESP32
// For ESP32
#ifndef LED_BUILTIN
#define LED_BUILTIN 2 // Pin D2 mapped to pin GPIO2/ADC12 of ESP32, control on-board LED
#endif
#define LED_OFF LOW
#define LED_ON HIGH
#else
// For ESP8266
#define LED_ON LOW
#define LED_OFF HIGH
#endif
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
Serial.println("\nStarting minimal example for ESP_DoubleResetDetector");
drd = new DoubleResetDetector(DRD_TIMEOUT, DRD_ADDRESS);
if (drd->detectDoubleReset())
{
Serial.println("Double Reset Detected");
digitalWrite(LED_BUILTIN, LED_ON);
}
else
{
Serial.println("No Double Reset Detected");
digitalWrite(LED_BUILTIN, LED_OFF);
}
}
void loop()
{
// Call the double reset detector loop method every so often,
// so that it can recognise when the timeout expires.
// You can also call drd.stop() when you wish to no longer
// consider the next reset as a double reset.
drd->loop();
}
- 1. ConfigOnDoubleReset
- 2. ConfigOnDRD_FS_MQTT_Ptr
- 3. ESP32_FSWebServer_DRD
- 4. ESP_FSWebServer_DRD
- 5. ConfigOnDRD_ESP32_minimal
- 6. ConfigOnDRD_ESP8266_minimal
- 7. ConfigOnDRD_FS_MQTT_Ptr_Complex
- 8. ConfigOnDRD_FS_MQTT_Ptr_Medium
- 1. Async_ConfigOnDoubleReset
- 2. Async_ConfigOnDRD_FS_MQTT_Ptr
- 3. Async_ESP32_FSWebServer_DRD
- 4. Async_ESP_FSWebServer_DRD
- 5. Async_ConfigOnDRD_ESP32_minimal
- 6. Async_ConfigOnDRD_ESP8266_minimal
- 7. Async_ConfigOnDRD_FS_MQTT_Ptr_Complex
- 8. Async_ConfigOnDRD_FS_MQTT_Ptr_Medium
and there are many more.
All examples of these following libraries are using DRD feature of this ESP_DoubleResetDetector Library
- 1. Blynk_WM
- 2. Blynk_Async_WM
- 3. BlynkEthernet_WM
- 4. BlynkESP32_BT_WF
- 5. BlynkGSM_Manager
- 6. Blynk_Async_ESP32_BT_WF
- 7. Blynk_Async_GSM_Manager
- 8. Ethernet_Manager
Example checkWaitingDRD
ESP_DoubleResetDetector/examples/checkWaitingDRD/checkWaitingDRD.ino
Lines 14 to 134 in d990bbb
This is terminal debug output when running ESP32_FSWebServer_DRD on ESP32 ESP32_DEV.. Config Portal was requested by DRD to input and save Credentials. The boards then connected to WiFi AP HueNet1 using new Static IP successfully. WiFi AP HueNet1 is then lost, and board autoreconnects itself to backup WiFi AP HueNet2.
Starting ESP32_FSWebServer_DRD with DoubleResetDetect using SPIFFS on ESP32_DEV
ESP_WiFiManager v1.12.1
ESP_DoubleResetDetector v1.3.2
FS File: /ConfigSW.json, size: 150B
FS File: /CanadaFlag_1.png, size: 40.25KB
FS File: /CanadaFlag_2.png, size: 8.12KB
FS File: /CanadaFlag_3.jpg, size: 10.89KB
FS File: /edit.htm.gz, size: 4.02KB
FS File: /favicon.ico, size: 1.12KB
FS File: /graphs.js.gz, size: 1.92KB
FS File: /index.htm, size: 3.63KB
FS File: /drd.dat, size: 4B
FS File: /wifi_cred.dat, size: 192B
[WM] RFC925 Hostname = ESP32-FSWebServerDRD
[WM] setAPStaticIPConfig
[WM] setSTAStaticIPConfig for USE_CONFIGURABLE_DNS
[WM] Set CORS Header to : Your Access-Control-Allow-Origin
Stored: SSID = HueNet2, Pass = 12345678
[WM] * Add SSID = HueNet2 , PW = 12345678
Got stored Credentials. Timeout 120s for Config Portal
SPIFFS Flag read = 0xd0d04321
No doubleResetDetected
Saving config file...
Saving config file OK
[WM] LoadWiFiCfgFile
[WM] OK
[WM] * Add SSID = HueNet1 , PW = 12345678
[WM] * Add SSID = HueNet2 , PW = 12345678
ConnectMultiWiFi in setup
[WM] ConnectMultiWiFi with :
[WM] * Flash-stored Router_SSID = HueNet2 , Router_Pass = 12345678
[WM] * Additional SSID = HueNet1 , PW = 12345678
[WM] * Additional SSID = HueNet2 , PW = 12345678
[WM] Connecting MultiWifi...
[WM] WiFi connected after time: 1
[WM] SSID: HueNet1 ,RSSI= -27
[WM] Channel: 2 ,IP address: 192.168.2.232
After waiting 3.16 secs more in setup(), connection result is connected. Local IP: 192.168.2.232
HTTP server started @ 192.168.2.232
Open http://esp32-fs-browser.local/edit to see the file browser
[WM] freeing allocated params!
Stop doubleResetDetecting
Saving config file...
Saving config file OK
WiFi lost. Call connectMultiWiFi in loop
[WM] ConnectMultiWiFi with :
[WM] * Flash-stored Router_SSID = HueNet2 , Router_Pass = 12345678
[WM] * Additional SSID = HueNet1 , PW = 12345678
[WM] * Additional SSID = HueNet2 , PW = 12345678
[WM] Connecting MultiWifi...
[WM] WiFi connected after time: 3
[WM] SSID: HueNet2 ,RSSI= -59
[WM] Channel: 4 ,IP address: 192.168.2.232
HHHHHHHHHH HHHHHHHHHH HHHHHHHHHH HHHHHHHHHH
This is terminal debug output when running ConfigOnDoubleReset on ESP32 ESP32_DEV.. Config Portal was requested by DRD to input and save Credentials.
Starting ConfigOnDoubleReset with DoubleResetDetect using LittleFS on ESP32_DEV
ESP_WiFiManager v1.12.1
ESP_DoubleResetDetector v1.3.2
[WM] RFC925 Hostname = ConfigOnDoubleReset
[WM] Set CORS Header to : Your Access-Control-Allow-Origin
Stored: SSID = HueNet1, Pass = 12345678
[WM] * Add SSID = HueNet1 , PW = 87654321
Got stored Credentials. Timeout 120s for Config Portal
LittleFS Flag read = 0xd0d04321
No doubleResetDetected
Saving config file...
Saving config file OK
[WM] LoadWiFiCfgFile
[WM] OK
[WM] * Add SSID = HueNet1 , PW = jenniqqs
[WM] * Add SSID = HueNet2 , PW = jenniqqs
ConnectMultiWiFi in setup
[WM] ConnectMultiWiFi with :
[WM] * Flash-stored Router_SSID = HueNet1 , Router_Pass = jenniqqs
[WM] * Additional SSID = HueNet1 , PW = 12345678
[WM] * Additional SSID = HueNet2 , PW = 87654321
[WM] Connecting MultiWifi...
[WM] WiFi connected after time: 1
[WM] SSID: HueNet1 ,RSSI= -32
[WM] Channel: 2 ,IP address: 192.168.2.101
After waiting 4.43 secs more in setup(), connection result is connected. Local IP: 192.168.2.101
[WM] freeing allocated params!
HStop doubleResetDetecting
Saving config file...
Saving config file OK
HHHHHHHHH HHHHHHHHHH HHHHHHHHHH HHHHHHHHHH HHHHHHHHHH HHHHHHHHHH HHHHHHHHHH HHHHHHHHHH
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:9720
ho 0 tail 12 room 4
load:0x40080400,len:6352
entry 0x400806b8
Starting ConfigOnDoubleReset with DoubleResetDetect using LittleFS on ESP32_DEV
ESP_WiFiManager v1.12.1
ESP_DoubleResetDetector v1.3.2
[WM] RFC925 Hostname = ConfigOnDoubleReset
[WM] Set CORS Header to : Your Access-Control-Allow-Origin
Stored: SSID = HueNet1, Pass = jenniqqs
[WM] * Add SSID = HueNet1 , PW = jenniqqs
Got stored Credentials. Timeout 120s for Config Portal
LittleFS Flag read = 0xd0d01234
doubleResetDetected
Saving config file...
Saving config file OK
Open Config Portal without Timeout: Double Reset Detected
Starting configuration portal.
[WM] WiFi.waitForConnectResult Done
[WM] SET AP
[WM] Configuring AP SSID = ESP_9ABF498
[WM] AP PWD = your_password
[WM] AP Channel = 3
[WM] AP IP address = 192.168.4.1
[WM] HTTP server started
[WM] ESP_WiFiManager::startConfigPortal : Enter loop
This is terminal debug output when running ESPAsync_WiFi on ESP32 ESP32S3_DEV.. Config Portal was requested by DRD to input and save Credentials.
Starting ESPAsync_WiFi using LittleFS on ESP32S3_DEV
ESPAsync_WiFiManager_Lite v1.9.0
ESP_DoubleResetDetector v1.3.2
LittleFS Flag read = 0xD0D01234
doubleResetDetected
Saving config file...
Saving config file OK
[WML] Hdr=ESP_WM_LITE,SSID=HueNet1,PW=12345678
[WML] SSID1=HueNet2,PW1=12345678
[WML] BName=ESP32_S3
[WML] Hdr=ESP_WM_LITE,SSID=HueNet1,PW=12345678
[WML] SSID1=HueNet2,PW1=12345678
[WML] BName=ESP32_S3
[WML] WiFi networks found:
[WML] 1: HueNet, -20dB
[WML] 2: HueNetTek, -32dB
[WML] 3: HueNet1, -39dB
[WML] 4: HueNet2, -54dB
[WML] 5: DECO-5655, -72dB
[WML] 6: Guest5655, -72dB
[WML] 7: rogers786, -76dB
[WML] 8: AT_301_WLREL6325F_f66d, -76dB
[WML] 10: Rogers 786, -78dB
[WML] 11: BELL042, -80dB
[WML] 12: WirelessMississauga, -82dB
[WML] 13: SmartRG-02a2, -87dB
[WML] 14: Jasmine, -87dB
[WML] 15: Access, -89dB
[WML] 16: Jessie, -90dB
[WML]
stConf:SSID=ESP_E1A1DF7C,PW=MyESP_E1A1DF7C
[WML] IP=192.168.4.1,ch=10
F
Your stored Credentials :
Blynk Server1 = account.duckdns.org
Token1 = token1
Blynk Server2 = account.ddns.net
Token2 = token2
Port = 8080
MQTT Server = mqtt.duckdns.org
You can also see how ESP_DoubleResetDetector
and DoubleResetDetector_Generic
are applied in many other libraries, such as:
- Blynk_WM
- BlynkEthernet_WM
- WiFiManager_NINA_Lite
- BlynkESP32_BT_WF,
- Blynk_GSM_Manager,
- Blynk_Esp8266AT_WM,
- Blynk_WiFiNINA_WM,
- Blynk_Async_WM,
- Blynk_Async_ESP32_BT_WF,
- Blynk_Async_GSM_Manager,
- ESP_WiFiManager
- ESPAsync_WiFiManager
- WiFiManager_NINA_Lite
- BlynkEthernet_STM32_WM,
- ESP_AT_WM_Lite
- WIOTerminal_WiFiManager
- Ethernet_Manager
- Ethernet_Manager_STM32
and the list is growing fast.
Debug is disabled by default. To enable debug:
// Use this to output debug msgs to Serial
#define DOUBLERESETDETECTOR_DEBUG true
If you get compilation errors, more often than not, you may need to install a newer version of the ESP32 / ESP8266
core for Arduino.
Sometimes, the library will only work if you update the ESP32 / ESP8266
core to the latest version because I am using some newly added function.
Submit issues to: ESP_DoubleResetDetector issues
- Search for bug and improvement.
- For ESP32 and ESP8266 (EEPROM, SPIFFS and LittleFS).
- Similar features for Arduino (UNO, Mega, SAM DUE, SAMD21/SAMD51, nRF52, STM32, Teensy, etc.). Look at DoubleResetDetector_Generic
- Add support to
ESP32_C3
,ESP32_S2
- Add support to
ESP32_S3
using ESP32 core v2.0.2+ - Add waitingForDRD() function to signal in DRD waiting period
- Fix ESP32 chipID for example
ConfigOnDoubleReset
- Remove dependency on
LittleFS_esp32
library to prevent PIO error when using new ESP32 core v1.0.6+ - Add astyle using
allman
style. Restyle the library
- Thanks to zobix for report the bug in Issue 2
- Thanks to Tochi Moreno for enhancement request in DRD is waiting for a double reset? #14 leading to v1.3.1 to add
waitingForDRD()
function to signal in DRD waiting period
Zobix |
Tochi Moreno |
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
- The library is licensed under MIT
Copyright (c) 2019- Khoi Hoang