Note · evergreen
35 bytes at 4800 baud take 73 ms
- Stage
- evergreen
- Links
- 1 out, 0 in
The Seeed Studio XIAO two-channel energy meter pairs an ESP32-C6 with a BL0939 metering chip. ESPHome has a BL0939 component, and on this board it returned nothing. I wrote a replacement for two reasons.
Timing. The chip answers a read request with a 35-byte packet at 4800 baud, which takes about 73 ms to arrive. The stock component checks whether any byte is available and then reads 35. On the C6 the first byte shows up long before the last, the read comes back short, and the partial packet is thrown away. That repeats every cycle. The fix is to return early from the loop until all 35 bytes are waiting.
Calibration. The stock constants are for a Sonoff Dual R3, which measures current across shunt resistors and voltage through a resistive divider. This board uses 2000:1 current-transformer clamps into a 0.6 ohm burden resistor and a voltage transformer. The reference values have to be computed from the board’s own schematic.
Once it worked, the chip turned out to offer more than the stock component exposes: a fast current reading that updates every 10 to 40 ms, its own temperature, and the phase angle between voltage and current on each channel. Apparent power, reactive power and power factor follow from the power triangle.
One comment in the code I started from was wrong and worth correcting: bit 0x1000 of the MODE register disables the pulse output. It does not select 50 Hz operation.