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

Commit da782ba

Browse files
authored
Add files via upload
1 parent 4ebf615 commit da782ba

File tree

5 files changed

+312
-17
lines changed

5 files changed

+312
-17
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@
44

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

7-
Using this library to detect a double reset, using RTC Memory for ESP8266 and EEPROM for ESP32.
7+
Using this library to detect a double reset, using
8+
9+
1. RTC Memory, EEPROM or SPIFFS for ESP8266
10+
2. EEPROM and SPIFFS for ESP32.
811

912
It is tested and working with
1013
1. The `ESP8266` Arduino platform with a recent stable release [`ESP8266 Core 2.6.2 or newer`] (https://github.com/esp8266/Arduino)
1114
2. The `ESP32` Arduino platform with a recent stable release [`ESP32 Core 1.0.4 or newer`] (https://github.com/espressif/arduino-esp32)
1215

1316
### Releases
14-
#### v1.0.0
17+
#### New in v1.0.1
18+
19+
1. Add EEPROM and SPIFFS support, besides RTC memory, for ESP8266
20+
2. Add SPIFFS support, besides EEPROM, for ESP32
21+
1522

1623
### PURPOSE:
1724

examples/ConfigOnDoubleReset/ConfigOnDoubleReset.ino

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626
*
2727
* How It Works
2828
* 1) ESP8266
29-
* Save data in RTC memory
29+
* Save data in RTC memory, EPPROM or SPIFFS
3030
* 2) ESP32
31-
* Save data in EEPROM from address 256, size 512 bytes (both configurable)
32-
*
31+
* Save data in
32+
* a) EEPROM from address 256, size 512 bytes (both configurable)
33+
* b) SPIFFS, file name "/drd.dat"
34+
*
3335
* So when the device starts up it checks this region of ram for a flag to see if it has been recently reset.
3436
* 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
3537
* it will only launch the configuration portal in response to closely spaced resets.
@@ -76,6 +78,20 @@ String Router_Pass;
7678

7779
#include <ESP_WiFiManager.h> //https://github.com/khoih-prog/ESP_WiFiManager
7880

81+
// These defines must be put before #include <ESP_DoubleResetDetector.h>
82+
// to select where to store DoubleResetDetector's variable.
83+
// For ESP32, You must select one to be true (EEPROM or SPIFFS)
84+
// For ESP8266, You must select one to be true (RTC, EEPROM or SPIFFS)
85+
// Otherwise, library will use default EEPROM storage
86+
#define ESP_DRD_USE_EEPROM false
87+
#define ESP_DRD_USE_SPIFFS true //false
88+
89+
#ifdef ESP8266
90+
#define ESP8266_DRD_USE_RTC false //true
91+
#endif
92+
93+
#define DOUBLERESETDETECTOR_DEBUG true //false
94+
7995
#include <ESP_DoubleResetDetector.h> //https://github.com/khoih-prog/ESP_DoubleResetDetector
8096

8197
// Number of seconds after reset during which a

library.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name=ESP_DoubleResetDetector
2-
version=1.0.0
2+
version=1.0.1
33
author=Khoi Hoang
44
maintainer=Khoi Hoang <[email protected]>
55
license=MIT
6-
sentence=Library to detect a double reset, using RTC Memory for ESP8266 and EEPROM for ESP32
7-
paragraph=An alternative start-up mode can be used. One example use is to allow re-configuration of a device's wifi.
6+
sentence=Library to detect a double reset, using RTC Memory, EEPROM or SPIFFS for ESP8266 and EEPROM and SPIFFS for ESP32
7+
paragraph=An alternative start-up mode can be used. One example use is to allow re-configuration of device's wifi credentials.
88
category=Device Control
99
url=https://github.com/khoih-prog/ESP_DoubleResetDetector
1010
architectures=esp8266,esp32

src/ESP_DoubleResetDetector.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/****************************************************************************************************************************
2+
* ESP_DoubleResetDetector.cpp
3+
* For ESP8266 / ESP32 boards
4+
*
5+
* ESP_DoubleResetDetector is a library for the ESP8266/Arduino platform
6+
* to enable trigger configure mode by resetting ESP32 / ESP8266 twice.
7+
*
8+
* Forked from DataCute https://github.com/datacute/DoubleResetDetector
9+
*
10+
* Built by Khoi Hoang https://github.com/khoih-prog/ESP_DoubleResetDetector
11+
* Licensed under MIT license
12+
* Version: 1.0.0
13+
*
14+
* Version Modified By Date Comments
15+
* ------- ----------- ---------- -----------
16+
* 1.0.0 K Hoang 15/12/2019 Initial coding
17+
* 1.0.1 K Hoang 30/12/2019 Now can use EEPROM or SPIFFS for both ESP8266 and ESP32. RTC still OK for ESP8266
18+
*****************************************************************************************************************************/
19+

src/ESP_DoubleResetDetector.h

Lines changed: 262 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* Version Modified By Date Comments
1515
* ------- ----------- ---------- -----------
1616
* 1.0.0 K Hoang 15/12/2019 Initial coding
17+
* 1.0.1 K Hoang 30/12/2019 Now can use EEPROM or SPIFFS for both ESP8266 and ESP32. RTC still OK for ESP8266
1718
*****************************************************************************************************************************/
1819

1920
#ifndef ESP_DoubleResetDetector_H
@@ -25,27 +26,279 @@
2526
#include <WProgram.h>
2627
#endif
2728

28-
#define ESP_DOUBLERESETDETECTOR_VERSION "1.0.0"
29+
//#define ESP_DRD_USE_EEPROM false
30+
//#define ESP_DRD_USE_SPIFFS false
31+
//#define ESP8266_DRD_USE_RTC false //true
32+
33+
#ifdef ESP32
34+
#if (!ESP_DRD_USE_EEPROM && !ESP_DRD_USE_SPIFFS)
35+
#warning Neither EEPROM nor SPIFFS selected. Default to EEPROM
36+
#ifdef ESP_DRD_USE_EEPROM
37+
#undef ESP_DRD_USE_EEPROM
38+
#define ESP_DRD_USE_EEPROM true
39+
#endif
40+
#endif
41+
#endif
42+
43+
#ifdef ESP8266
44+
#if (!ESP8266_DRD_USE_RTC && !ESP_DRD_USE_EEPROM && !ESP_DRD_USE_SPIFFS)
45+
#warning Neither RTC, EEPROM nor SPIFFS selected. Default to EEPROM
46+
#ifdef ESP_DRD_USE_EEPROM
47+
#undef ESP_DRD_USE_EEPROM
48+
#define ESP_DRD_USE_EEPROM true
49+
#endif
50+
#endif
51+
#endif
52+
53+
//default to use EEPROM, otherwise, use SPIFFS
54+
#if ESP_DRD_USE_EEPROM
55+
#include <EEPROM.h>
56+
57+
#define FLAG_DATA_SIZE 4
58+
59+
#ifndef EEPROM_SIZE
60+
#define EEPROM_SIZE 512
61+
#endif
62+
63+
#ifndef EEPROM_START
64+
#define EEPROM_START 256
65+
#endif
66+
#elif ESP_DRD_USE_SPIFFS
67+
#include <FS.h>
68+
#ifdef ESP32
69+
#include "SPIFFS.h"
70+
#endif
71+
72+
#define DRD_FILENAME "/drd.dat"
73+
74+
#endif //#if ESP_DRD_USE_EEPROM
75+
76+
#ifndef DOUBLERESETDETECTOR_DEBUG
77+
#define DOUBLERESETDETECTOR_DEBUG false
78+
#endif
79+
80+
#define ESP_DOUBLERESETDETECTOR_VERSION "1.0.1"
2981

3082
#define DOUBLERESETDETECTOR_FLAG_SET 0xD0D01234
3183
#define DOUBLERESETDETECTOR_FLAG_CLEAR 0xD0D04321
3284

3385
class DoubleResetDetector
3486
{
3587
public:
36-
DoubleResetDetector(int timeout, int address);
37-
bool detectDoubleReset();
38-
bool doubleResetDetected;
39-
void loop();
40-
void stop();
88+
DoubleResetDetector(int timeout, int address)
89+
{
90+
#if ESP_DRD_USE_EEPROM
91+
#if (DOUBLERESETDETECTOR_DEBUG)
92+
Serial.println("EEPROM size = " + String(EEPROM_SIZE) + ", start = " + String(EEPROM_START));
93+
#endif
94+
95+
EEPROM.begin(EEPROM_SIZE);
96+
#elif ESP_DRD_USE_SPIFFS
97+
// SPIFFS code
98+
if (!SPIFFS.begin())
99+
{
100+
#if (DOUBLERESETDETECTOR_DEBUG)
101+
Serial.println("SPIFFS failed!. Please use EEPROM.");
102+
#endif
103+
}
104+
#else
105+
#ifdef ESP8266
106+
//RTC only for ESP8266
107+
#endif
108+
#endif
109+
110+
this->timeout = timeout * 1000;
111+
this->address = address;
112+
doubleResetDetected = false;
113+
waitingForDoubleReset = false;
114+
};
115+
116+
bool detectDoubleReset()
117+
{
118+
doubleResetDetected = detectRecentlyResetFlag();
119+
120+
if (doubleResetDetected)
121+
{
122+
#if (DOUBLERESETDETECTOR_DEBUG)
123+
Serial.println("doubleResetDetected");
124+
#endif
125+
126+
clearRecentlyResetFlag();
127+
}
128+
else
129+
{
130+
#if (DOUBLERESETDETECTOR_DEBUG)
131+
Serial.println("No doubleResetDetected");
132+
#endif
133+
134+
setRecentlyResetFlag();
135+
waitingForDoubleReset = true;
136+
}
137+
138+
return doubleResetDetected;
139+
140+
};
141+
142+
void loop()
143+
{
144+
if (waitingForDoubleReset && millis() > timeout)
145+
{
146+
#if (DOUBLERESETDETECTOR_DEBUG)
147+
Serial.println("Stop doubleResetDetecting");
148+
#endif
149+
150+
stop();
151+
}
152+
};
153+
154+
void stop()
155+
{
156+
clearRecentlyResetFlag();
157+
waitingForDoubleReset = false;
158+
};
159+
160+
bool doubleResetDetected;
161+
41162

42163
private:
164+
uint32_t DOUBLERESETDETECTOR_FLAG;
43165
int timeout;
44166
int address;
45167
bool waitingForDoubleReset;
46-
bool detectRecentlyResetFlag();
47-
void clearRecentlyResetFlag();
48-
void setRecentlyResetFlag();
168+
169+
bool detectRecentlyResetFlag()
170+
{
171+
#if (ESP_DRD_USE_EEPROM)
172+
EEPROM.get(EEPROM_START, DOUBLERESETDETECTOR_FLAG);
173+
doubleResetDetectorFlag = DOUBLERESETDETECTOR_FLAG;
174+
175+
#if (DOUBLERESETDETECTOR_DEBUG)
176+
Serial.println("EEPROM Flag read = 0x" + String(DOUBLERESETDETECTOR_FLAG, HEX) );
177+
#endif
178+
#elif (ESP_DRD_USE_SPIFFS)
179+
// SPIFFS code
180+
if (SPIFFS.exists(DRD_FILENAME))
181+
{
182+
// if config file exists, load
183+
File file = SPIFFS.open(DRD_FILENAME, "r");
184+
185+
if (!file)
186+
{
187+
#if (DOUBLERESETDETECTOR_DEBUG)
188+
Serial.println("Loading config file failed");
189+
#endif
190+
}
191+
192+
file.readBytes((char *) &DOUBLERESETDETECTOR_FLAG, sizeof(DOUBLERESETDETECTOR_FLAG));
193+
doubleResetDetectorFlag = DOUBLERESETDETECTOR_FLAG;
194+
195+
#if (DOUBLERESETDETECTOR_DEBUG)
196+
Serial.println("SPIFFS Flag read = 0x" + String(DOUBLERESETDETECTOR_FLAG, HEX) );
197+
#endif
198+
199+
file.close();
200+
}
201+
#else
202+
#ifdef ESP8266
203+
//RTC only for ESP8266
204+
ESP.rtcUserMemoryRead(address, &doubleResetDetectorFlag, sizeof(doubleResetDetectorFlag));
205+
#endif
206+
#endif
207+
208+
doubleResetDetected = (doubleResetDetectorFlag == DOUBLERESETDETECTOR_FLAG_SET);
209+
return doubleResetDetected;
210+
};
211+
212+
void setRecentlyResetFlag()
213+
{
214+
doubleResetDetectorFlag = DOUBLERESETDETECTOR_FLAG_SET;
215+
216+
DOUBLERESETDETECTOR_FLAG = DOUBLERESETDETECTOR_FLAG_SET;
217+
218+
#if (ESP_DRD_USE_EEPROM)
219+
EEPROM.put(EEPROM_START, DOUBLERESETDETECTOR_FLAG);
220+
EEPROM.commit();
221+
222+
#if (DOUBLERESETDETECTOR_DEBUG)
223+
delay(1000);
224+
EEPROM.get(EEPROM_START, DOUBLERESETDETECTOR_FLAG);
225+
Serial.println("SetFlag write = 0x" + String(DOUBLERESETDETECTOR_FLAG, HEX) );
226+
#endif
227+
#elif (ESP_DRD_USE_SPIFFS)
228+
// SPIFFS code
229+
File file = SPIFFS.open(DRD_FILENAME, "w");
230+
#if (DOUBLERESETDETECTOR_DEBUG)
231+
Serial.println("Saving config file...");
232+
#endif
233+
234+
if (file)
235+
{
236+
file.write((uint8_t *) &DOUBLERESETDETECTOR_FLAG, sizeof(DOUBLERESETDETECTOR_FLAG));
237+
file.close();
238+
#if (DOUBLERESETDETECTOR_DEBUG)
239+
Serial.println("Saving config file OK");
240+
#endif
241+
}
242+
else
243+
{
244+
#if (DOUBLERESETDETECTOR_DEBUG)
245+
Serial.println("Saving config file failed");
246+
#endif
247+
}
248+
#else
249+
#ifdef ESP8266
250+
//RTC only for ESP8266
251+
ESP.rtcUserMemoryWrite(address, &doubleResetDetectorFlag, sizeof(doubleResetDetectorFlag));
252+
#endif
253+
#endif
254+
};
255+
256+
257+
void clearRecentlyResetFlag()
258+
{
259+
doubleResetDetectorFlag = DOUBLERESETDETECTOR_FLAG_CLEAR;
260+
DOUBLERESETDETECTOR_FLAG = DOUBLERESETDETECTOR_FLAG_CLEAR;
261+
262+
#if (ESP_DRD_USE_EEPROM)
263+
//DOUBLERESETDETECTOR_FLAG = DOUBLERESETDETECTOR_FLAG_CLEAR;
264+
EEPROM.put(EEPROM_START, DOUBLERESETDETECTOR_FLAG);
265+
EEPROM.commit();
266+
267+
#if (DOUBLERESETDETECTOR_DEBUG)
268+
delay(1000);
269+
EEPROM.get(EEPROM_START, DOUBLERESETDETECTOR_FLAG);
270+
Serial.println("ClearFlag write = 0x" + String(DOUBLERESETDETECTOR_FLAG, HEX) );
271+
#endif
272+
#elif (ESP_DRD_USE_SPIFFS)
273+
// SPIFFS code
274+
File file = SPIFFS.open(DRD_FILENAME, "w");
275+
#if (DOUBLERESETDETECTOR_DEBUG)
276+
Serial.println("Saving config file...");
277+
#endif
278+
279+
if (file)
280+
{
281+
file.write((uint8_t *) &DOUBLERESETDETECTOR_FLAG, sizeof(DOUBLERESETDETECTOR_FLAG));
282+
file.close();
283+
#if (DOUBLERESETDETECTOR_DEBUG)
284+
Serial.println("Saving config file OK");
285+
#endif
286+
}
287+
else
288+
{
289+
#if (DOUBLERESETDETECTOR_DEBUG)
290+
Serial.println("Saving config file failed");
291+
#endif
292+
}
293+
294+
#else
295+
#ifdef ESP8266
296+
//RTC only for ESP8266
297+
ESP.rtcUserMemoryWrite(address, &doubleResetDetectorFlag, sizeof(doubleResetDetectorFlag));
298+
#endif
299+
#endif
300+
};
301+
49302
uint32_t doubleResetDetectorFlag;
50303
};
51304
#endif // DoubleResetDetector_H

0 commit comments

Comments
 (0)