Skip to content

fix: MR60BHA2 UART resync and presence (#7, #8) - #18

Draft
limengdu wants to merge 1 commit into
mainfrom
limengdu-fix-bha2-uart-4a80
Draft

limengdu wants to merge 1 commit into
mainfrom
limengdu-fix-bha2-uart-4a80

Conversation

@limengdu

Copy link
Copy Markdown
Collaborator

Fixes #7
Fixes #8

This PR keeps the existing Arduino polling API and fixes the two library-level bugs that match those reports. It does not change ESPHome YAML and does not touch Matter (#3).

Causes

#8 “sometimes no values” / NaN after plug-in

Three things in this library can leave the host with no vital-sign updates until a power cycle (including swapping the kit’s second USB-C port):

  1. Weak UART lock. fetch() treated every 0x01 as SOF and only checked checksums after the declared payload arrived. A 0x01 in boot banner / reset noise with a large LEN swallowed the following real frames. Parser state was also static, so it survived across begin() calls.
  2. No drain after begin(). The radar and the XIAO share UART. Default rst = -1 does not reset the module, and leftover boot bytes stayed in the 32 KB RX buffer.
  3. Uninitialized presence / people-count flags and an unbounded target_num on 0x0A04 / 0x0A08. A huge count can allocate or walk off the payload and hang the MCU.

The “other USB-C port unblocks it” report is consistent with a power-on race: one port restarts the radar, the other the ESP, and a lucky boot happens to start on a real SOF.

#7 “disabling api: leaves only Target Number”

ESPHome’s seeed_mr60bha2 component has its own UART parser (this library is not linked into that firmware). The same symptoms still come from this driver when the host loop is slower (MQTT-only, reconnect stalls, long update() gaps):

  1. Queue drop. People-count frames (0x0A04) arrive much more often than heart / breath (0x0A14 / 0x0A15). When byteQueue hit MMWaveMaxQueueSize (120), the oldest frame was discarded. Sparse vitals were the ones that disappeared; the latest target count survived.
  2. Type-filtered waits ate vitals. fetchType() / processQueuedFrames(type) returned before handleType() on non-matching frames, so a command ACK wait dropped heart / breath already in the queue.
  3. isHumanDetected() consumed the “new data” flag. After one read it returned false even when a person was still present. Wrappers that gate heart / breath on that call then stop publishing everything except target count.

I2C illuminance continuing to work matches both reports: BH1750 never goes through this UART path.

Fixes

  • New Arduino-free FrameCodec + FrameAssembler: validate HEAD_CKSUM at 8 bytes, reject LEN > 512 immediately, resync by scanning leftover bytes for the next 0x01.
  • begin() waits briefly, drains UART, and calls resync() (public, so a sketch can recover without resetting the MCU).
  • Queue overflow applies the oldest frame through handleType() before dropping it.
  • Every checksum-valid frame is dispatched, including during a type-filtered wait.
  • isHumanDetected() returns the last reported presence (same idea as SEEED_MR60FDA2::getHuman()).
  • Payload length checks; people-count / point-cloud target_num is capped by payload size and MAX_TARGET_NUM / MAX_POINT_CLOUD_NUM.
  • Host unit tests (pio test -e native) and GitHub Actions CI (host tests, arduino-lint --compliance specification, compile examples for XIAO ESP32-C6).

Testing already run

pio test -e native

17/17 host tests passed, including:

  • valid heart-rate frame after boot garbage containing 0x01
  • forged oversized LEN that must not consume the next real frame
  • 40 target-count frames followed by a heart-rate frame (the heart-rate frame still assembles)
  • SOF bytes inside a valid payload stay in that frame

Local arduino-lint --compliance specification --library-manager submit from a Seeed-mmWave-library folder: 0 errors (existing name-has-“Arduino” / spaces warnings only).

Needs hardware verification

This environment has no MR60BHA2 / MR60FDA2 kit. Please confirm on a real XIAO ESP32-C6 + MR60BHA2:

  1. sometimes no values #8: Power from the XIAO USB-C only, then from the carrier USB-C. After mmWave.begin(&mmWaveSerial), examples/mmwaveBreath should print heart / breath / distance within ~60 s of a person sitting still (the module’s own lock time). Repeat cold-plug 10 times; values should appear without swapping ports. If the stream is still silent, call mmWave.resync() once after a 200 ms delay.
  2. disableing api disables the mr60bha2 sensor #7 (Arduino stand-in for a slow host): Run update(100) only every 1–2 s (or add a long MQTT-style delay). Target count, presence, heart rate, and breath rate should all keep updating; presence should stay true while a person is in range.
  3. disableing api disables the mr60bha2 sensor #7 (ESPHome YAML): Disabling api: and using MQTT is an ESPHome app concern. After this library ships, Arduino/custom-component users are covered. Official / limengdu ESPHome components copy the protocol and do not link this repo — if MQTT-only still shows only Target Number on stock ESPHome firmware, that needs a change in MR60BHA2_ESPHome_external_components or esphome/esphome (their has_target gate and “publish only if != 0” filters).
  4. Regression: examples/PeopleCounting_demo still reports 1–3 targets. examples/fall_demo still reads fall / human. setInstallationHeight / setThreshold / setSensitivity on MR60FDA2 still get ACKs.

Do not merge until those kit checks pass.

Open in Web Open in Cursor 

Recover from boot-time UART noise by checking HEAD_CKSUM at eight bytes
and re-locking onto the next SOF. Apply completed frames when the queue
is full so sparse heart/breath reports survive a delayed host loop.
isHumanDetected() returns the last reported presence.

Co-authored-by: Citric Li <limengdu@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sometimes no values disableing api disables the mr60bha2 sensor

2 participants