13static constexpr uint16_t SERVICE_NOTIFY_UUID = 0xFFF0;
14static constexpr uint16_t CHAR_NOTIFY_UUID = 0xFFF1;
15static constexpr uint16_t SERVICE_WRITE_UUID = 0xFFD0;
16static constexpr uint16_t CHAR_WRITE_UUID = 0xFFD1;
17static constexpr uint16_t CHAR_INIT_UUID = 0xFFD4;
19static constexpr uint8_t MODBUS_DEVICE_ID = 0x20;
20static constexpr uint8_t MODBUS_READ_HOLDING = 0x03;
21static constexpr uint16_t REG_MAIN = 4000;
22static constexpr uint16_t REG_MAIN_WORDS = 32;
23static constexpr uint16_t REG_LOAD = 4408;
24static constexpr uint16_t REG_LOAD_WORDS = 6;
27static constexpr uint32_t CYCLE_TIMEOUT_MS = 3000;
29static constexpr uint32_t LOAD_READ_DELAY_MS = 300;
31static constexpr uint32_t CYCLE_TIMEOUT_ID = 0;
32static constexpr uint32_t LOAD_READ_ID = 1;
35 ESP_LOGCONFIG(TAG,
"Renogy Inverter BLE:");
36 LOG_UPDATE_INTERVAL(
this);
37 LOG_SENSOR(
" ",
"AC input voltage", this->ac_input_voltage_sensor_);
38 LOG_SENSOR(
" ",
"AC output voltage", this->ac_output_voltage_sensor_);
39 LOG_SENSOR(
" ",
"AC output current", this->ac_output_current_sensor_);
40 LOG_SENSOR(
" ",
"AC output frequency", this->ac_output_frequency_sensor_);
41 LOG_SENSOR(
" ",
"Input frequency", this->input_frequency_sensor_);
42 LOG_SENSOR(
" ",
"Battery voltage", this->battery_voltage_sensor_);
43 LOG_SENSOR(
" ",
"Temperature", this->temperature_sensor_);
44 LOG_SENSOR(
" ",
"Load current", this->load_current_sensor_);
45 LOG_SENSOR(
" ",
"Load active power", this->load_active_power_sensor_);
46 LOG_SENSOR(
" ",
"Load apparent power", this->load_apparent_power_sensor_);
57 std::array<uint8_t, 8> req{MODBUS_DEVICE_ID,
59 static_cast<uint8_t
>(start_register >> 8),
60 static_cast<uint8_t
>(start_register),
61 static_cast<uint8_t
>(word_count >> 8),
62 static_cast<uint8_t
>(word_count)};
63 const uint16_t crc =
crc16(req.data(), 6);
69 esp_ble_gattc_write_char(this->
parent_->
get_gattc_if(), this->parent_->get_conn_id(), this->write_handle_,
70 req.size(), req.data(), ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
72 ESP_LOGW(TAG,
"write_char (reg %u) failed, status=%d", start_register,
status);
78 this->
set_timeout(CYCLE_TIMEOUT_ID, CYCLE_TIMEOUT_MS, [
this]() {
79 ESP_LOGW(TAG,
"Read cycle timed out (state=%u); resetting",
static_cast<uint8_t
>(this->
state_));
90 this->init_handle_, ESP_GATT_AUTH_REQ_NONE);
92 ESP_LOGW(TAG,
"init read_char failed, status=%d",
status);
97void RenogyInverterBle::update() {
98 if (this->
node_state != espbt::ClientState::ESTABLISHED) {
99 ESP_LOGD(TAG,
"Not connected yet; skipping poll");
103 ESP_LOGD(TAG,
"Previous read cycle still in progress; skipping");
112 ESP_LOGW(TAG,
"CRC mismatch (state=%u, len=%u)",
static_cast<uint8_t
>(this->
state_), len);
117 if ((this->
frame_[1] & 0x80) != 0) {
118 ESP_LOGW(TAG,
"Modbus exception 0x%02X (state=%u)", this->
frame_[2],
static_cast<uint8_t
>(this->
state_));
124 const uint16_t expected_words = this->
state_ ==
State::MAIN ? REG_MAIN_WORDS : REG_LOAD_WORDS;
125 if (!awaiting_reply || this->
frame_[0] != MODBUS_DEVICE_ID || this->
frame_[1] != MODBUS_READ_HOLDING ||
126 this->
frame_[2] != 2 * expected_words) {
127 ESP_LOGW(TAG,
"Unexpected response %02X %02X %02X (state=%u)", this->
frame_[0], this->
frame_[1], this->
frame_[2],
128 static_cast<uint8_t
>(this->
state_));
135 PUBLISH_STATE(this->ac_input_voltage_sensor_, reg16(0) * 0.1f);
136 PUBLISH_STATE(this->ac_output_voltage_sensor_, reg16(2) * 0.1f);
137 PUBLISH_STATE(this->ac_output_current_sensor_, reg16(3) * 0.01f);
138 PUBLISH_STATE(this->ac_output_frequency_sensor_, reg16(4) * 0.01f);
139 PUBLISH_STATE(this->battery_voltage_sensor_, reg16(5) * 0.1f);
140 PUBLISH_STATE(this->temperature_sensor_, reg16(6) * 0.1f);
141 PUBLISH_STATE(this->input_frequency_sensor_, reg16(9) * 0.01f);
143 this->
set_timeout(LOAD_READ_ID, LOAD_READ_DELAY_MS, [
this]() {
149 PUBLISH_STATE(this->load_current_sensor_, reg16(0) * 0.01f);
150 PUBLISH_STATE(this->load_active_power_sensor_,
static_cast<float>(reg16(1)));
151 PUBLISH_STATE(this->load_apparent_power_sensor_,
static_cast<float>(reg16(2)));
155void RenogyInverterBle::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
156 esp_ble_gattc_cb_param_t *param) {
158 case ESP_GATTC_DISCONNECT_EVT: {
163 case ESP_GATTC_SEARCH_CMPL_EVT: {
165 espbt::ESPBTUUID::from_uint16(CHAR_NOTIFY_UUID));
167 espbt::ESPBTUUID::from_uint16(CHAR_WRITE_UUID));
169 espbt::ESPBTUUID::from_uint16(CHAR_INIT_UUID));
170 if (notify_chr ==
nullptr || write_chr ==
nullptr) {
171 ESP_LOGE(TAG,
"Required characteristics not found; not a Renogy inverter?");
176 this->
init_handle_ = (init_chr !=
nullptr) ? init_chr->handle : 0;
180 if (status != ESP_OK) {
181 ESP_LOGW(TAG,
"register_for_notify failed, status=%d", status);
185 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
187 if (param->reg_for_notify.handle != this->notify_handle_) {
190 if (param->reg_for_notify.status != ESP_GATT_OK) {
191 ESP_LOGW(TAG,
"Notification registration failed, status=%d", param->reg_for_notify.status);
194 this->
node_state = espbt::ClientState::ESTABLISHED;
195 ESP_LOGI(TAG,
"Connected; will poll the inverter every update interval");
198 case ESP_GATTC_READ_CHAR_EVT: {
202 if (param->read.status != ESP_GATT_OK) {
203 ESP_LOGW(TAG,
"init read failed, status=%d", param->read.status);
212 case ESP_GATTC_NOTIFY_EVT: {
213 if (param->notify.handle != this->notify_handle_) {
216 const uint16_t value_len = param->notify.value_len;
217 if (this->
frame_len_ + value_len > MAX_FRAME) {
218 ESP_LOGW(TAG,
"Frame overflow; resetting");
229 const uint16_t expected_len = ((this->
frame_[1] & 0x80) != 0) ? 5 : 3 + this->
frame_[2] + 2;
uint16_t crc16(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t reverse_poly, bool refin, bool refout)
Calculate a CRC-16 checksum of data with size len.