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