ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
bl0906.h
Go to the documentation of this file.
1#pragma once
7
8// https://www.belling.com.cn/media/file_object/bel_product/BL0906/datasheet/BL0906_V1.02_cn.pdf
9// https://www.belling.com.cn/media/file_object/bel_product/BL0906/guide/BL0906%20APP%20Note_V1.02.pdf
10
11namespace esphome::bl0906 {
12
13// Stage values for the read state machine. After STAGE_CHANNEL_6 the state machine
14// jumps to the two sentinel stages below, then to STAGE_IDLE which marks the cycle
15// as complete and disables the loop.
16enum BL0906Stage : uint8_t {
17 STAGE_TEMP = 0, // chip temperature
18 STAGE_CHANNEL_1 = 1, // per-phase current + power + energy
24 STAGE_FREQ = UINT8_MAX - 2, // frequency + voltage
25 STAGE_POWER = UINT8_MAX - 1, // total power + total energy
26 STAGE_IDLE = UINT8_MAX, // cycle complete
27};
28
29struct DataPacket { // NOLINT(altera-struct-pack-align)
30 uint8_t l{0};
31 uint8_t m{0};
32 uint8_t h{0};
33 uint8_t checksum; // checksum
34 uint8_t address;
35} __attribute__((packed));
36
37struct ube24_t { // NOLINT(readability-identifier-naming,altera-struct-pack-align)
38 uint8_t l{0};
39 uint8_t m{0};
40 uint8_t h{0};
41} __attribute__((packed));
42
43struct sbe24_t { // NOLINT(readability-identifier-naming,altera-struct-pack-align)
44 uint8_t l{0};
45 uint8_t m{0};
46 int8_t h{0};
47} __attribute__((packed));
48
49class BL0906;
50
51using ActionCallbackFuncPtr = void (BL0906::*)();
52
53class BL0906 final : public PollingComponent, public uart::UARTDevice {
54 SUB_SENSOR(voltage)
55 SUB_SENSOR(current_1)
56 SUB_SENSOR(current_2)
57 SUB_SENSOR(current_3)
58 SUB_SENSOR(current_4)
59 SUB_SENSOR(current_5)
60 SUB_SENSOR(current_6)
61 SUB_SENSOR(power_1)
62 SUB_SENSOR(power_2)
63 SUB_SENSOR(power_3)
64 SUB_SENSOR(power_4)
65 SUB_SENSOR(power_5)
66 SUB_SENSOR(power_6)
67 SUB_SENSOR(total_power)
68 SUB_SENSOR(energy_1)
69 SUB_SENSOR(energy_2)
70 SUB_SENSOR(energy_3)
71 SUB_SENSOR(energy_4)
72 SUB_SENSOR(energy_5)
73 SUB_SENSOR(energy_6)
74 SUB_SENSOR(total_energy)
75 SUB_SENSOR(frequency)
76 SUB_SENSOR(temperature)
77
78 public:
79 void loop() override;
80
81 void update() override;
82 void setup() override;
83 void dump_config() override;
84
86 void reset_energy() { this->enqueue_action_(&BL0906::reset_energy_); }
87
88 protected:
89 void reset_energy_();
90
91 void read_data_(uint8_t address, float reference, sensor::Sensor *sensor);
92
93 void bias_correction_(uint8_t address, float measurements, float correction);
94
96 void advance_stage_();
98 void handle_actions_();
99
100 private:
101 std::vector<ActionCallbackFuncPtr> action_queue_{};
103
104} // namespace esphome::bl0906
uint8_t address
Definition bl0906.h:4
uint16_le_t frequency
Definition bl0942.h:6
virtual void loop()
This method will be called repeatedly.
Definition component.cpp:86
This class simplifies creating components that periodically check a state.
Definition component.h:519
void read_data_(uint8_t address, float reference, sensor::Sensor *sensor)
Definition bl0906.cpp:151
BL0906Stage current_stage_
Definition bl0906.h:95
void bias_correction_(uint8_t address, float measurements, float correction)
Definition bl0906.cpp:211
size_t enqueue_action_(ActionCallbackFuncPtr function)
Definition bl0906.cpp:115
Base-class for all sensors.
Definition sensor.h:47
void(BL0906::*)() ActionCallbackFuncPtr
Definition bl0906.h:51
esphome::bl0906::BL0906 __attribute__
uint16_t temperature
Definition sun_gtil2.cpp:12