RTC Counter
Counts how many times the device has woken up from deep sleep. The counter is stored in RTC memory, so it survives deep sleep but resets on power loss. Uses the kv, schema, and sleep APIs.
Hardware
- Any ESP32 board
- USB cable
Code
import {rtcStorage} from 'mikro/kv/rtc'
import * as s from 'mikro/schema'
import {deepSleep, sleep} from 'mikro/sleep'
// Read the wake counter from RTC memory (survives deep sleep).
// optional() because the key may not exist on first boot.
const count = rtcStorage.createValue('count', {schema: s.optional(s.number())})
console.log(`Wake #${count.get() ?? 0}`)
console.log(`RTC memory: ${rtcStorage.info().used}/${rtcStorage.info().total} bytes used`)
// Increment and store back
count.update((n) => (n ?? 0) + 1).orPanic('failed to store count')
console.log('Waiting for a few seconds before going to deep sleep...')
await sleep(5000)
// Sleep for 15 seconds, then wake up again
console.log('Going to deep sleep for 15s...')
deepSleep({timer: 15_000})Walkthrough
RTC-backed key/value.
rtcStorage.createValue('count', {schema})returns a typed handle whose value lives in RTC memory.optional(number())makes the schema acceptundefinedso the first boot reads cleanly.Read.
count.get()returns the stored value orundefined. The?? 0defaults the first boot to0.Update.
count.update(fn)reads, transforms, and writes atomically. Returns aResult, so.orPanic()halts with a clear message if the write fails.Deep sleep.
deepSleep({timer: 15_000})powers down the CPU for 15 seconds (the value is in milliseconds). RTC memory is preserved; everything else is lost. On wake, the script runs from the top.
Create project
pnpm create mikro --template rtc-counternpm create mikro -- --template rtc-counteryarn create mikro --template rtc-counterbun create mikro --template rtc-counterRun it
pnpm install
pnpm mikro flash # only needed once per board
pnpm mikro devnpm install
npx mikro flash # only needed once per board
npx mikro devyarn install
yarn mikro flash # only needed once per board
yarn mikro devbun install
bunx mikro flash # only needed once per board
bunx mikro devThe console shows Wake #0, then after each deep sleep cycle Wake #1, Wake #2, and so on. Power-cycle the board to reset the counter.