An ESP32-S3 and a 60 GHz radar build a self-hosted sleep monitor with device shadow
A non-contact sleep monitor removes the wearable and the camera and leaves a radar module, a microcontroller and a software stack. One published build documents the whole path: a Yunfan R60ABD2 60 GHz sleep radar, an ESP32-S3, a self-hosted EMQX broker and FastAPI backend, and a Kotlin Android app. The radar module and its output categories are verified against the vendor's public documents; the build itself was not reproduced or measured by us.
The radar and the parts around it
Yunfan Ruida describes the R60ABD2 as a 60 GHz sleep-monitoring module using 1T3R FMCW to sense respiration, heart rate, movement and sleep state. Its public page and data sheet list respiration and sleep analysis as the primary functions. The project pairs it with an ESP32-S3 over UART at 115200 8N1, a WS2812 for the night light, a photoresistor on an ADC input for ambient brightness and a momentary key for provisioning.
The radar speaks a fixed frame format with a two-byte header, control and command bytes, a big-endian length, a data field, an 8-bit additive checksum and a two-byte tail. The firmware feeds bytes into a parser state machine rather than assuming one read returns a whole frame. It first sends a sleep-mode command so the radar reports on its own, then queries presence, respiration, heart rate, in-bed state and the overnight report as needed.
The counter that resets is the real design problem
The module keeps one cumulative counter for the current sleep session. Wake, light and deep sleep times start at zero after the user gets into bed, and are pushed out as an overnight report and cleared when the user leaves bed. A person who gets up during the night would therefore split the night into fragments if the controller simply forwarded the radar totals.
The build adds a session state machine with cross-session accumulation. A statistical day runs from 08:00 to 08:00. Before the radar clears its counter, the firmware adds the wake, light and deep increments to a daily total. An overnight report is deduplicated by a fingerprint of the accumulated leave-bed duration and count, so a report replayed after a reboot is only counted once. When NTP has not synced, the device keeps computing in memory but does not archive, write to NVS or upload, avoiding records dated 1970.
Scoring and alerts on the device
The sleep score is a four-part weighted sum out of 100: duration 55, deep-sleep share 25, awake share 15 and leave-bed count 5. Full marks for duration fall between 7.5 and 8.5 hours; deep sleep scores highest at 20 to 30 percent; awake share of 40 percent or more scores zero. The published C function implements this directly, and the project notes the scoring is explainable, tunable and runs on-device.
Health alerts use consecutive-sample confirmation. Heart rate above 110 or below 50 BPM, respiration above 24 or below 8 per minute, and a reported apnoea event each need three consecutive abnormal samples, except apnoea which is raised from the radar status. Alerts are only evaluated in bed, care alerts clear after ten minutes without confirmation, and only the most severe alert is exposed at a time. The night light turns on for a fixed 30 seconds when the photoresistor raw value exceeds 1500 and body movement exceeds 30, and does not extend on further movement.
Identity, auth and the device shadow
Device ID is derived at runtime from the chip MAC as sm- plus twelve lowercase hex digits. The MQTT password is SHA-256 of a product secret and the device ID. The backend checks the same hash through an EMQX HTTP auth callback and auto-registers a device on first successful connect, so a thousand units share one firmware but not one credential. EMQX ACLs restrict each device to its own up, down and ack topics, which limits what a extracted device can read.
A device shadow gives the app one endpoint to read. It holds online state, the latest real-time and daily fields, the last seven nights, firmware version and the most recent successfully delivered desired state. Commands flow from the app through the backend to the down topic and back on ack, with a four-second timeout and distinct 401, 404, 409, 504 and 502 error codes. The SQLite schema keeps only the newest frame in one table and keyed night reports in another, so repeated uploads overwrite rather than duplicate; WAL mode is cited as sufficient for roughly 300 writes per second at a thousand devices.
What is solid and what is a project claim
The transferable engineering is in the boundaries: a session counter that cannot be trusted across night-time wakeups, a device identity that avoids per-unit firmware, ACLs that turn a shared secret into per-device credentials, and a shadow that keeps the app away from MQTT details. These are patterns that apply to any radar-based presence or sleep build.
The health thresholds and the scoring weights are this project's choices, not validated clinical limits. The radar vendor's public material describes respiration, heart-rate and sleep outputs; it does not establish that these thresholds detect apnoea or that the score correlates with clinical sleep quality. Anyone adapting the build for care would have to validate those rules separately and treat the current values as configurable starting points.
This article is part of the mmWave Radar channel.