ESPHome  2022.11.3
bl0942.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 namespace esphome {
9 namespace bl0942 {
10 
11 static const float BL0942_PREF = 596; // taken from tasmota
12 static const float BL0942_UREF = 15873.35944299; // should be 73989/1.218
13 static const float BL0942_IREF = 251213.46469622; // 305978/1.218
14 static const float BL0942_EREF = 3304.61127328; // Measured
15 
16 struct DataPacket {
17  uint8_t frame_header;
24  uint8_t reserved1;
25  uint8_t status;
26  uint8_t reserved2;
27  uint8_t reserved3;
28  uint8_t checksum;
29 } __attribute__((packed));
30 
31 class BL0942 : public PollingComponent, public uart::UARTDevice {
32  public:
33  void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; }
34  void set_current_sensor(sensor::Sensor *current_sensor) { current_sensor_ = current_sensor; }
35  void set_power_sensor(sensor::Sensor *power_sensor) { power_sensor_ = power_sensor; }
36  void set_energy_sensor(sensor::Sensor *energy_sensor) { energy_sensor_ = energy_sensor; }
37  void set_frequency_sensor(sensor::Sensor *frequency_sensor) { frequency_sensor_ = frequency_sensor; }
38 
39  void loop() override;
40 
41  void update() override;
42  void setup() override;
43  void dump_config() override;
44 
45  protected:
46  sensor::Sensor *voltage_sensor_{nullptr};
47  sensor::Sensor *current_sensor_{nullptr};
48  // NB This may be negative as the circuits is seemingly able to measure
49  // power in both directions
50  sensor::Sensor *power_sensor_{nullptr};
51  sensor::Sensor *energy_sensor_{nullptr};
52  sensor::Sensor *frequency_sensor_{nullptr};
53 
54  // Divide by this to turn into Watt
55  float power_reference_ = BL0942_PREF;
56  // Divide by this to turn into Volt
57  float voltage_reference_ = BL0942_UREF;
58  // Divide by this to turn into Ampere
59  float current_reference_ = BL0942_IREF;
60  // Divide by this to turn into kWh
61  float energy_reference_ = BL0942_EREF;
62 
63  static bool validate_checksum(DataPacket *data);
64 
65  void received_package_(DataPacket *data);
66 };
67 } // namespace bl0942
68 } // namespace esphome
void setup()
void loop()
internal::LittleEndianLayout< uint16_t > uint16_le_t
Definition: datatypes.h:55
esphome::bl0942::BL0942 __attribute__
uint24_le_t i_fast_rms
Definition: bl0942.h:20
uint16_le_t frequency
Definition: bl0942.h:23
This class simplifies creating components that periodically check a state.
Definition: component.h:267
void set_current_sensor(sensor::Sensor *current_sensor)
Definition: bl0942.h:34
void set_frequency_sensor(sensor::Sensor *frequency_sensor)
Definition: bl0942.h:37
internal::LittleEndianLayout< uint24_t > uint24_le_t
Definition: datatypes.h:54
void set_energy_sensor(sensor::Sensor *energy_sensor)
Definition: bl0942.h:36
void set_power_sensor(sensor::Sensor *power_sensor)
Definition: bl0942.h:35
Definition: a4988.cpp:4
internal::LittleEndianLayout< int24_t > int24_le_t
Definition: datatypes.h:58
Base-class for all sensors.
Definition: sensor.h:48
void set_voltage_sensor(sensor::Sensor *voltage_sensor)
Definition: bl0942.h:33