Skip to content

Commit

Permalink
Improve Liontron support (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi authored Jul 28, 2024
1 parent 0b47085 commit 6aef9e7
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 17 deletions.
5 changes: 2 additions & 3 deletions components/jbd_bms/jbd_bms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static const char *const ERRORS[ERRORS_SIZE] = {
"Unknown (0x0F)", // 0x0F
};

void JbdBms::setup() { this->send_command(JBD_CMD_READ, JBD_CMD_HWVER); }
void JbdBms::setup() { this->send_command(JBD_CMD_READ, JBD_CMD_HWINFO); }

void JbdBms::loop() {
const uint32_t now = millis();
Expand Down Expand Up @@ -157,6 +157,7 @@ void JbdBms::on_jbd_bms_data(const uint8_t &function, const std::vector<uint8_t>
switch (function) {
case JBD_CMD_HWINFO:
this->on_hardware_info_data_(data);
this->send_command(JBD_CMD_READ, JBD_CMD_CELLINFO);
break;
case JBD_CMD_CELLINFO:
this->on_cell_info_data_(data);
Expand Down Expand Up @@ -299,8 +300,6 @@ void JbdBms::on_hardware_info_data_(const std::vector<uint8_t> &data) {
this->publish_state_(this->temperatures_[i].temperature_sensor_,
(float) (jbd_get_16bit(23 + (i * 2)) - 2731) * 0.1f);
}

this->send_command(JBD_CMD_READ, JBD_CMD_CELLINFO);
}

void JbdBms::on_hardware_version_data_(const std::vector<uint8_t> &data) {
Expand Down
16 changes: 8 additions & 8 deletions components/jbd_bms_ble/jbd_bms_ble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,16 @@ void JbdBmsBle::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t ga
case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
this->node_state = espbt::ClientState::ESTABLISHED;

ESP_LOGI(TAG, "Request device info");
this->send_command(JBD_CMD_READ, JBD_CMD_HWVER);
this->send_command(JBD_CMD_READ, JBD_CMD_HWINFO);

break;
}
case ESP_GATTC_NOTIFY_EVT: {
if (param->notify.handle != this->char_notify_handle_)
break;

ESP_LOGVV(TAG, "Notification received: %s",
format_hex_pretty(param->notify.value, param->notify.value_len).c_str());
ESP_LOGV(TAG, "Notification received (handle 0x%02X): %s", param->notify.handle,
format_hex_pretty(param->notify.value, param->notify.value_len).c_str());

this->assemble_(param->notify.value, param->notify.value_len);

Expand Down Expand Up @@ -167,6 +166,7 @@ void JbdBmsBle::on_jbd_bms_data(const uint8_t &function, const std::vector<uint8
switch (function) {
case JBD_CMD_HWINFO:
this->on_hardware_info_data_(data);
this->send_command(JBD_CMD_READ, JBD_CMD_CELLINFO);
break;
case JBD_CMD_CELLINFO:
this->on_cell_info_data_(data);
Expand Down Expand Up @@ -309,8 +309,6 @@ void JbdBmsBle::on_hardware_info_data_(const std::vector<uint8_t> &data) {
this->publish_state_(this->temperatures_[i].temperature_sensor_,
(float) (jbd_get_16bit(23 + (i * 2)) - 2731) * 0.1f);
}

this->send_command(JBD_CMD_READ, JBD_CMD_CELLINFO);
}

void JbdBmsBle::on_hardware_version_data_(const std::vector<uint8_t> &data) {
Expand Down Expand Up @@ -508,7 +506,8 @@ bool JbdBmsBle::write_register(uint8_t address, uint16_t value) {
frame[7] = crc >> 0;
frame[8] = JBD_PKT_END;

ESP_LOGVV(TAG, "Send command: %s", format_hex_pretty(frame, sizeof(frame)).c_str());
ESP_LOGV(TAG, "Send command (handle 0x%02X): %s", this->char_command_handle_,
format_hex_pretty(frame, sizeof(frame)).c_str());
auto status =
esp_ble_gattc_write_char(this->parent_->get_gattc_if(), this->parent_->get_conn_id(), this->char_command_handle_,
sizeof(frame), frame, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
Expand All @@ -533,7 +532,8 @@ bool JbdBmsBle::send_command(uint8_t action, uint8_t function) {
frame[5] = crc >> 0;
frame[6] = JBD_PKT_END;

ESP_LOGVV(TAG, "Send command: %s", format_hex_pretty(frame, sizeof(frame)).c_str());
ESP_LOGV(TAG, "Send command (handle 0x%02X): %s", this->char_command_handle_,
format_hex_pretty(frame, sizeof(frame)).c_str());
auto status =
esp_ble_gattc_write_char(this->parent_->get_gattc_if(), this->parent_->get_conn_id(), this->char_command_handle_,
sizeof(frame), frame, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
Expand Down
2 changes: 1 addition & 1 deletion esp32-ble-example-multiple-devices.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ esphome:
min_version: 2024.6.0
project:
name: "syssi.esphome-jbd-bms"
version: 1.6.0
version: 2.0.0

esp32:
board: wemos_d1_mini32
Expand Down
3 changes: 2 additions & 1 deletion esp32-ble-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ esphome:
min_version: 2024.6.0
project:
name: "syssi.esphome-jbd-bms"
version: 1.6.0
version: 2.0.0

esp32:
board: wemos_d1_mini32
Expand Down Expand Up @@ -62,6 +62,7 @@ ble_client:
jbd_bms_ble:
- id: bms0
ble_client_id: client0
# Some Liontron BMS models require an update interval of less than 8s
update_interval: 2s

button:
Expand Down
2 changes: 1 addition & 1 deletion esp32-ble-scanner.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ esphome:
min_version: 2024.6.0
project:
name: "syssi.esphome-jbd-bms"
version: 1.6.0
version: 2.0.0

esp32:
board: wemos_d1_mini32
Expand Down
2 changes: 1 addition & 1 deletion esp32-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ esphome:
min_version: 2024.6.0
project:
name: "syssi.esphome-jbd-bms"
version: 1.6.0
version: 2.0.0

esp32:
board: wemos_d1_mini32
Expand Down
2 changes: 1 addition & 1 deletion esp8266-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ esphome:
min_version: 2024.6.0
project:
name: "syssi.esphome-jbd-bms"
version: 1.6.0
version: 2.0.0

esp8266:
board: d1_mini
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ esphome:
min_version: 2024.6.0
project:
name: "syssi.esphome-jbd-bms"
version: 1.6.0
version: 2.0.0

esp32:
board: wemos_d1_mini32
Expand Down

0 comments on commit 6aef9e7

Please sign in to comment.