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