RTC Memory

esp8266 rtc

Use RTC memory, instead of EEPROM, in ESP8266 because it survives deep sleep mode to reduce power consumption even further

Write a simple number to RTC Memory, deep sleep it and then wake it up again to read the same number! Use the number of count, when to turn on the radio to process something.

Example of RTC Memory read

bool	system_rtc_mem_read	(
	uint32	src_addr,
	void	*	des_addr,
	uint32	save_size
)

Example of RTC Memory write

bool	system_rtc_mem_write	(
	uint32	des_addr,
	void	*	src_addr,
	uint32	save_size
)

Code

Download code
#define RTCMEMORYSTART 65
#define MAXHOUR 5 // number of hours to deep sleep for

extern "C" {
#include "user_interface.h"
}

typedef struct {
  int count;
} rtcStore;

rtcStore rtcMem;

void setup() {
  Serial.begin(115200);
  Serial.println("");

  Serial.println("Waking up...");

  Serial.print("Reading ");
  readFromRTCMemory();
  Serial.print("Writing ");
  writeToRTCMemory();

  Serial.print("Sleeping for 5 seconds. ");
  if (rtcMem.count == 0) {
    Serial.println("Will wake up with radio!");
    ESP.deepSleep(5000000, WAKE_RFCAL);
  } else {
    Serial.println("Will wake up without radio!");
    ESP.deepSleep(5000000, WAKE_RF_DISABLED);
  }
}

void loop() {}

void readFromRTCMemory() {
  system_rtc_mem_read(RTCMEMORYSTART, &rtcMem, sizeof(rtcMem));

  Serial.print("count = ");
  Serial.println(rtcMem.count);
  yield();
}

void writeToRTCMemory() {
  if (rtcMem.count <= MAXHOUR) {
    rtcMem.count++;
  } else {
    rtcMem.count = 0;
  }

  system_rtc_mem_write(RTCMEMORYSTART, &rtcMem, 4);

  Serial.print("count = ");
  Serial.println(rtcMem.count);
  yield();
}

References