Problem: I need to retry at least 3 times (before giving up) to reconnect my NodeMCU if it doesn't get connected to Wifi, through deep sleep cycles in order to save batteries. I thought to use RTC memory to save a counter, but it doesn't work because content of RTC RAM is unpredicatble at first power up.

Following is a NOT WORKING code, but it gives the idea of what I need to achieve. The problem is: the kounter variable is persistent through cycles, but its very first value is unpredicatble.

#define DONEPIN 5
uint32_t kounter;

void setup() {
    digitalWrite(DONEPIN, HIGH);
    //WiFi setup functions...
    // .....
    if (WiFi.status() != WL_CONNECTED) {
        ESP.rtcUserMemoryRead(0, &kounter, sizeof(kounter));
        if (kounter > 3) {
            digitalWrite(DONEPIN, LOW);
        ESP.rtcUserMemoryWrite(0, &kounter, sizeof(kounter));
  • @Majenko interesting! Didn’t know that. I removed my comment. – Gerben Jul 29 '18 at 20:15
  • @Gerben So did I :P – Majenko Jul 29 '18 at 20:16
  • 1
    You can differentiate between first powerup and resets (after deep sleeps) by using rtc_get_reset_reason(0). If I’m not mistaken this should return a 1 on powerup. So when this returns 1 overwrite the RTC memory with zero. Otherwise read the memory. – Gerben Jul 29 '18 at 20:34

The simplest trick is to use more than one memory location. Have one, as you have now, as a counter, and then have one or more as a "This has been configured" flag.

Ideally you want the flag to have a very low probability of occurring randomly, so using a combination of a number of memory locations would improve that.

For instance, if you read memory locations 1-4 and look for four specific bytes, that gives you about a 1 in 4 billion possibility of it happening randomly.

If you don't see those four bytes, you set the counter byte to 0 (or whatever you want your initial value to be) and set the other 4 memory locations to contain your 4 flag bytes.

Using 8 bytes for your flag would give you in the order of 1 in 1.8x1019 chance of it occuring randomly.

These "flag byte combination" are often called "magic numbers" and are used all over the place to identify either what something is, or if something is valid.

  • So, can I use a struct { byte counter; byte validation[7]} ? – Fabio Marzocca Jul 29 '18 at 19:05
  • 1
    Sure, you could do. I don't know what the functions allow you to easily pass though, so it may be simpler to just directly access the memory's bytes. – Majenko Jul 29 '18 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.