7 static const char *
const TAG =
"sun_gtil2";
9 static const double NTC_A = 0.0011591051055979914;
10 static const double NTC_B = 0.00022878183547845582;
11 static const double NTC_C = 1.0396291358342124e-07;
12 static const float PULLUP_RESISTANCE = 10000.0f;
13 static const uint16_t ADC_MAX = 1023;
15 struct SunGTIL2Message {
36 static const uint16_t MESSAGE_SIZE =
sizeof(SunGTIL2Message);
38 static_assert(MESSAGE_SIZE == 350,
"Expected the message size to be 350 bytes");
43 while (this->available()) {
46 this->handle_char_(c);
53 return "Starting voltage too low";
62 if (adc_value >= ADC_MAX || adc_value == 0) {
66 float ntc_resistance = PULLUP_RESISTANCE / ((
static_cast<float>(ADC_MAX) / adc_value) - 1.0f);
67 double lr = log(
double(ntc_resistance));
68 double v = NTC_A + NTC_B * lr + NTC_C * lr * lr * lr;
69 return float(1.0 / v - 273.15);
73 if (this->rx_message_.size() > 1 || c == 0x07) {
74 this->rx_message_.push_back(c);
75 }
else if (!this->rx_message_.empty()) {
76 this->rx_message_.clear();
78 if (this->rx_message_.size() < MESSAGE_SIZE) {
83 memcpy(&msg, this->rx_message_.data(), MESSAGE_SIZE);
84 this->rx_message_.clear();
86 if (!((msg.end[0] == 0) && (msg.end[38] == 0x08)))
89 ESP_LOGVV(TAG,
"Frequency raw value: %02x", msg.frequency);
90 ESP_LOGVV(TAG,
"Unknown values: %02x %02x %02x %02x %02x", msg.unknown1, msg.unknown2, msg.unknown3, msg.unknown4,
94 if (this->ac_voltage_ !=
nullptr)
95 this->ac_voltage_->publish_state(__builtin_bswap16(msg.ac_voltage) / 10.0f);
96 if (this->dc_voltage_ !=
nullptr)
97 this->dc_voltage_->publish_state(__builtin_bswap16(msg.dc_voltage) / 8.0f);
98 if (this->ac_power_ !=
nullptr)
99 this->ac_power_->publish_state(__builtin_bswap16(msg.ac_power) / 10.0f);
100 if (this->dc_power_ !=
nullptr)
101 this->dc_power_->publish_state(__builtin_bswap16(msg.dc_power) / 10.0f);
102 if (this->limiter_power_ !=
nullptr)
103 this->limiter_power_->publish_state(static_cast<int32_t>(__builtin_bswap32(msg.limiter_power)) / 10.0f);
104 if (this->temperature_ !=
nullptr)
105 this->temperature_->publish_state(calculate_temperature_(__builtin_bswap16(msg.temperature)));
107 #ifdef USE_TEXT_SENSOR 108 if (this->state_ !=
nullptr) {
109 this->state_->publish_state(this->state_to_string_(msg.state));
111 if (this->serial_number_ !=
nullptr) {
113 serial_number.assign(msg.serial_number, 10);
114 this->serial_number_->publish_state(serial_number);
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_);
128 #ifdef USE_TEXT_SENSOR 129 LOG_TEXT_SENSOR(
"",
"State", this->state_);
130 LOG_TEXT_SENSOR(
"",
"Serial Number", this->serial_number_);
esphome::sun_gtil2::SunGTIL2 __attribute__
void dump_config() override
std::string state_to_string_(uint8_t state)
void handle_char_(uint8_t c)
std::string str_sprintf(const char *fmt,...)
float calculate_temperature_(uint16_t adc_value)
Implementation of SPI Controller mode.