Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UART-TTL command support (write registers) #343

Merged
merged 6 commits into from
Jun 24, 2023
Merged

Add UART-TTL command support (write registers) #343

merged 6 commits into from
Jun 24, 2023

Conversation

syssi
Copy link
Owner

@syssi syssi commented Jun 23, 2023

See #341

substitutions:
  external_components_source: github://syssi/esphome-jk-bms@add-controls

external_components:
  - source: ${external_components_source}
    refresh: 0s
  • Handle short response frames

@syssi syssi force-pushed the add-controls branch 3 times, most recently from 09c4fd8 to f90bb87 Compare June 23, 2023 06:10
@jrventer
Copy link

Hi

Did a quick build but looks like it is not working. Below the logs. Did you put 100ms delay between the 0x5 and the write request as from below it seems there is no delay?
[09:09:26][D][switch:016]: 'esphome-web-f3d9c8 charging' Turning OFF.
[09:09:26][D][switch:055]: 'esphome-web-f3d9c8 charging': Sending state OFF
[09:09:26][D][uart_debug:158]: >>> "NW\x00\x14\x00\x00\x00\x00\x05\x02\x00\xB2\x00\x00\x00\x00\x00h\x00\x00\x01rNW\x00\x14\x00\x00\x00\x00\x02\x02\x00\xAB\x00\x00\x00\x00\x00h\x00\x00\x01h"
[09:09:30][D][switch:012]: 'esphome-web-f3d9c8 charging' Turning ON.
[09:09:30][D][switch:055]: 'esphome-web-f3d9c8 charging': Sending state ON
[09:09:30][D][uart_debug:158]: >>> "NW\x00\x14\x00\x00\x00\x00\x05\x02\x00\xB2\x00\x00\x00\x00\x00h\x00\x00\x01rNW\x00\x14\x00\x00\x00\x00\x02\x02\x00\xAB\x01\x00\x00\x00\x00h\x00\x00\x01i"

@jrventer
Copy link

@syssi also update the crc frame length to 18 as per the main thread.
frame[11] = address; // register: 0x00 (read all registers), 0x8E...0xBF (holding registers)
frame[12] = value; // data
frame[13] = 0x00; // record number
frame[14] = 0x00; // record number
frame[15] = 0x00; // record number
frame[16] = 0x00; // record number
frame[17] = 0x68; // end sequence
auto crc = chksum(frame, 17); <<<<<<<<<< should be 18

@syssi
Copy link
Owner Author

syssi commented Jun 23, 2023

Fixed. Is the delay required? I would like to avoid delays as long as possible.

@jrventer
Copy link

Fixed. Is the delay required? I would like to avoid delays as long as possible.

From the documentation they state it is required between requests but lets test without it with the checksum fixed.

@jrventer
Copy link

Fixed. Is the delay required? I would like to avoid delays as long as possible.

Ok tested still not working likely would need some delay between the auth and write request.

@syssi
Copy link
Owner Author

syssi commented Jun 23, 2023

I've added a delay(150) between the commands.

@jrventer
Copy link

I've added a delay(150) between the commands.

Ok works now but see this in the logs "[jk_bms:056]: Invalid size (5) for JK BMS frame!" not sure why but will have to check. Also balancer switch not working looks like you not using the correct register.

Balancer Switch:
[10:58:54][D][uart_debug:158]: >>> "NW\x00\x14\x00\x00\x00\x00\x05\x02\x00\xB2\x00\x00\x00\x00\x00h\x00\x00\x01\xDANW\x00\x14\x00\x00\x00\x00\x02\x02\x00\x9D\x01\x00\x00\x00\x00h\x00\x00\x01\xC3"
[10:58:54][W][jk_bms:056]: Invalid size (5) for JK BMS frame!
[10:58:54][D][uart_debug:158]: <<< "NW\x00\x13\x00\x00\x00\x00\x02\x00\x01\x9D\x00\x00\x00\x00h\x00\x00\x01\xC0"
Seems like you are using 0x9D but from the document it should be 9A for Active equalisation switch.

Below logs for charge and discharge that is working:
[10:57:06][D][switch:012]: 'esphome-web-f3d9c8 charging' Turning ON.
[10:57:07][D][switch:055]: 'esphome-web-f3d9c8 charging': Sending state ON
[10:57:07][D][uart_debug:158]: >>> "NW\x00\x14\x00\x00\x00\x00\x05\x02\x00\xB2\x00\x00\x00\x00\x00h\x00\x00\x01\xDANW\x00\x14\x00\x00\x00\x00\x02\x02\x00\xAB\x01\x00\x00\x00\x00h\x00\x00\x01\xD1"
[10:57:07][W][jk_bms:056]: Invalid size (5) for JK BMS frame!
[10:57:07][D][uart_debug:158]: <<< "NW\x00\x13\x00\x00\x00\x00\x02\x00\x01\xAB\x00\x00\x00\x00h\x00\x00\x01\xCE"
[10:57:09][D][switch:016]: 'esphome-web-f3d9c8 charging' Turning OFF.
[10:57:09][D][switch:055]: 'esphome-web-f3d9c8 charging': Sending state OFF
[10:57:09][D][uart_debug:158]: >>> "NW\x00\x14\x00\x00\x00\x00\x05\x02\x00\xB2\x00\x00\x00\x00\x00h\x00\x00\x01\xDANW\x00\x14\x00\x00\x00\x00\x02\x02\x00\xAB\x00\x00\x00\x00\x00h\x00\x00\x01\xD0"
[10:57:09][W][jk_bms:056]: Invalid size (5) for JK BMS frame!
[10:57:09][D][uart_debug:158]: <<< "NW\x00\x13\x00\x00\x00\x00\x02\x00\x01\xAB\x00\x00\x00\x00h\x00\x00\x01\xCE"
[10:57:12][D][switch:012]: 'esphome-web-f3d9c8 discharging' Turning ON.
[10:57:12][D][switch:055]: 'esphome-web-f3d9c8 discharging': Sending state ON
[10:57:12][D][uart_debug:158]: >>> "NW\x00\x14\x00\x00\x00\x00\x05\x02\x00\xB2\x00\x00\x00\x00\x00h\x00\x00\x01\xDANW\x00\x14\x00\x00\x00\x00\x02\x02\x00\xAC\x01\x00\x00\x00\x00h\x00\x00\x01\xD2"
[10:57:12][W][jk_bms:056]: Invalid size (5) for JK BMS frame!
[10:57:12][D][uart_debug:158]: <<< "NW\x00\x13\x00\x00\x00\x00\x02\x00\x01\xAC\x00\x00\x00\x00h\x00\x00\x01\xCF"
[10:57:14][D][switch:016]: 'esphome-web-f3d9c8 discharging' Turning OFF.
[10:57:14][D][switch:055]: 'esphome-web-f3d9c8 discharging': Sending state OFF
[10:57:14][D][uart_debug:158]: >>> "NW\x00\x14\x00\x00\x00\x00\x05\x02\x00\xB2\x00\x00\x00\x00\x00h\x00\x00\x01\xDANW\x00\x14\x00\x00\x00\x00\x02\x02\x00\xAC\x00\x00\x00\x00\x00h\x00\x00\x01\xD1"
[10:57:14][W][jk_bms:056]: Invalid size (5) for JK BMS frame!

@jrventer
Copy link

Had a quick look at seems like in jk_bms.cpp the registers from after 0x90 will need to checked and aligned with the latest documents. Hopefully not a difference for older version of the BMS that would need to be catered for.

Using this document as a reference:
https://github.com/NEEY-electronic/JK/blob/JK-BMS/BMS-RS485Communication%20protocol.pdf

@syssi
Copy link
Owner Author

syssi commented Jun 23, 2023

Hmm... it looks like the registers has changed over time. Interesting.

@jrventer
Copy link

Ok also does not work but I have also now checked and looks like 0x9D was correct corresponding with example document:
https://github.com/NEEY-electronic/JK/blob/JK-BMS/Jikong-BMS-RS485-Communication%20example.pdf The protocol document is clearly older.

Difficult without the correct documentation. Will have to see why it did not want to write with 0x9D. Also confirmed the 0x9D changes when changing the switch via BLE app.

@syssi
Copy link
Owner Author

syssi commented Jun 23, 2023

Could you try to identify the minimum required delay?

@jrventer
Copy link

Ok I have done further testing but cannot get the balancer switch to change. I think you will have to just do the charge and discharge switches for now which I believe is a huge improvement already. I will try and see what I can get out of JK further. If we can just get the code for bluetooth module we should be able to get this working quickly as that seem to have the best knowledge of the protocol of the BMS.

For the authentication you can also change the 0xB2 to 0x00 as it seems to be ignored.

For the delay between the auth and the write it appears to work with 10ms even I guess it depends on what the BMS microcontroller is busy with. My testing for this was using a lambda to build the frame for uart.write function in esphome config so might be handled slightly differently to the delay you adding at the lower level code side. I guess the best way to test would be to for you build and the modify the value and rebuild and test.

@jrventer
Copy link

After some further testing I will keep delay at 100ms.

@syssi
Copy link
Owner Author

syssi commented Jun 24, 2023

@jrventer I've applied all suggestions! Thanks a lot for heavily testing. I've the chance today to test the code on a pretty old BMS firmware version. If it does work out of the box this is ready to get merged!

@syssi
Copy link
Owner Author

syssi commented Jun 24, 2023

[09:37:16][D][switch:055]: 'attic-bms-uart charging': Sending state ON
[09:37:16][D][uart_debug:114]: >>> 4E:57:00:14:00:00:00:00:05:02:00:00:00:00:00:00:00:68:00:00:01:28:4E:57:00:14:00:00:00:00:02:02:00:AB:01:00:00:00:00:68:00:00:01:D1
[09:37:17][W][jk_bms:056]: Invalid size (5) for JK BMS frame!
[09:37:17][D][uart_debug:114]: <<< 4E:57:00:13:00:00:00:00:02:00:01:AB:00:00:00:00:68:00:00:01:CE
[09:37:18][D][uart_debug:114]: >>> 4E:57:00:13:00:00:00:00:06:02:00:00:00:00:00:00:68:00:00:01:28

@syssi
Copy link
Owner Author

syssi commented Jun 24, 2023

My BMS (JK-BD6A17S6P, hw 7.2, sw 7.1.0H) needs a pretty high delay around 200ms to accept authenticated write instructions. But it's working!! :-)

@jrventer
Copy link

@syssi Not sure if you are aware but JK actually list your project url right at the top of the document they ship with the BMS. I only noticed it recently as I did not really look at it before. So they basically acknowledge that the community have a better understanding on their communication interfaces. I suspect they got a 3rd party company to develop the BMS firmware and communication components thats why they are not able to easily access information on this topic.
image

@syssi
Copy link
Owner Author

syssi commented Jun 24, 2023

We already celebrated this nice fact a year ago here: #132

@syssi syssi merged commit 6c4acee into main Jun 24, 2023
6 checks passed
@LichtiMC
Copy link

Any progress on the balancing switch? Would be awesome if it worked via uart.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants