Python lib to talk to pylontech lithium batteries (US2000, US3000, ...) using RS485

Overview

python-pylontech

Python lib to talk to pylontech lithium batteries (US2000, US3000, ...) using RS485

What is this lib ?

This lib is meant to talk to Pylontech batteries using RS485. Sadly the protocol over RS485 is not some fancy thing like MODBUS but their own crappy protocol.

How to use this lib ?

First of all, you need a USB to RS485 converter. They are many available online for some bucks.

Then, you simply need to import the lib and start asking values:

>>> import pylontech
>>> p = pylontech.Pylontech()
>>> print(p.get_values())
Container: 
    CommandValue = 1
    NumberOfCells = 15
    CellVoltages = ListContainer: 
        3.325
        3.326
        3.325
        3.325
        3.325
        3.325
        3.325
        3.324
        3.324
        3.324
        3.326
        3.326
        3.326
        3.326
        3.326
    NumberOfTemperatures = 5
    AverageBMSTemperature = 30.01
    GroupedCellsTemperatures = ListContainer: 
        29.61
        29.61
        29.61
        29.61
    Current = 0
    Voltage = 49.878
    RemainingCapacity = 49.0
    TotalCapacity = 50.0
    CycleNumber = 0
>>> print(p.get_system_parameters())
Container: 
    CellHighVoltageLimit = 3.7
    CellLowVoltageLimit = 3.05
    CellUnderVoltageLimit = 2.9
    ChargeHighTemperatureLimit = 33.41
    ChargeLowTemperatureLimit = 26.21
    ChargeCurrentLimit = 10.2
    ModuleHighVoltageLimit = 54.0
    ModuleLowVoltageLimit = 46.0
    ModuleUnderVoltageLimit = 44.5
    DischargeHighTemperatureLimit = 33.41
    DischargeLowTemperatureLimit = 26.21
    DischargeCurrentLimit = -10.0

Dependencies

This lib depends on pyserial and the awesome construct lib.

Comments
  • Question: How to use different serial port than ttyUSB0?

    Question: How to use different serial port than ttyUSB0?

    Can I tell pylontech.py to use e.g. ttyUSB11 instead of ttyUSB0? Everything is working fine except my USB devices (I have multiple) are differently enumerated...

    opened by df8oe 4
  • Add start of support/testing with UP2500.

    Add start of support/testing with UP2500.

    I thought it might be useful to open a pull request for discussion rather than spamming other people's issues :)

    I've added parsing which I think works for all of for US2000/US3000/UP2500 as well as a few simple tests to make sure the code behaves as expected to help future refactoring.

    I don't have a US2000/US3000 (or any multi battery configuration) to test with, so it someone could verify it works on real devices it would be helpful!

    Again, many thanks for this code! It's great that I managed to talk to my battery with relatively little effort thanks to this :)

    opened by abelsson 2
  • module 'pylontech' has no attribute 'Pylontech'

    module 'pylontech' has no attribute 'Pylontech'

    Hi, @Frankkkkk this is what I've got from the very beginning:

    root@raspberrypi:~# pip3 install python-pylontech Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple Collecting python-pylontech Using cached https://files.pythonhosted.org/packages/19/29/caedde3663ae093c2046ed70925b4f2cc673817f7255d7b2ae9e4dc92e63/python_pylontech-0.0.6-py3-none-any.whl Requirement already satisfied: pyserial in /usr/lib/python3/dist-packages (from python-pylontech) (3.4) Requirement already satisfied: construct in /usr/local/lib/python3.7/dist-packages (from python-pylontech) (2.10.67) Installing collected packages: python-pylontech Successfully installed python-pylontech-0.0.6 root@raspberrypi:~# python3 Python 3.7.3 (default, Jan 22 2021, 20:04:44) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information.

    import pylontech p = pylontech.Pylontech() Traceback (most recent call last): File "", line 1, in AttributeError: module 'pylontech' has no attribute 'Pylontech' p = import pylontech p = pylontech.Pylontech() Traceback (most recent call last): File "", line 1, in AttributeError: module 'pylontech' has no attribute 'Pylontech'

    opened by VictorBucha 2
  • Fix temperature calculation

    Fix temperature calculation

    I thought the temperature data reported looked a bit suspicious, so I took a closer look at the protocol specification and found temperatures specified like this (typos included :)

    "Kelvin temperature: K Temperature blow 0 the value is negative e.g.: 25.5°C = 25.510+2731=2986 -12.4°C = -12.410+2731=2607"

    Something like this seems to give more reasonable temperatures for my battery:

     class ToCelsius(construct.Adapter):
         def _decode(self, obj, context, path) -> float:
    -        return obj / 100
    +        return (obj - 2731) / 10.0  # in Kelvin*10
    

    I have implemented this and updated tests. This builds on top of PR #16, so if you merge this you can ignore the other one.

    opened by abelsson 1
  • wonderfull,

    wonderfull,

    wonderfull,

    Exactly what I'am looking for to translate JK-BMS rs485 protocole (or other BMS) to pylontech rs485 protocole , and then have my voltronic/mppsolar inverter talk to my DIY battery.

    Thank you.

    opened by demoze 1
  • US2000 Assertion Error

    US2000 Assertion Error

    Traceback (most recent call last):
      File "pylontech/pylontech.py", line 250, in <module>
        print(p.get_values_single(2))
      File "pylontech/pylontech.py", line 236, in get_values_single
        f = self.read_frame()
      File "pylontech/pylontech.py", line 186, in read_frame
        f = self._decode_hw_frame(raw_frame=raw_frame)
      File "pylontech/pylontech.py", line 167, in _decode_hw_frame
        assert got_frame_checksum == int(frame_chksum, 16)
    AssertionError
    

    Hooked up via instructions in readme. Dumping raw frames shows some data, but doesn't seem to decode:

    b'\xb0\xf7\xf7\xf7\xf7w\xf7\xf7\xf7\xbf\xbf\xbf\xbf\xbf\xf7\xff\xbb\xbb\xef\x00~20024600C06E11020F0CA30CA50CA30CA30CA20CA40CA50CA30CA30CA50CA60CA50CA50CA50CA4050B0F0AF10AF10AF10AF10000BD9D157C02C3500283E517\r\x00'
    

    it's a vanilla US2000, made in 2019

    opened by jasonmadigan 1
  • US2000: works perfectly. US2000C: no communication at all

    US2000: works perfectly. US2000C: no communication at all

    As title says: has anyone successfully communicated with US2000C? When talking to US2000C I only get

    File "/usr/lib/python3.10/site-packages/pylontech/pylontech.py", line 270, in get_values f = self.read_frame() File "/usr/lib/python3.10/site-packages/pylontech/pylontech.py", line 200, in read_frame f = self._decode_hw_frame(raw_frame=raw_frame) File "/usr/lib/python3.10/site-packages/pylontech/pylontech.py", line 181, in _decode_hw_frame assert got_frame_checksum == int(frame_chksum, 16) ValueError: invalid literal for int() with base 16: b'' Tested with two different setups (RPI, batteries) - same result...

    opened by df8oe 15
  • Pack 1 (master) of 7 errors when I get details.

    Pack 1 (master) of 7 errors when I get details.

    I have 7 2000C with firmware 1.7 flashed onto them. They all functioning as-expected and talk quite happily to BatteryView and the Victron Inverter.

    When I try get details for pack 1 (the master) the python crashes on the line

        assert got_frame_checksum == int(frame_chksum, 16)
    

    If I run pack 2-7, works just fine. How can I debug this?

    opened by CrispinP 1
  • Improve implementation of get_management_info function

    Improve implementation of get_management_info function

    Hi!

    I've hacked a bit further and I think the get_management_info message seem to work for me now, so we can see when the battery requests charging and when it allows charging / discharging - which could be used for inverter control.

    Please let me know if you think this looks right or if there's any comments or concerns :)

    opened by abelsson 0
  • Observations and Bugs connecting to US2000C (y2022)

    Observations and Bugs connecting to US2000C (y2022)

    Hi,

    first, thanks for implementing this crappy protocol.

    Initially I thought my RS485 connection was not working as I was not able to establish any communication. But as soon as I did send the example "string" from the protocol documentation the battery started talking. So I thought that something was wrong with the code or the checksum (or python version) and started reversing and debugging. It does also NOT help that the checksum explanation in the PDF is wrong (wrong sum for the sample characters given). Anyway, the code is/was ok.

    I'm not able to query anything except "get_values_single" (I'm getting ignored by the battery, no response). And the first battery is 2, for whatever reason. So no get_protocol_version, no get_module_serial_number. (?)

    Is this normal? I do not have a gateway. Just 4 linked batteries us2000C of the same type.

    Also I found a bug in the code:

    According to the PDF (Page 15 "M+N+6") there is a field "UserDefinedData" in the data returned by get_values_single (42H), that is missing in the code/struct. This leads to data garbage after "RemainingCapacity".

    Sooo... around Line 99:

         99     get_values_single_fmt = construct.Struct(
        100         "NumberOfModule" / construct.Byte,
        101         "NumberOfCells" / construct.Int8ub,
        102         "CellVoltages" / construct.Array(construct.this.NumberOfCells, ToVolt(construct.Int16sb)),
        103         "NumberOfTemperatures" / construct.Int8ub,
        104         "AverageBMSTemperature" / ToCelsius(construct.Int16sb),
        105         "GroupedCellsTemperatures" / construct.Array(construct.this.NumberOfTemperatures - 1, ToCelsius(construct.Int16sb)),
        106         "Current" / ToAmp(construct.Int16sb),
        107         "Voltage" / ToVolt(construct.Int16ub),
        108         "Power" / construct.Computed(construct.this.Current * construct.this.Voltage),
        109         "RemainingCapacity" / DivideBy1000(construct.Int16ub),
        110         "UserDefinedItems" / construct.Byte,
        111         "TotalCapacity" / DivideBy1000(construct.Int16ub),
        112         "CycleNumber" / construct.Int16ub,
        113     )
    

    See line 110 of my code snipped. Also this is still not correct according to the protocol (PDF), as the following numbers are an array of the size of "UserDefinedItems". Did not investigate further into this. I do not know if this breaks something I'm not able to call. For me this fixes "TotalCapacity" and "CycleNumber", but I do not have batteries > 65ah.

    Protocol version is still 2.0 for me (b'~200246...).

    Greetings Frank

    opened by stei-f 4
  • Can’t get any data from UP2500

    Can’t get any data from UP2500

    Hi! I’ve tried all combinations of A/B And DIP on/off (changing baud rate in code) and using L0/L1 But I don’t seem to get any answers from the battery. Currently the battery is not connected anywhere, but SOC does flash so I suppose it should reply in RS485?

    Could this be just because the model is UP2500 vs US2000 and therefore it has some different commands?

    or do I have some other issue?

    what do you think, Frank?

    thanks, Dennis

    opened by n4dmh 5
Owner
Frank
🚞 Trains over IPv6 on Kubernetes
Frank
[ICML 2021, Long Talk] Delving into Deep Imbalanced Regression

Delving into Deep Imbalanced Regression This repository contains the implementation code for paper: Delving into Deep Imbalanced Regression Yuzhe Yang

Yuzhe Yang 568 Dec 30, 2022
mbrl-lib is a toolbox for facilitating development of Model-Based Reinforcement Learning algorithms.

mbrl-lib is a toolbox for facilitating development of Model-Based Reinforcement Learning algorithms. It provides easily interchangeable modeling and planning components, and a set of utility functions that allow writing model-based RL algorithms with only a few lines of code.

Facebook Research 724 Jan 4, 2023
OpenDILab RL Kubernetes Custom Resource and Operator Lib

DI Orchestrator DI Orchestrator is designed to manage DI (Decision Intelligence) jobs using Kubernetes Custom Resource and Operator. Prerequisites A w

OpenDILab 205 Dec 29, 2022
FluidNet re-written with ATen tensor lib

fluidnet_cxx: Accelerating Fluid Simulation with Convolutional Neural Networks. A PyTorch/ATen Implementation. This repository is based on the paper,

JoliBrain 50 Jun 7, 2022
Jittor Medical Segmentation Lib -- The assignment of Pattern Recognition course (2021 Spring) in Tsinghua University

THU模式识别2021春 -- Jittor 医学图像分割 模型列表 本仓库收录了课程作业中同学们采用jittor框架实现的如下模型: UNet SegNet DeepLab V2 DANet EANet HarDNet及其改动HarDNet_alter PSPNet OCNet OCRNet DL

null 48 Dec 26, 2022
A mini lib that implements several useful functions binding to PyTorch in C++.

Torch-gather A mini library that implements several useful functions binding to PyTorch in C++. What does gather do? Why do we need it? When dealing w

maxwellzh 8 Sep 7, 2022
Patient-Survival - Using Python, I developed a Machine Learning model using classification techniques such as Random Forest and SVM classifiers to predict a patient's survival status that have undergone breast cancer surgery.

Patient-Survival - Using Python, I developed a Machine Learning model using classification techniques such as Random Forest and SVM classifiers to predict a patient's survival status that have undergone breast cancer surgery.

Nafis Ahmed 1 Dec 28, 2021
Objective of the repository is to learn and build machine learning models using Pytorch. 30DaysofML Using Pytorch

30 Days Of Machine Learning Using Pytorch Objective of the repository is to learn and build machine learning models using Pytorch. List of Algorithms

Mayur 119 Nov 24, 2022
This source code is implemented using keras library based on "Automatic ocular artifacts removal in EEG using deep learning"

CSP_Deep_EEG This source code is implemented using keras library based on "Automatic ocular artifacts removal in EEG using deep learning" {https://www

Seyed Mahdi Roostaiyan 2 Nov 8, 2022
A Web API for automatic background removal using Deep Learning. App is made using Flask and deployed on Heroku.

Automatic_Background_Remover A Web API for automatic background removal using Deep Learning. App is made using Flask and deployed on Heroku. ?? https:

Gaurav 16 Oct 29, 2022
Cancer-and-Tumor-Detection-Using-Inception-model - In this repo i am gonna show you how i did cancer/tumor detection in lungs using deep neural networks, specifically here the Inception model by google.

Cancer-and-Tumor-Detection-Using-Inception-model In this repo i am gonna show you how i did cancer/tumor detection in lungs using deep neural networks

Deepak Nandwani 1 Jan 1, 2022
MohammadReza Sharifi 27 Dec 13, 2022
Video lie detector using xgboost - A video lie detector using OpenFace and xgboost

video_lie_detector_using_xgboost a video lie detector using OpenFace and xgboost

null 2 Jan 11, 2022
A fast python implementation of Ray Tracing in One Weekend using python and Taichi

ray-tracing-one-weekend-taichi A fast python implementation of Ray Tracing in One Weekend using python and Taichi. Taichi is a simple "Domain specific

null 157 Dec 26, 2022
Technical Indicators implemented in Python only using Numpy-Pandas as Magic - Very Very Fast! Very tiny! Stock Market Financial Technical Analysis Python library . Quant Trading automation or cryptocoin exchange

MyTT Technical Indicators implemented in Python only using Numpy-Pandas as Magic - Very Very Fast! to Stock Market Financial Technical Analysis Python

dev 34 Dec 27, 2022
Space-invaders - Simple Game created using Python & PyGame, as my Beginner Python Project

Space Invaders This is a simple SPACE INVADER game create using PYGAME whihc hav

Gaurav Pandey 2 Jan 8, 2022
Yolov5-opencv-cpp-python - Example of using ultralytics YOLO V5 with OpenCV 4.5.4, C++ and Python

yolov5-opencv-cpp-python Example of performing inference with ultralytics YOLO V

null 183 Jan 9, 2023