RTC Memory

dev board WeMos D1 Mini
chip ESP8266
features RTC

Before starting

Dependancies

Ensure the following requirements are available:

Pre-requisites

Try these simpler examples:

Buy the components

Description

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();
}

Makefile

BOARD?=esp8266com:esp8266:d1_mini
PORT?=/dev/cu.wchusbserial1410
BUILD=build
# Arduino CLI version 0.14.0 is used.

.PHONY: default lint all flash clean

default: lint all flash clean

lint:
	cpplint --extensions=ino --filter=-legal/copyright *.ino

all:
	arduino-cli compile --fqbn $(BOARD) --output-dir $(BUILD) ./

flash:
	arduino-cli upload --fqbn $(BOARD) --port $(PORT) --input-dir $(BUILD)

clean:
	rm -r build

References