syssi / esphome-jk-bms

ESPHome component to monitor and control a Jikong Battery Management System (JK-BMS) via UART-TTL or BLE
Apache License 2.0
450 stars 151 forks source link

JK BMS B2A20S20P Heat CAN/RS485 problem with connection #309

Closed Tesla72PL closed 1 year ago

Tesla72PL commented 1 year ago

Hello, I uploaded the following code for ESP8266 D1 mini and unfortunately nothing works - what am I doing wrong?

substitutions:
  name: jk-bms
  device_description: "Monitor a JK-BMS via UART-TTL"
  external_components_source: github://syssi/esphome-jk-bms@main
  tx_pin: GPIO1
  rx_pin: GPIO3

esphome:
  name: ${name}
  comment: ${device_description}
  project:
    name: "syssi.esphome-jk-bms"
    version: 1.4.0

esp8266:
  board: d1_mini

external_components:
  - source: ${external_components_source}
    refresh: 0s

api:
  encryption:
    key: "..."

ota:
  password: "..."

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Jk-Battery-01-Info"
    password: "..."

logger:
  baud_rate: 0
  level: DEBUG

# If you use Home Assistant please remove this `mqtt` section and uncomment the `api` component!
# The native API has many advantages over MQTT: https://esphome.io/components/api.html#advantages-over-mqtt

uart:
  - id: uart0
    baud_rate: 115200
    rx_buffer_size: 384
    tx_pin: ${tx_pin}
    rx_pin: ${rx_pin}
    debug:
     direction: BOTH

jk_modbus:
  - id: modbus0
    uart_id: uart0
    rx_timeout: 50ms

jk_bms:
  - id: bms0
    jk_modbus_id: modbus0
    update_interval: 5s

binary_sensor:
  - platform: jk_bms
    balancing:
      name: "${name} balancing"
    balancing_switch:
      name: "${name} balancing switch"
    charging:
      name: "${name} charging"
    charging_switch:
      name: "${name} charging switch"
    discharging:
      name: "${name} discharging"
    discharging_switch:
      name: "${name} discharging switch"
    dedicated_charger_switch:
      name: "${name} dedicated charger switch"
    online_status:
      name: "${name} online status"

sensor:
  - platform: jk_bms
    min_cell_voltage:
      name: "${name} min cell voltage"
    max_cell_voltage:
      name: "${name} max cell voltage"
    min_voltage_cell:
      name: "${name} min voltage cell"
    max_voltage_cell:
      name: "${name} max voltage cell"
    delta_cell_voltage:
      name: "${name} delta cell voltage"
    average_cell_voltage:
      name: "${name} average cell voltage"
    cell_voltage_1:
      name: "${name} cell voltage 1"
    cell_voltage_2:
      name: "${name} cell voltage 2"
    cell_voltage_3:
      name: "${name} cell voltage 3"
    cell_voltage_4:
      name: "${name} cell voltage 4"
    cell_voltage_5:
      name: "${name} cell voltage 5"
    cell_voltage_6:
      name: "${name} cell voltage 6"
    cell_voltage_7:
      name: "${name} cell voltage 7"
    cell_voltage_8:
      name: "${name} cell voltage 8"
    cell_voltage_9:
      name: "${name} cell voltage 9"
    cell_voltage_10:
      name: "${name} cell voltage 10"
    cell_voltage_11:
      name: "${name} cell voltage 11"
    cell_voltage_12:
      name: "${name} cell voltage 12"
    cell_voltage_13:
      name: "${name} cell voltage 13"
    cell_voltage_14:
      name: "${name} cell voltage 14"
    cell_voltage_15:
      name: "${name} cell voltage 15"
    cell_voltage_16:
      name: "${name} cell voltage 16"
    cell_voltage_17:
      name: "${name} cell voltage 17"
    cell_voltage_18:
      name: "${name} cell voltage 18"
    cell_voltage_19:
      name: "${name} cell voltage 19"
    cell_voltage_20:
      name: "${name} cell voltage 20"
    cell_voltage_21:
      name: "${name} cell voltage 21"
    cell_voltage_22:
      name: "${name} cell voltage 22"
    cell_voltage_23:
      name: "${name} cell voltage 23"
    cell_voltage_24:
      name: "${name} cell voltage 24"
    power_tube_temperature:
      name: "${name} power tube temperature"
    temperature_sensor_1:
      name: "${name} temperature sensor 1"
    temperature_sensor_2:
      name: "${name} temperature sensor 2"
    total_voltage:
      name: "${name} total voltage"
    current:
      name: "${name} current"
    power:
      name: "${name} power"
    charging_power:
      name: "${name} charging power"
    discharging_power:
      name: "${name} discharging power"
    capacity_remaining:
      name: "${name} capacity remaining"
    capacity_remaining_derived:
      name: "${name} capacity remaining derived"
    temperature_sensors:
      name: "${name} temperature sensors"
    charging_cycles:
      name: "${name} charging cycles"
    total_charging_cycle_capacity:
      name: "${name} total charging cycle capacity"
    battery_strings:
      name: "${name} battery strings"
    errors_bitmask:
      name: "${name} errors bitmask"
    operation_mode_bitmask:
      name: "${name} operation mode bitmask"
    total_voltage_overvoltage_protection:
      name: "${name} total voltage overvoltage protection"
    total_voltage_undervoltage_protection:
      name: "${name} total voltage undervoltage protection"
    cell_voltage_overvoltage_protection:
      name: "${name} cell voltage overvoltage protection"
    cell_voltage_overvoltage_recovery:
      name: "${name} cell voltage overvoltage recovery"
    cell_voltage_overvoltage_delay:
      name: "${name} cell voltage overvoltage delay"
    cell_voltage_undervoltage_protection:
      name: "${name} cell voltage undervoltage protection"
    cell_voltage_undervoltage_recovery:
      name: "${name} cell voltage undervoltage recovery"
    cell_voltage_undervoltage_delay:
      name: "${name} cell voltage undervoltage delay"
    cell_pressure_difference_protection:
      name: "${name} cell pressure difference protection"
    discharging_overcurrent_protection:
      name: "${name} discharging overcurrent protection"
    discharging_overcurrent_delay:
      name: "${name} discharging overcurrent delay"
    charging_overcurrent_protection:
      name: "${name} charging overcurrent protection"
    charging_overcurrent_delay:
      name: "${name} charging overcurrent delay"
    balance_starting_voltage:
      name: "${name} balance starting voltage"
    balance_opening_pressure_difference:
      name: "${name} balance opening pressure difference"
    power_tube_temperature_protection:
      name: "${name} power tube temperature protection"
    power_tube_temperature_recovery:
      name: "${name} power tube temperature recovery"
    temperature_sensor_temperature_protection:
      name: "${name} temperature sensor temperature protection"
    temperature_sensor_temperature_recovery:
      name: "${name} temperature sensor temperature recovery"
    temperature_sensor_temperature_difference_protection:
      name: "${name} temperature sensor temperature difference protection"
    charging_high_temperature_protection:
      name: "${name} charging high temperature protection"
    discharging_high_temperature_protection:
      name: "${name} discharging high temperature protection"
    charging_low_temperature_protection:
      name: "${name} charging low temperature protection"
    charging_low_temperature_recovery:
      name: "${name} charging low temperature recovery"
    discharging_low_temperature_protection:
      name: "${name} discharging low temperature protection"
    discharging_low_temperature_recovery:
      name: "${name} discharging low temperature recovery"
    total_battery_capacity_setting:
      name: "${name} total battery capacity setting"
    current_calibration:
      name: "${name} current calibration"
    device_address:
      name: "${name} device address"
    sleep_wait_time:
      name: "${name} sleep wait time"
    alarm_low_volume:
      name: "${name} alarm low volume"
    manufacturing_date:
      name: "${name} manufacturing date"
    total_runtime:
      name: "${name} total runtime"
#    start_current_calibration:
#      name: "${name} start current calibration"
    actual_battery_capacity:
      name: "${name} actual battery capacity"
#    protocol_version:
#      name: "${name} protocol version"

text_sensor:
  - platform: jk_bms
    errors:
      name: "${name} errors"
    operation_mode:
      name: "${name} operation mode"
    battery_type:
      name: "${name} battery type"
    password:
      name: "${name} password"
    device_type:
      name: "${name} device type"
    software_version:
      name: "${name} software version"
    manufacturer:
      name: "${name} manufacturer"
    total_runtime_formatted:
      name: "${name} total runtime formatted"
vjlb commented 1 year ago

Here you go @syssi , mine not working at all too:

BMS model - JK_B2A24S15P Software and hardware version of the BMS - Hard: V11.XW Soft: V11.25 Where did you buy the BMS? - Hankzor Did you buy the model with or without CAN support? - With CAN How do you power your ESP? - USB-C Which GPIOs do you use? 1/3

Tried also with MAX232 UART-TTL Port is working good for sure, used it before with other software monitor via usb to serial converter

Code used: espjk.txt

Log(modbus errors on the end) logs_jk-bms_logs.txt

1686507543872 IMG_20230611_191635

syssi commented 1 year ago

Tried also with MAX232 UART-TTL

What does this mean? Did you attach the MAX232 to the RS232 port of your computer? I hope you haven't pushed -12/+12V into the UART-TTL port.

Port is working good for sure, used it before with other software monitor via usb to serial converter

@vjlb Could you verify the other software monitor via the serial converter is still working? It's super interesting one software works and the other / this project isn't working.

syssi commented 1 year ago

@vjlb Could you change your YAML to this feature branch + flash the ESP and provide another log? I would like to know some more details about the invalid header warnings:

substitutions:
  external_components_source: github://syssi/esphome-jk-bms@debug-decoder
vjlb commented 1 year ago

Tried also with MAX232 UART-TTL

What does this mean? Did you attach the MAX232 to the RS232 port of your computer? I hope you haven't pushed -12/+12V into the UART-TTL port.

Not to PC mate, connected like this: Sem título

I use same connection on this software to monitor my inverter over MQTT: https://github.com/softwarecrash/Solar2MQTT

Port is working good for sure, used it before with other software monitor via usb to serial converter

@vjlb Could you verify the other software monitor via the serial converter is still working? It's super interesting one software works and the other / this project isn't working.

That's the one i used before to monitor BMS over USB to Serial, but i prefer the ESP solution over the Raspberry PI: https://github.com/dj-nitehawk/Hybrid-Inverter-Monitor

Will test the branch you suggested

vjlb commented 1 year ago

@vjlb Could you change your YAML to this feature branch + flash the ESP and provide another log? I would like to know some more details about the invalid header warnings:

substitutions:
  external_components_source: github://syssi/esphome-jk-bms@debug-decoder

Is this my bad? Trying it wirelessly

INFO ESPHome 2023.5.5 INFO Reading configuration /config/esphome/jk-bms.yaml... INFO Updating https://github.com/syssi/esphome-jk-bms.git@debug-decoder INFO Generating C++ source... INFO Compiling app... Processing jk-bms (board: d1_mini; framework: arduino; platform: platformio/espressif8266@3.2.0)

HARDWARE: ESP8266 80MHz, 80KB RAM, 4MB Flash Dependency Graph |-- ESP8266WiFi @ 1.0 |-- ESP8266mDNS @ 1.2 Compiling /data/jk-bms/.pioenvs/jk-bms/src/esphome/components/jk_modbus/jk_modbus.cpp.o Compiling /data/jk-bms/.pioenvs/jk-bms/src/main.cpp.o In file included from src/esphome/components/uart/uart.h:6, from src/esphome/components/jk_modbus/jk_modbus.h:4, from src/esphome/components/jk_modbus/jk_modbus.cpp:1: src/esphome/components/jk_modbus/jk_modbus.cpp: In member function 'virtual void esphome::jk_modbus::JkModbus::loop()': src/esphome/components/jk_modbus/jk_modbus.cpp:13:14: error: 'format_hex_pretty' was not declared in this scope 13 | format_hex_pretty(&this->rxbuffer.front(), this->rxbuffer.size()).c_str()); | ^~~~~ src/esphome/core/log.h:114:88: note: in definition of macro 'esph_log_w' 114 | esp_logprintf(ESPHOME_LOG_LEVEL_WARN, tag, LINE, ESPHOME_LOG_FORMAT(format), ##VA_ARGS__) | ^~~ src/esphome/components/jk_modbus/jk_modbus.cpp:12:5: note: in expansion of macro 'ESP_LOGW' 12 | ESP_LOGW(TAG, "Buffer cleared due to timeout: %s", | ^~~~ src/esphome/components/jk_modbus/jk_modbus.cpp:25:16: error: 'format_hex_pretty' was not declared in this scope 25 | format_hex_pretty(&this->rxbuffer.front(), this->rxbuffer.size()).c_str()); | ^~~~~ src/esphome/core/log.h:114:88: note: in definition of macro 'esph_log_w' 114 | esp_logprintf(ESPHOME_LOG_LEVEL_WARN, tag, LINE, ESPHOME_LOG_FORMAT(format), ##VA_ARGS__) | ^~~ src/esphome/components/jk_modbus/jk_modbus.cpp:24:7: note: in expansion of macro 'ESP_LOGW' 24 | ESP_LOGW(TAG, "Buffer cleared due to reset: %s", | ^~~~ *** [/data/jk-bms/.pioenvs/jk-bms/src/esphome/components/jk_modbus/jk_modbus.cpp.o] Error 1 ========================== [FAILED] Took 2.56 seconds ==========================

syssi commented 1 year ago

I've pushed a probably missing include. Please try again!

syssi commented 1 year ago

Please don't use a MAX232 with a JK-BMS! The high logic level could destroy the opto-couplers / UART-TTL port of the BMS.

vjlb commented 1 year ago

Please don't use a MAX232 with a JK-BMS! The high logic level could destroy the opto-couplers / UART-TTL port of the BMS.

Noted! Will test with other software to make sure it works

vjlb commented 1 year ago

Just finished install on RPI, it's working with other monitor software over the same port: image

Here is the log, very fast outputing those errors BTW: "Buffer cleared timeout"

logs_jk-bms_logs (1).txt

vjlb commented 1 year ago

Looks like sometimes i now get some tiny data, but i lowered the cable lenght

logs_jk-bms_logs (2).txt

Dimu1234 commented 10 months ago

This is my code & connect to HA through ESP32 via Bluetooth

`substitutions: name: jk-bms device_description: "Monitor and control a JK-BMS v11 via bluetooth" external_components_source: github://syssi/esphome-jk-bms@main mac_address: C8:47:8C:ED:1A:2F

Defaults to "JK02" (hardware version >= 6.0 and < 11.0)

Please use "JK02_32S" if you own a new JK-BMS >= hardware version 11.0 (f.e. JK-B2A8S20P hw 11.XW, sw 11.26)

Please use "JK04" if you have some old JK-BMS <= hardware version 3.0 (f.e. JK-B2A16S hw 3.0, sw. 3.3.0)

protocol_version: JK02_32S

esphome: name: ${name} comment: ${device_description} project: name: "syssi.esphome-jk-bms" version: 1.5.0

esp32: board: wemos_d1_mini32 framework: type: esp-idf

external_components:

wifi: ssid: !secret wifi_ssid password: !secret wifi_password

ota:

logger: level: DEBUG

If you don't use Home Assistant please remove this api section and uncomment the mqtt component!

api: encryption: key: sjN3JULMjM2T90efzTxKXp56Gt/DVqbGoagTtTBHHIo=

mqtt:

broker: !secret mqtt_host

username: !secret mqtt_username

password: !secret mqtt_password

id: mqtt_client

esp32_ble_tracker: on_ble_advertise: then:

ble_client:

jk_bms_ble:

binary_sensor:

button:

number:

sensor:

switch:

text_sensor:

Untitled

jeremGi commented 8 months ago

merci je galère depuis 3 jours et j'étais dans le même cas et en Bluetooth ca fonctionne

chaosnature commented 7 months ago

@JakobTewes I cannot provide a good answer. I'm a software developer with limited skills in electrical circuits. I'm just aware of users who release magic smoke (destroyed the serial port of the BMS, ESP or buck converter).

Can you assist me i have the same issue how does one upgrade the firmware?

silviu61 commented 4 months ago

Hello, I uploaded the following code for ESP8266 D1 mini and unfortunately nothing works - what am I doing wrong?

substitutions:
  name: jk-bms
  device_description: "Monitor a JK-BMS via UART-TTL"
  external_components_source: github://syssi/esphome-jk-bms@main
  tx_pin: GPIO1
  rx_pin: GPIO3

esphome:
  name: ${name}
  comment: ${device_description}
  project:
    name: "syssi.esphome-jk-bms"
    version: 1.4.0

esp8266:
  board: d1_mini

external_components:
  - source: ${external_components_source}
    refresh: 0s

api:
  encryption:
    key: "..."

ota:
  password: "..."

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Jk-Battery-01-Info"
    password: "..."

logger:
  baud_rate: 0
  level: DEBUG

# If you use Home Assistant please remove this `mqtt` section and uncomment the `api` component!
# The native API has many advantages over MQTT: https://esphome.io/components/api.html#advantages-over-mqtt

uart:
  - id: uart0
    baud_rate: 115200
    rx_buffer_size: 384
    tx_pin: ${tx_pin}
    rx_pin: ${rx_pin}
    debug:
     direction: BOTH

jk_modbus:
  - id: modbus0
    uart_id: uart0
    rx_timeout: 50ms

jk_bms:
  - id: bms0
    jk_modbus_id: modbus0
    update_interval: 5s

binary_sensor:
  - platform: jk_bms
    balancing:
      name: "${name} balancing"
    balancing_switch:
      name: "${name} balancing switch"
    charging:
      name: "${name} charging"
    charging_switch:
      name: "${name} charging switch"
    discharging:
      name: "${name} discharging"
    discharging_switch:
      name: "${name} discharging switch"
    dedicated_charger_switch:
      name: "${name} dedicated charger switch"
    online_status:
      name: "${name} online status"

sensor:
  - platform: jk_bms
    min_cell_voltage:
      name: "${name} min cell voltage"
    max_cell_voltage:
      name: "${name} max cell voltage"
    min_voltage_cell:
      name: "${name} min voltage cell"
    max_voltage_cell:
      name: "${name} max voltage cell"
    delta_cell_voltage:
      name: "${name} delta cell voltage"
    average_cell_voltage:
      name: "${name} average cell voltage"
    cell_voltage_1:
      name: "${name} cell voltage 1"
    cell_voltage_2:
      name: "${name} cell voltage 2"
    cell_voltage_3:
      name: "${name} cell voltage 3"
    cell_voltage_4:
      name: "${name} cell voltage 4"
    cell_voltage_5:
      name: "${name} cell voltage 5"
    cell_voltage_6:
      name: "${name} cell voltage 6"
    cell_voltage_7:
      name: "${name} cell voltage 7"
    cell_voltage_8:
      name: "${name} cell voltage 8"
    cell_voltage_9:
      name: "${name} cell voltage 9"
    cell_voltage_10:
      name: "${name} cell voltage 10"
    cell_voltage_11:
      name: "${name} cell voltage 11"
    cell_voltage_12:
      name: "${name} cell voltage 12"
    cell_voltage_13:
      name: "${name} cell voltage 13"
    cell_voltage_14:
      name: "${name} cell voltage 14"
    cell_voltage_15:
      name: "${name} cell voltage 15"
    cell_voltage_16:
      name: "${name} cell voltage 16"
    cell_voltage_17:
      name: "${name} cell voltage 17"
    cell_voltage_18:
      name: "${name} cell voltage 18"
    cell_voltage_19:
      name: "${name} cell voltage 19"
    cell_voltage_20:
      name: "${name} cell voltage 20"
    cell_voltage_21:
      name: "${name} cell voltage 21"
    cell_voltage_22:
      name: "${name} cell voltage 22"
    cell_voltage_23:
      name: "${name} cell voltage 23"
    cell_voltage_24:
      name: "${name} cell voltage 24"
    power_tube_temperature:
      name: "${name} power tube temperature"
    temperature_sensor_1:
      name: "${name} temperature sensor 1"
    temperature_sensor_2:
      name: "${name} temperature sensor 2"
    total_voltage:
      name: "${name} total voltage"
    current:
      name: "${name} current"
    power:
      name: "${name} power"
    charging_power:
      name: "${name} charging power"
    discharging_power:
      name: "${name} discharging power"
    capacity_remaining:
      name: "${name} capacity remaining"
    capacity_remaining_derived:
      name: "${name} capacity remaining derived"
    temperature_sensors:
      name: "${name} temperature sensors"
    charging_cycles:
      name: "${name} charging cycles"
    total_charging_cycle_capacity:
      name: "${name} total charging cycle capacity"
    battery_strings:
      name: "${name} battery strings"
    errors_bitmask:
      name: "${name} errors bitmask"
    operation_mode_bitmask:
      name: "${name} operation mode bitmask"
    total_voltage_overvoltage_protection:
      name: "${name} total voltage overvoltage protection"
    total_voltage_undervoltage_protection:
      name: "${name} total voltage undervoltage protection"
    cell_voltage_overvoltage_protection:
      name: "${name} cell voltage overvoltage protection"
    cell_voltage_overvoltage_recovery:
      name: "${name} cell voltage overvoltage recovery"
    cell_voltage_overvoltage_delay:
      name: "${name} cell voltage overvoltage delay"
    cell_voltage_undervoltage_protection:
      name: "${name} cell voltage undervoltage protection"
    cell_voltage_undervoltage_recovery:
      name: "${name} cell voltage undervoltage recovery"
    cell_voltage_undervoltage_delay:
      name: "${name} cell voltage undervoltage delay"
    cell_pressure_difference_protection:
      name: "${name} cell pressure difference protection"
    discharging_overcurrent_protection:
      name: "${name} discharging overcurrent protection"
    discharging_overcurrent_delay:
      name: "${name} discharging overcurrent delay"
    charging_overcurrent_protection:
      name: "${name} charging overcurrent protection"
    charging_overcurrent_delay:
      name: "${name} charging overcurrent delay"
    balance_starting_voltage:
      name: "${name} balance starting voltage"
    balance_opening_pressure_difference:
      name: "${name} balance opening pressure difference"
    power_tube_temperature_protection:
      name: "${name} power tube temperature protection"
    power_tube_temperature_recovery:
      name: "${name} power tube temperature recovery"
    temperature_sensor_temperature_protection:
      name: "${name} temperature sensor temperature protection"
    temperature_sensor_temperature_recovery:
      name: "${name} temperature sensor temperature recovery"
    temperature_sensor_temperature_difference_protection:
      name: "${name} temperature sensor temperature difference protection"
    charging_high_temperature_protection:
      name: "${name} charging high temperature protection"
    discharging_high_temperature_protection:
      name: "${name} discharging high temperature protection"
    charging_low_temperature_protection:
      name: "${name} charging low temperature protection"
    charging_low_temperature_recovery:
      name: "${name} charging low temperature recovery"
    discharging_low_temperature_protection:
      name: "${name} discharging low temperature protection"
    discharging_low_temperature_recovery:
      name: "${name} discharging low temperature recovery"
    total_battery_capacity_setting:
      name: "${name} total battery capacity setting"
    current_calibration:
      name: "${name} current calibration"
    device_address:
      name: "${name} device address"
    sleep_wait_time:
      name: "${name} sleep wait time"
    alarm_low_volume:
      name: "${name} alarm low volume"
    manufacturing_date:
      name: "${name} manufacturing date"
    total_runtime:
      name: "${name} total runtime"
#    start_current_calibration:
#      name: "${name} start current calibration"
    actual_battery_capacity:
      name: "${name} actual battery capacity"
#    protocol_version:
#      name: "${name} protocol version"

text_sensor:
  - platform: jk_bms
    errors:
      name: "${name} errors"
    operation_mode:
      name: "${name} operation mode"
    battery_type:
      name: "${name} battery type"
    password:
      name: "${name} password"
    device_type:
      name: "${name} device type"
    software_version:
      name: "${name} software version"
    manufacturer:
      name: "${name} manufacturer"
    total_runtime_formatted:
      name: "${name} total runtime formatted"

Hi from romania. code for esp32 wemos that is working you can give to me? Thanks.