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;
14struct SunGTIL2Message {
16 uint8_t ac_waveform[277];
30 char serial_number[10];
35static const uint16_t MESSAGE_SIZE =
sizeof(SunGTIL2Message);
37static_assert(MESSAGE_SIZE == 350,
"Expected the message size to be 350 bytes");
41void SunGTIL2::loop() {
48 return "Starting voltage too low";
58 if (adc_value >= ADC_MAX || adc_value == 0) {
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);
79 memcpy(&msg, this->
rx_message_.data(), MESSAGE_SIZE);
82 if ((msg.end[0] != 0) || (msg.end[38] != 0x08))
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,
90 PUBLISH_STATE(this->ac_voltage_sensor_, __builtin_bswap16(msg.ac_voltage) / 10.0f);
91 PUBLISH_STATE(this->dc_voltage_sensor_, __builtin_bswap16(msg.dc_voltage) / 8.0f);
92 PUBLISH_STATE(this->ac_power_sensor_, __builtin_bswap16(msg.ac_power) / 10.0f);
93 PUBLISH_STATE(this->dc_power_sensor_, __builtin_bswap16(msg.dc_power) / 10.0f);
94 PUBLISH_STATE(this->limiter_power_sensor_,
static_cast<int32_t
>(__builtin_bswap32(msg.limiter_power)) / 10.0f);
98 if (this->state_text_sensor_ !=
nullptr) {
99 char state_buffer[STATE_BUFFER_SIZE];
100 this->state_text_sensor_->publish_state(this->
state_to_string_(msg.state, state_buffer));
102 PUBLISH_STATE(this->serial_number_text_sensor_, msg.serial_number, 10);
106void SunGTIL2::dump_config() {
108 LOG_SENSOR(
"",
"AC Voltage", this->ac_voltage_sensor_);
109 LOG_SENSOR(
"",
"DC Voltage", this->dc_voltage_sensor_);
110 LOG_SENSOR(
"",
"AC Power", this->ac_power_sensor_);
111 LOG_SENSOR(
"",
"DC Power", this->dc_power_sensor_);
112 LOG_SENSOR(
"",
"Limiter Power", this->limiter_power_sensor_);
113 LOG_SENSOR(
"",
"Temperature", this->temperature_sensor_);
115#ifdef USE_TEXT_SENSOR
116 LOG_TEXT_SENSOR(
"",
"State", this->state_text_sensor_);
117 LOG_TEXT_SENSOR(
"",
"Serial Number", this->serial_number_text_sensor_);
virtual void setup()
Where the component's initialization should happen.
float calculate_temperature_(uint16_t adc_value)
void handle_char_(uint8_t c)
std::vector< uint8_t > rx_message_
const char * state_to_string_(uint8_t state, std::span< char, STATE_BUFFER_SIZE > buffer)
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
struct @66::@67 __attribute__
Wake the main loop task from an ISR. ISR-safe.
ESPHOME_LOG_TAG(TAG, "sun_gtil2")