The main code is the lighter version of Pytes_serial, written in MicroPyton to be used with ESP32.
This application runs on an ESP32 board and reads the RS232 serial port of PYTES or PYLONTECH batteries and sends data to Home Assistant via MQTT. Home Assistant autodiscovery is build in, so device will show up in Homeassistant automatically. A signal converter from RS323 to TTL is needed between Pytes serial console port and ESP32 serial port.
Program reads serial port with a specific freqvency, parsing the data and send it trought MQTT. Program has build-in integration with Home Assisant where the following sensors will be automatic created for each battery:
- "pytes_esp_current", "pytes_esp_voltage", "pytes_esp_temperature", "pytes_esp_soc", "pytes_esp_status" the battery number is embeded at the end of each sensor (i.e pytes_esp_current_1, pytes_esp_current_2...)
- when cells_monitoring = 'true' in config.py an additional device will be created in Home Assistant with sufix "_cells" with all associated sensors. The battery and cell number is embeded at the end of each sensor ( i.e. voltage_102 means voltage for battery 1 cell 2). Under monitoring_level, three levels of details can be selected.
If more sensors will be needed, they can be added manually as per Home Assistant documentation MQTT sensor and the example in docs folder here. You can have more examples in full fladged program Pytes_serial for better understanding of what program does.
ESP32 board ---------------------------- tested on ESP32 WROOM 38 pins
RS232 to TTL converter ----------------- tested on RS232 to TTL
RJ45 to RS232 DB9(male) cable -------- tested on RJ45 to DB09 male
simple connection flow: esp32 uart2 used for tx/rx -> TTL to RS232 -> DB09 to RJ45 cable -> Pytes RJ45 console port
connection with RS232serial to TTL with DB9(female) converter using RJ45 to RS232 DB9(male) cable - TESTED:
- ESP32 Pin: 16 RXD2 ------ RX converter RX DB9 Female ------ DB9 Male console cable RJ45 Pin: 3 TXD
- ESP32 Pin: 17 TXD2 ------ TX converter TX DB9 Female ------ DB9 Male console cable RJ45 Pin: 6 RXD
- ESP32 PIN: GND ------ GND converter GND DB9 Female ------ DB9 Male console cable RJ45 Pin: 4 GND
- ESP32 PIN: 3.3V ------ VCC converter DB9 Female ------ DB9 Male console cable RJ45
If you have basic soldering skills, a simple module RS232 to TTL converter (without DB9 connector) can be used too (not tested).
connection with simple RS232serial to TTL module (without DB9 connector) - NOT TESTED:
- ESP32 Pin: 16 RXD2 ------ RX converter RX ------ RJ45 Pin: 3 TXD
- ESP32 Pin: 17 TXD2 ------ TX converter TX ------ RJ45 Pin: 6 RXD
- ESP32 PIN: GND ------ GND converter GND ----- RJ45 Pin: 4 GND
- ESP32 PIN: 3.3V ------ VCC converter
Both coonection works, important is to respect the PYTES console port pinout documentation and to ensure correct connection of TX, RX trough all chain, that was proved to be one of main source of failure and frustration. In case something goes wrong, swapping RXD and TXD on one side will do the magic in most of the cases. You can find more pictures in docs folder.
- Install Thonny IDE -- interpreter to write,read with ESP32 -- guide here
- Install Micropython firmware on the ESP32 board -- latest firmware here
- Download pytes_esp repository and copy the content of ESP32 folder to ESP32device
- Open config.py file and do your configuration (wifi, mqtt, batteries) -> save to ESP32
- Connect the hardware as per recomandation above
- Press restart ESP32 device and you should be able to see:
When the ESP32 is powered, the script starts running in a loop and will read the serial port with a defined frequency. After version updates it is highly recomanded to delete all devices previous created in Home Assistant and to let them to be recreated at script start.
The LED on the ESP32 board can provide some functionality details as follows:
- initialization phase (wifi, MQTT discovery) - fast blinks [1/0.1sec]
- initialization errors - 10s of constant blinks [1/sec] before device REBOOT
- normal operation - led ON reads, led off during waiting time
- program stopped - no led activity -- can be ON or OFF
enjoy