Skip to content

MacGH23/BatteryScript_ABCDs

Repository files navigation

ABCDS: Automatic (DIY) Battery Charge / Discharge Script

Python3 script for fully automatic charge and discharge a DIY battery for photovoltaic systems to provide a "zero feed-in".

WARNING - USE THIS SCRIPT TOTALLY ON YOUR OWN RISK !!! (needed to add ;-) Check which device can be used in your country and fullfill all regulatory requirements !

The idea of this script is to provide a framework for handling the power stuff,
and everyone can add new devices and provide it to the community.

Current tested/running on a Raspberry Pi:

  • Raspberry Pi Zero
  • Raspberry Pi 2

Should work on any Raspberry Pi.

Status Webserver:
For a better overview a own webserver at port 9000 is available (can be configured in conf file).
Here you can have a quick overview of the current status, current config and BMS.
You can also enable and disable Charger and DisCharger
For automation you can call a restart (see below BSstart.py) via URL:
[IP:PORT]/DIRECTRESTART[x]
x = 0..3

Current supported hardware:
Charger:

  • Meanwell BIC-2200
  • Meanwell NPB-[450/750/1200/1700]-[12/24/48/96] incl. voltage adjust for Chargecurrent < MinCurrent (able to charge below MinCurrent)
  • Constant current based power supply via external power switch
    IP based power switch supported now: Tasmota, Shelly
  • Simulator, can be used for testing
  • Huawei R48xxGx 48V in development, need a sample, if you have one which you can share please contact me

Important Note for Meanwell devices !
Changing the output values are limited to 4.000.000 by design.
The NPB and BIC2200 has a newer firmware with an additional option to disable the EEPROM write (from 02/2024 - option will be set if available)
This can only be updated by Meanwell !
Therefore several options are available to configure.

  • ZeroDeltaChargerWatt = 30 (range in WATT without set device to new value)
  • chargercounter = 8 (wait for change, multiply with METER updates in seconds.)
    e.g. (Meter update all 2 seconds) x (8) = only after 16 seconds an update will be send if necessary

Note for Meanwell NPB:
Voltage adjust is avaiable for NPB devices. This will lower the charge voltage to lower the charge current if the charge current is smaller than min charge current.
It is not 100% exact, because the voltage <-> current is not exact, but you are able to charge.

Note for all Meanwell EEPROM
If EEPROM write is disabled the values which are written before disabling will be used by system start
If you want to change the "default" you have to enable write EEPROM again and change it with mycancmd.py in "/charger" folder e.g.
./mwcancmd.py.py systemconfigset 1
./mwcancmd.py.py cvset 2760
./mwcancmd.py.py dvset 2440
./mwcancmd.py.py systemconfigset 1025

Discharger:

  • Meanwell BIC-2200
  • Lumentree (by ask4it) 600/1000/2000, upto 3 can be used in parallel
  • Sun1000/2000 inverters with TruckiRS485 PCB should also work with UART and RS485, but not tested
  • Simulator, can be used for testing

BMS:
Note: Use a wired connection if possible, Bluetooth is not recommended !

  • JKBMS with original JK RS485 interface adapter (dedicated lib)
  • DalyBMS with original Daly UART interface adapter (no Bluetooth, dedicated lib)
  • almost all BMS supported by standalone_serialbattery lib based on venus-os_dbus-serialbattery Supported BMS
    Note:
    Currently only one single BMS is supported and not testes with all of the BMS. But should work
    Use standalone_serialbattery_test in SASB folder to test your BMS

Power meter information:
To adjust everything automatically, you need to get the current power.
Positive = Take power from the GRID
Negative = Get power from PV

  • MQTT (get vlaues from any mqtt broker)
  • Shelly (EM, 3EM, 3EMPro, 1PM, 1PMPro)
  • Tasmota
  • IOBroker via simple REST API (install the "RESTful API" adapter)
  • EMLOG (not tested)
  • VZlogger (not tested)
  • AMIsLeser (not tested - https://www.mitterbaur.at/amis-leser.html)
  • Simulator, can be used for testing (random power value)

Additional hardware needed, depending on the used hardware

Device / HW Interface Recommended
Meanwell CAN Waveshare RS485 CAN HAT or Fischl USBTin
Waveshare RS485CAN Fischl USBTin
Lumentree RS232 Any simple USB to RS232 adapter
USB RS232 or USB RS232
See Lumentree installation hints
JKBMS RS485 Waveshare RS485 CAN HAT with original JKRS485 adapter or simple USB-RS485 adapter
JKBms RS485
DALYBMS UART Original DALY UART USB adapter
DALYBms UART RS485

Meanwell devices using a small 2x7 pin, 2.0 pitch connector from MPE for the CAN interface.
Normally a connector with 2 wires are part of the delievery. The connector is a MPE BLC 14 (partnumber 906-2-014-X-BS0A10) and crimp pins (partnumber CC2-22/30-TT-RL)

Only the CAN wires are needed to install additionally. See manual of the device where the CAN PINs are.

Lumentree devices uses a normal RS232 interface with modbus protocol.
Easiest methode to connect is a simple USB to RS232 adapter.
But see also Lumentree installation hints, because of Pin 9!

JKBMS is optional. You have to use the original JK RS485 adapter and connect it to a RS485 interface at the Raspberry.
Use the Waveshare CAN/RS485 HAT. Especially if you also use a CAN device or simples USB-RS485 adapter.

MQTT publish:
You can publish some data form the script to the mqtt server.
Change the already implemented ones in the charger.conf, or add a new in the script.

  • mqttpublishWATT : send the status of charger / discharger
    (positive = discharger, negative = charger)
  • mqttpublishSOC = SOC status of BMS 0..100 (if used)

Usage / Installation:
All settings can be configured in BSsetup.conf
All the options are explained or self explaining in detail in the conf file.
For all devices exists a testprogram in the devices folder (e.g. lt232test for Lumentree)
Here you can test all devcies seperatly and get the right "/dev/..." and id needed IDs for the access, before running the BatteryScript

Change to executeable by chmod 755 BatteryScript.py
Change to executeable by chmod 755 BSstart.py
Run: sudo chmod 755 BatteryScript.py
Run: sudo chmod 755 BSstart.py

For first test just start the script after you configuerd the BSsetup.conf
./BatteryScript.py

To start using TMUX you can ./BSstart.py 0
./BSstart.py 0

Since I forget always the command to attach to the tmux window just call:
./ShowBS.sh
or
./BSstart.py 9
To detach, press CTRL+"b" -> release -> Press "d"
To scroll inside TMUX, press CTRL+"B" -> release -> Press "Page Up"
To exit scroll mode press "q"

Options for BSstart [0..3,9]
Default "0" just starts the script,
1..3 can be definded on your own startup procedure e.g. different settings for summer / winter
9: show the tmux console"
For a 10 second delay call BSstart.py with [10..13] (network boot delay)

Tmux Startup
Run during startup:
Thee are several ways to start the script automatically.
I use an entry in cron to start the BSstart.py

crontab -e
(add at the end:)
@reboot /home/pi/ABCDs/BSstart.py 10

Change the path to the right one.
Use 10 as argument to be sure that network is up.

systemd startup
To use the script as a service copy the file ABCDS.service to /etc/systemd/system (not tested now, tmux is recommended)
sudo cp ABCDS.service /etc/systemd/system
sudo systemctl daemon-reload
systemctl start book-scraper

Additional Features
BatteryScript_external.py:
Here you can code your own extensions for several IO/GPIO actions

GPIO:
Upto 4 GPIO buttons are already prepared, but you have to implement the action yourself

LCD:
I added a cheap 2x16 Character LCD with i2c interface.
Use the green one, because it can run without backlight.
The statusinfo is displayed on it.
Installation:
Enable i2c with
sudo raspi-config -> Interfaces
Install python3 smbus lib:
sudo apt install python3-smbus

Webserver:
An integrated webserver at port 9000 is implemented to provide information and some interaction like restart, reboot, ...
http://[IP_of_Raspi]:9000

Installation:
Automatic installation:
wget -O - https://github.com/MacGH23/BatteryScript_ABCDs/blob/ABCDs_install.sh | sudo bash

Manual Install with Git and clone the repository:
sudo apt install git
git clone https://github.com/MacGH23/BatteryScript_ABCDs.git BatteryScript_ABCDs
Some python lib needed. Please install:
pip3 install pyserial
pip3 install paho-mqtt
--> if you already have installed paho-mqtt < 2.0 you have to update to 2.x
pip3 install -U paho-mqtt
apt install pip3 python3-can
pip3 install ifcfg
pip3 install minimalmodbus
pip3 install configupdater
pip3 install psutil
pip3 install schedule
pip3 install rpi
Install Tmux
sudo apt install tmux

For the serial communication with Lumentree and BMS the user must be added to dialout group
sudo usermod -a -G tty $USER
sudo usermod -a -G dialout $USER

Lumentree installation hints:

  • Disable ModemManager to prevent scanning COM ports
    sudo systemctl stop ModemManager
    sudo systemctl disable ModemManager

  • Disable Lumentree during startup
    Since Lumentree devices startup with max. watt output during AC on (e.g. resume from power failure), you should add "lt232test setwatt 0" to crontab
    Be sure that the device path "/dev/USBx" in lt232test is correct and working !
    Change the path to the right one.

crontab -e
(add at the end:)
@reboot /home/pi/ABCDs/DisCharger/lt232test.py setwatt 0
  • Lumentree should not be updated too quickly
    Setting recommendation:
    LastDisChargePower_delta = 15 (between 10..20)
    DisChargerPowerCalcCount = 4 (or higher)
    If you have problems (no answer or checksum error) with the USB RS232 adapter it could be that Lumentree DSUB9 connector is not 100% compatible with RS232 spec.
    It uses
    PIN2 : TX
    PIN3 : RX
    PIN5 : GND
    PIN9 : 12V <-- This can cause communication errors or defect for the USB<->RS232 adapter.
    In this case use a additional cable from the USB RS232 which only uses PIN2, PIN3, GND (USB -> RS232 -> Cable PIN2/3/GND -> Lumentree)
    Or remove PIN9 from the RS232 adapter
    Or use a RS232 port protector (1:1) and remove PIN9
    RS232_pp

Meanwell installation hints (you take the risk !!):

  • BIC-2200 (need to be installed by a electrically qualified person):
    Normally a ON Jumperwire is installed in the device.
    Before setup everything (including AC/DC cables) remove this cable (prevent to start before configured).
    Connect the CANBus cable to the RasPi but not the DC wires.
    Now connect it to AC.
    The Device should start itself but should not do any AC<->DC conversion.
    Run the script the first time and allow to set the needed parameter.
    After that you need do a AC power cycle.
    Now connect the DC wires to the battery during it is OFF and
    connect the ON Jumperwire back to device.
    Connect it to AC and run the script (after configuration of cource).
  • NPB:
    Connect the CANBus cable to the RasPi but not the the DC wires to the battery.
    Connect it to AC and switch it on.
    Run the script the first time and allow to set the needed parameter.
    After that you need do a AC power cycle.
    Now connect the DC wires to the battery during it is OFF.
    Run the script (after configuration of cource).

DALY BMS hints:
DALY BMS needs quite long to answer, use min. 2 seconds for meter update

CAN/RS485 devices hints:
If you see problems during init of CAN device, check / add an entry in
sudo nano /etc/hosts
127.0.1.1 [Hostname of your Raspberry]

Waveshare CAN RS485 HAT Short summery:
Insert the module into the Raspberry Pi, modify the start-up script "config.txt".

sudo nano /boot/config.txt
or
sudo nano /boot/firmware/config.txt

Add the following content at the file:
dtparam=spi=on
dtoverlay=mcp2515-can0,oscillator=12000000,interrupt=25,spimaxfrequency=2000000
reboot raspberry

If you have problems with RS485 and waveshare HAT, check if console outout is set to serial in cmdline.txt
sudo nano /boot/firmware/cmdline.txt
Change
console=serial0,115200 console=tty1 [...]
to
console=tty1 [...]
reboot raspberry

Add new devices:
You have to provide the interface to the device. If possible add a class in a subfolder for easier handling:
Add a:

  • Dis or Charger_[Device] function for the final communication
  • Add the devices in the StartStop[Dis]Charger functions
  • For BMS devices see the BMS sections for it
  • Add the initialisation and shutdown code in "main" and "onexit"

Credits:
Thanks to:
https://github.com/stcan/meanwell-can-control for first idea for this script
https://github.com/reserve85/HoymilesZeroExport where I take the code for the http meter request
https://github.com/fah/jk-bms for the JKBMS interface
https://github.com/dreadnought/python-daly-bms for Daly BMS interface
https://github.com/mr-manuel/venus-os_dbus-serialbattery for base of standalone-serialbattery BMS interface

About

Automatic Battery Charge Discharge script

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published