diff --git a/README.md b/README.md index adba3c6..3a662ab 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,12 @@ [](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 @@ -9,27 +15,35 @@ 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 [](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 @@ -98,13 +112,21 @@ 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 @@ -112,5 +134,4 @@ If you want to contribute to this project: - Tell other people about this library ### Copyright - Copyright 2019- Khoi Hoang diff --git a/examples/ConfigOnDoubleReset/ConfigOnDoubleReset.ino b/examples/ConfigOnDoubleReset/ConfigOnDoubleReset.ino index 298a917..879dea5 100644 --- a/examples/ConfigOnDoubleReset/ConfigOnDoubleReset.ino +++ b/examples/ConfigOnDoubleReset/ConfigOnDoubleReset.ino @@ -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 @@ -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 @@ -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)) { @@ -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. @@ -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. @@ -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. @@ -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) @@ -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. @@ -252,5 +249,5 @@ void loop() // put your main code here, to run repeatedly check_status(); - + } diff --git a/examples/minimal/minimal.ino b/examples/minimal/minimal.ino index 0748eb1..7a95776 100644 --- a/examples/minimal/minimal.ino +++ b/examples/minimal/minimal.ino @@ -1,6 +1,26 @@ +/**************************************************************************************************************************** + minimal.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. + *****************************************************************************************************************************/ + #include <ESP_DoubleResetDetector.h> -// 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 @@ -9,10 +29,14 @@ DoubleResetDetector drd(DRD_TIMEOUT, DRD_ADDRESS); +#ifndef LED_BUILTIN +#define LED_BUILTIN 2 // Pin D2 mapped to pin GPIO2/ADC12 of ESP32, control on-board LED +#endif + void setup() { pinMode(LED_BUILTIN, OUTPUT); - + Serial.begin(115200); Serial.println(); Serial.println("DoubleResetDetector Example Program"); diff --git a/library.json b/library.json new file mode 100644 index 0000000..97feb08 --- /dev/null +++ b/library.json @@ -0,0 +1,13 @@ +{ + "name": "ESP_DoubleResetDetector", + "keywords": "rtc,eeprom,spiffs,reset,data,esp32,esp8266", + "description": "Library to detect a double reset, using RTC Memory, EEPROM or SPIFFS for ESP8266 and EEPROM and SPIFFS for ESP32", + "repository": + { + "type": "git", + "url": "https://github.com/khoih-prog/ESP_DoubleResetDetector" + }, + "version": "1.0.2", + "frameworks": "arduino", + "platforms": "espressif" +} diff --git a/library.properties b/library.properties index eaf5c05..86a5796 100644 --- a/library.properties +++ b/library.properties @@ -1,7 +1,7 @@ name=ESP_DoubleResetDetector -version=1.0.1 +version=1.0.2 author=Khoi Hoang -maintainer=Khoi Hoang <khoih58@gmail.com> +maintainer=Khoi Hoang <khoih.prog@gmail.com> license=MIT sentence=Library to detect a double reset, using RTC Memory, EEPROM or SPIFFS for ESP8266 and EEPROM and SPIFFS for ESP32 paragraph=An alternative start-up mode can be used. One example use is to allow re-configuration of device's wifi credentials. diff --git a/src/ESP_DoubleResetDetector.h b/src/ESP_DoubleResetDetector.h index 1e00454..4477d06 100644 --- a/src/ESP_DoubleResetDetector.h +++ b/src/ESP_DoubleResetDetector.h @@ -9,12 +9,13 @@ Built by Khoi Hoang https://github.com/khoih-prog/ESP_DoubleResetDetector Licensed under MIT license - Version: 1.0.0 + 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. *****************************************************************************************************************************/ #ifndef ESP_DoubleResetDetector_H