ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
sun_gtil2.cpp
Go to the documentation of this file.
1#include "sun_gtil2.h"
6ESPHOME_LOG_TAG(TAG, "sun_gtil2");
8static const double NTC_A = 0.0011591051055979914;
9static const double NTC_B = 0.00022878183547845582;
10static const double NTC_C = 1.0396291358342124e-07;
11static const float PULLUP_RESISTANCE = 10000.0f;
12static const uint16_t ADC_MAX = 1023; // ADC of the inverter controller, not the ESP
14struct SunGTIL2Message {
15 uint16_t sync;
16 uint8_t ac_waveform[277];
17 uint8_t frequency;
18 uint16_t ac_voltage;
19 uint16_t ac_power;
20 uint16_t dc_voltage;
21 uint8_t state;
22 uint8_t unknown1;
23 uint8_t unknown2;
24 uint8_t unknown3;
25 uint8_t limiter_mode;
26 uint8_t unknown4;
27 uint16_t temperature;
28 uint32_t limiter_power;
29 uint16_t dc_power;
30 char serial_number[10];
31 uint8_t unknown5;
32 uint8_t end[39];
33} __attribute__((packed));
34
35static const uint16_t MESSAGE_SIZE = sizeof(SunGTIL2Message);
36
37static_assert(MESSAGE_SIZE == 350, "Expected the message size to be 350 bytes");
38
39void SunGTIL2::setup() { this->rx_message_.reserve(MESSAGE_SIZE); }
40
42 this->read_available([this](uint8_t c) { this->handle_char_(c); });
43}
44
45const char *SunGTIL2::state_to_string_(uint8_t state, std::span<char, STATE_BUFFER_SIZE> buffer) {
46 switch (state) {
47 case 0x02:
48 return "Starting voltage too low";
49 case 0x07:
50 return "Working";
51 default:
52 snprintf(buffer.data(), buffer.size(), "Unknown (0x%02x)", state);
53 return buffer.data();
54 }
55}
56
57float SunGTIL2::calculate_temperature_(uint16_t adc_value) {
58 if (adc_value >= ADC_MAX || adc_value == 0) {
59 return NAN;
60 }
61
62 float ntc_resistance = PULLUP_RESISTANCE / ((static_cast<float>(ADC_MAX) / adc_value) - 1.0f);
63 double lr = log(double(ntc_resistance));
64 double v = NTC_A + NTC_B * lr + NTC_C * lr * lr * lr;
65 return float(1.0 / v - 273.15);
66}
67
68void SunGTIL2::handle_char_(uint8_t c) {
69 if (this->rx_message_.size() > 1 || c == 0x07) {
70 this->rx_message_.push_back(c);
71 } else if (!this->rx_message_.empty()) {
72 this->rx_message_.clear();
73 }
74 if (this->rx_message_.size() < MESSAGE_SIZE) {
75 return;
76 }
77
78 SunGTIL2Message msg;
79 memcpy(&msg, this->rx_message_.data(), MESSAGE_SIZE);
80 this->rx_message_.clear();
81
82 if ((msg.end[0] != 0) || (msg.end[38] != 0x08))
83 return;
84
85 ESP_LOGVV(TAG, "Frequency raw value: %02x", msg.frequency);
86 ESP_LOGVV(TAG, "Unknown values: %02x %02x %02x %02x %02x", msg.unknown1, msg.unknown2, msg.unknown3, msg.unknown4,
87 msg.unknown5);
88
89#ifdef USE_SENSOR
90 if (this->ac_voltage_ != nullptr)
91 this->ac_voltage_->publish_state(__builtin_bswap16(msg.ac_voltage) / 10.0f);
92 if (this->dc_voltage_ != nullptr)
93 this->dc_voltage_->publish_state(__builtin_bswap16(msg.dc_voltage) / 8.0f);
94 if (this->ac_power_ != nullptr)
95 this->ac_power_->publish_state(__builtin_bswap16(msg.ac_power) / 10.0f);
96 if (this->dc_power_ != nullptr)
97 this->dc_power_->publish_state(__builtin_bswap16(msg.dc_power) / 10.0f);
98 if (this->limiter_power_ != nullptr)
99 this->limiter_power_->publish_state(static_cast<int32_t>(__builtin_bswap32(msg.limiter_power)) / 10.0f);
100 if (this->temperature_ != nullptr)
101 this->temperature_->publish_state(calculate_temperature_(__builtin_bswap16(msg.temperature)));
102#endif
103#ifdef USE_TEXT_SENSOR
104 if (this->state_ != nullptr) {
105 char state_buffer[STATE_BUFFER_SIZE];
106 this->state_->publish_state(this->state_to_string_(msg.state, state_buffer));
107 }
108 if (this->serial_number_ != nullptr) {
109 this->serial_number_->publish_state(msg.serial_number, 10);
110 }
111#endif
112}
113
115#ifdef USE_SENSOR
116 LOG_SENSOR("", "AC Voltage", this->ac_voltage_);
117 LOG_SENSOR("", "DC Voltage", this->dc_voltage_);
118 LOG_SENSOR("", "AC Power", this->ac_power_);
119 LOG_SENSOR("", "DC Power", this->dc_power_);
120 LOG_SENSOR("", "Limiter Power", this->limiter_power_);
121 LOG_SENSOR("", "Temperature", this->temperature_);
122#endif
123#ifdef USE_TEXT_SENSOR
124 LOG_TEXT_SENSOR("", "State", this->state_);
125 LOG_TEXT_SENSOR("", "Serial Number", this->serial_number_);
126#endif
127}
128
129} // namespace esphome::sun_gtil2
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
float calculate_temperature_(uint16_t adc_value)
Definition sun_gtil2.cpp:57
static constexpr size_t STATE_BUFFER_SIZE
Definition sun_gtil2.h:38
void handle_char_(uint8_t c)
Definition sun_gtil2.cpp:68
text_sensor::TextSensor * state_
Definition sun_gtil2.h:51
sensor::Sensor * dc_power_
Definition sun_gtil2.h:46
text_sensor::TextSensor * serial_number_
Definition sun_gtil2.h:52
sensor::Sensor * limiter_power_
Definition sun_gtil2.h:47
std::vector< uint8_t > rx_message_
Definition sun_gtil2.h:57
sensor::Sensor * dc_voltage_
Definition sun_gtil2.h:44
const char * state_to_string_(uint8_t state, std::span< char, STATE_BUFFER_SIZE > buffer)
Definition sun_gtil2.cpp:45
sensor::Sensor * ac_power_
Definition sun_gtil2.h:45
sensor::Sensor * ac_voltage_
Definition sun_gtil2.h:43
sensor::Sensor * temperature_
Definition sun_gtil2.h:48
void publish_state(const std::string &state)
Definition text_sensor.h:42
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
Definition uart.h:77
struct @66::@67 __attribute__
Wake the main loop task from an ISR. ISR-safe.
Definition main_task.h:45
bool state
Definition fan.h:2
uint32_t buffer
Definition gif.cpp:50
ESPHOME_LOG_TAG(TAG, "sun_gtil2")
static void uint32_t