ESPHome 2025.5.0
Loading...
Searching...
No Matches
growatt_solar.cpp
Go to the documentation of this file.
1#include "growatt_solar.h"
2#include "esphome/core/log.h"
4
5namespace esphome {
6namespace growatt_solar {
7
8static const char *const TAG = "growatt_solar";
9
10static const uint8_t MODBUS_CMD_READ_IN_REGISTERS = 0x04;
11static const uint8_t MODBUS_REGISTER_COUNT[] = {33, 95}; // indexed with enum GrowattProtocolVersion
12
14 // If update() was unable to send we retry until we can send.
15 if (!this->waiting_to_update_)
16 return;
17 update();
18}
19
21 // If our last send has had no reply yet, and it wasn't that long ago, do nothing.
22 const uint32_t now = App.get_loop_component_start_time();
23 if (now - this->last_send_ < this->get_update_interval() / 2) {
24 return;
25 }
26
27 // The bus might be slow, or there might be other devices, or other components might be talking to our device.
28 if (this->waiting_for_response()) {
29 this->waiting_to_update_ = true;
30 return;
31 }
32
33 this->waiting_to_update_ = false;
34 this->send(MODBUS_CMD_READ_IN_REGISTERS, 0, MODBUS_REGISTER_COUNT[this->protocol_version_]);
35 this->last_send_ = millis();
36}
37
38void GrowattSolar::on_modbus_data(const std::vector<uint8_t> &data) {
39 // Other components might be sending commands to our device. But we don't get called with enough
40 // context to know what is what. So if we didn't do a send, we ignore the data.
41 if (!this->last_send_)
42 return;
43 this->last_send_ = 0;
44
45 // Also ignore the data if the message is too short. Otherwise we will publish invalid values.
46 if (data.size() < MODBUS_REGISTER_COUNT[this->protocol_version_] * 2)
47 return;
48
49 auto publish_1_reg_sensor_state = [&](sensor::Sensor *sensor, size_t i, float unit) -> void {
50 if (sensor == nullptr)
51 return;
52 float value = encode_uint16(data[i * 2], data[i * 2 + 1]) * unit;
53 sensor->publish_state(value);
54 };
55
56 auto publish_2_reg_sensor_state = [&](sensor::Sensor *sensor, size_t reg1, size_t reg2, float unit) -> void {
57 float value = ((encode_uint16(data[reg1 * 2], data[reg1 * 2 + 1]) << 16) +
58 encode_uint16(data[reg2 * 2], data[reg2 * 2 + 1])) *
59 unit;
60 if (sensor != nullptr)
61 sensor->publish_state(value);
62 };
63
64 switch (this->protocol_version_) {
65 case RTU: {
66 publish_1_reg_sensor_state(this->inverter_status_, 0, 1);
67
68 publish_2_reg_sensor_state(this->pv_active_power_sensor_, 1, 2, ONE_DEC_UNIT);
69
70 publish_1_reg_sensor_state(this->pvs_[0].voltage_sensor_, 3, ONE_DEC_UNIT);
71 publish_1_reg_sensor_state(this->pvs_[0].current_sensor_, 4, ONE_DEC_UNIT);
72 publish_2_reg_sensor_state(this->pvs_[0].active_power_sensor_, 5, 6, ONE_DEC_UNIT);
73
74 publish_1_reg_sensor_state(this->pvs_[1].voltage_sensor_, 7, ONE_DEC_UNIT);
75 publish_1_reg_sensor_state(this->pvs_[1].current_sensor_, 8, ONE_DEC_UNIT);
76 publish_2_reg_sensor_state(this->pvs_[1].active_power_sensor_, 9, 10, ONE_DEC_UNIT);
77
78 publish_2_reg_sensor_state(this->grid_active_power_sensor_, 11, 12, ONE_DEC_UNIT);
79 publish_1_reg_sensor_state(this->grid_frequency_sensor_, 13, TWO_DEC_UNIT);
80
81 publish_1_reg_sensor_state(this->phases_[0].voltage_sensor_, 14, ONE_DEC_UNIT);
82 publish_1_reg_sensor_state(this->phases_[0].current_sensor_, 15, ONE_DEC_UNIT);
83 publish_2_reg_sensor_state(this->phases_[0].active_power_sensor_, 16, 17, ONE_DEC_UNIT);
84
85 publish_1_reg_sensor_state(this->phases_[1].voltage_sensor_, 18, ONE_DEC_UNIT);
86 publish_1_reg_sensor_state(this->phases_[1].current_sensor_, 19, ONE_DEC_UNIT);
87 publish_2_reg_sensor_state(this->phases_[1].active_power_sensor_, 20, 21, ONE_DEC_UNIT);
88
89 publish_1_reg_sensor_state(this->phases_[2].voltage_sensor_, 22, ONE_DEC_UNIT);
90 publish_1_reg_sensor_state(this->phases_[2].current_sensor_, 23, ONE_DEC_UNIT);
91 publish_2_reg_sensor_state(this->phases_[2].active_power_sensor_, 24, 25, ONE_DEC_UNIT);
92
93 publish_2_reg_sensor_state(this->today_production_, 26, 27, ONE_DEC_UNIT);
94 publish_2_reg_sensor_state(this->total_energy_production_, 28, 29, ONE_DEC_UNIT);
95
96 publish_1_reg_sensor_state(this->inverter_module_temp_, 32, ONE_DEC_UNIT);
97 break;
98 }
99 case RTU2: {
100 publish_1_reg_sensor_state(this->inverter_status_, 0, 1);
101
102 publish_2_reg_sensor_state(this->pv_active_power_sensor_, 1, 2, ONE_DEC_UNIT);
103
104 publish_1_reg_sensor_state(this->pvs_[0].voltage_sensor_, 3, ONE_DEC_UNIT);
105 publish_1_reg_sensor_state(this->pvs_[0].current_sensor_, 4, ONE_DEC_UNIT);
106 publish_2_reg_sensor_state(this->pvs_[0].active_power_sensor_, 5, 6, ONE_DEC_UNIT);
107
108 publish_1_reg_sensor_state(this->pvs_[1].voltage_sensor_, 7, ONE_DEC_UNIT);
109 publish_1_reg_sensor_state(this->pvs_[1].current_sensor_, 8, ONE_DEC_UNIT);
110 publish_2_reg_sensor_state(this->pvs_[1].active_power_sensor_, 9, 10, ONE_DEC_UNIT);
111
112 publish_2_reg_sensor_state(this->grid_active_power_sensor_, 35, 36, ONE_DEC_UNIT);
113 publish_1_reg_sensor_state(this->grid_frequency_sensor_, 37, TWO_DEC_UNIT);
114
115 publish_1_reg_sensor_state(this->phases_[0].voltage_sensor_, 38, ONE_DEC_UNIT);
116 publish_1_reg_sensor_state(this->phases_[0].current_sensor_, 39, ONE_DEC_UNIT);
117 publish_2_reg_sensor_state(this->phases_[0].active_power_sensor_, 40, 41, ONE_DEC_UNIT);
118
119 publish_1_reg_sensor_state(this->phases_[1].voltage_sensor_, 42, ONE_DEC_UNIT);
120 publish_1_reg_sensor_state(this->phases_[1].current_sensor_, 43, ONE_DEC_UNIT);
121 publish_2_reg_sensor_state(this->phases_[1].active_power_sensor_, 44, 45, ONE_DEC_UNIT);
122
123 publish_1_reg_sensor_state(this->phases_[2].voltage_sensor_, 46, ONE_DEC_UNIT);
124 publish_1_reg_sensor_state(this->phases_[2].current_sensor_, 47, ONE_DEC_UNIT);
125 publish_2_reg_sensor_state(this->phases_[2].active_power_sensor_, 48, 49, ONE_DEC_UNIT);
126
127 publish_2_reg_sensor_state(this->today_production_, 53, 54, ONE_DEC_UNIT);
128 publish_2_reg_sensor_state(this->total_energy_production_, 55, 56, ONE_DEC_UNIT);
129
130 publish_1_reg_sensor_state(this->inverter_module_temp_, 93, ONE_DEC_UNIT);
131 break;
132 }
133 }
134}
135
137 ESP_LOGCONFIG(TAG, "GROWATT Solar:");
138 ESP_LOGCONFIG(TAG, " Address: 0x%02X", this->address_);
139}
140
141} // namespace growatt_solar
142} // namespace esphome
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
virtual uint32_t get_update_interval() const
Get the update interval in ms of this sensor.
void on_modbus_data(const std::vector< uint8_t > &data) override
struct esphome::growatt_solar::GrowattSolar::GrowattPhase phases_[3]
GrowattProtocolVersion protocol_version_
struct esphome::growatt_solar::GrowattSolar::GrowattPV pvs_[2]
void send(uint8_t function, uint16_t start_address, uint16_t number_of_entities, uint8_t payload_len=0, const uint8_t *payload=nullptr)
Definition modbus.h:62
Base-class for all sensors.
Definition sensor.h:57
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:191
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:27
Application App
Global storage of Application pointer - only one Application can exist.