ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
bl0939.h
Go to the documentation of this file.
1#pragma once
7namespace esphome::bl0939 {
9// https://datasheet.lcsc.com/lcsc/2108071830_BL-Shanghai-Belling-BL0939_C2841044.pdf
10// (unfortunately chinese, but the formulas can be easily understood)
11// The datasheet has one set of formulas for direct mode (current shunt) and one for current transformer mode.
13// Direct mode.
14// Sonoff Dual R3 V2 has the exact same resistor values for the current shunts (RL=1miliOhm)
15// and for the voltage divider (R1=0.51kOhm, R2=5*390kOhm)
16// as in the manufacturer's reference circuit, so the same formulas were used here (Vref=1.218V)
17static constexpr float BL0939_IREF = 324004 * 1 / 1.218;
18static constexpr float BL0939_UREF = 79931 * 0.51 * 1000 / (1.218 * (5 * 390 + 0.51));
19static constexpr float BL0939_PREF = 4046 * 1 * 0.51 * 1000 / (1.218 * 1.218 * (5 * 390 + 0.51));
20static constexpr float BL0939_EREF = 3.6e6 * 4046 * 1 * 0.51 * 1000 / (1638.4 * 256 * 1.218 * 1.218 * (5 * 390 + 0.51));
21
22// Current transformer mode.
23// Values from the Seeed Studio XIAO 2-Channel Wi-Fi AC Energy Meter V1.0 schematic (Vref=1.218V).
24// Current: 100A:50mA CT (2000:1) into a 0.6Ohm burden (2 * 0.3Ohm), so 0.3mV per primary amp.
25// That is the same as a 0.3mOhm shunt in the direct mode formula.
26// Voltage: ZMPT107-1 2mA:2mA voltage transformer with R2=5*22kOhm on the primary and R1=24.9Ohm on
27// the secondary. The transformer isolates R1 from R2, so the divider is R1/R2 instead of R1/(R1+R2).
28static constexpr float BL0939_IREF_CT = 324004 * 0.3 / 1.218;
29static constexpr float BL0939_UREF_CT = 79931 * 24.9 * 1000 / (1.218 * 5 * 22000);
30static constexpr float BL0939_PREF_CT = 4046 * 0.3 * 24.9 * 1000 / (1.218 * 1.218 * 5 * 22000);
31static constexpr float BL0939_EREF_CT = 3.6e6 * 4046 * 0.3 * 24.9 * 1000 / (1638.4 * 256 * 1.218 * 1.218 * 5 * 22000);
32
33enum class BL0939Mode : uint8_t {
36};
37
38struct ube24_t { // NOLINT(readability-identifier-naming,altera-struct-pack-align)
39 uint8_t l;
40 uint8_t m;
41 uint8_t h;
42} __attribute__((packed));
43
44struct ube16_t { // NOLINT(readability-identifier-naming,altera-struct-pack-align)
45 uint8_t l;
46 uint8_t h;
47} __attribute__((packed));
48
49struct sbe24_t { // NOLINT(readability-identifier-naming,altera-struct-pack-align)
50 uint8_t l;
51 uint8_t m;
52 int8_t h;
53} __attribute__((packed));
54
55// Caveat: All these values are big endian (low - middle - high)
56
57union DataPacket { // NOLINT(altera-struct-pack-align)
58 uint8_t raw[35];
59 struct {
60 uint8_t frame_header; // 0x55 according to docs
71 uint8_t RESERVED1; // value of 0x00
73 uint8_t RESERVED2; // value of 0x00
74 uint8_t checksum; // checksum
75 };
76} __attribute__((packed));
77
78class BL0939 final : public PollingComponent, public uart::UARTDevice {
79 public:
80 SUB_SENSOR(voltage)
81 SUB_SENSOR(current_1)
82 SUB_SENSOR(current_2)
83 // NB This may be negative as the circuits is seemingly able to measure
84 // power in both directions
85 SUB_SENSOR(power_1)
86 SUB_SENSOR(power_2)
87 SUB_SENSOR(energy_1)
88 SUB_SENSOR(energy_2)
89 SUB_SENSOR(energy_sum)
90 SUB_SENSOR(internal_temperature)
91 // Inline so a constant mode from codegen folds down to plain stores.
92 void set_mode(BL0939Mode mode) {
93 this->mode_ = mode;
94 const bool current_transformer = mode == BL0939Mode::BL0939_MODE_CURRENT_TRANSFORMER;
95 this->power_reference_ = current_transformer ? BL0939_PREF_CT : BL0939_PREF;
96 this->voltage_reference_ = current_transformer ? BL0939_UREF_CT : BL0939_UREF;
97 this->current_reference_ = current_transformer ? BL0939_IREF_CT : BL0939_IREF;
98 this->energy_reference_ = current_transformer ? BL0939_EREF_CT : BL0939_EREF;
99 }
100
101 void loop() override;
102
103 void update() override;
104 void setup() override;
105 void dump_config() override;
106
107 protected:
108 // Divide by this to turn into Watt
109 float power_reference_ = BL0939_PREF;
110 // Divide by this to turn into Volt
111 float voltage_reference_ = BL0939_UREF;
112 // Divide by this to turn into Ampere
113 float current_reference_ = BL0939_IREF;
114 // Divide by this to turn into kWh
115 float energy_reference_ = BL0939_EREF;
117
118 static uint32_t to_uint32_t(ube24_t input);
119
120 static int32_t to_int32_t(sbe24_t input);
121
122 static bool validate_checksum(const DataPacket *data);
123
124 void received_package_(const DataPacket *data) const;
126} // namespace esphome::bl0939
BedjetMode mode
BedJet operating mode.
This class simplifies creating components that periodically check a state.
Definition component.h:532
void update() override
Definition bl0939.cpp:64
static int32_t to_int32_t(sbe24_t input)
Definition bl0939.cpp:138
void setup() override
Definition bl0939.cpp:70
void loop() override
Definition bl0939.cpp:39
SUB_SENSOR(voltage) SUB_SENSOR(current_1) SUB_SENSOR(current_2) SUB_SENSOR(power_1) SUB_SENSOR(power_2) SUB_SENSOR(energy_1) SUB_SENSOR(energy_2) SUB_SENSOR(energy_sum) SUB_SENSOR(internal_temperature) void set_mode(BL0939Mode mode)
Definition bl0939.h:80
static bool validate_checksum(const DataPacket *data)
Definition bl0939.cpp:55
static uint32_t to_uint32_t(ube24_t input)
Definition bl0939.cpp:136
void received_package_(const DataPacket *data) const
Definition bl0939.cpp:80
void dump_config() override
Definition bl0939.cpp:119
esphome::bl0939::BL0939 __attribute__
static void uint32_t