This project provides a Python-based bridge that reads power meter data delivered by MQTT and makes it available to Modbus devices. It is designed to work with an ESP8266 running Tasmota firmware configured to read data from a power meter using an optical reader.
- Converts power meter data received via MQTT to Modbus registers.
- Supports various power metrics like voltage, current, frequency, and power.
- Runs as a systemd service for easy deployment and management.
- Python 3.x
- Tasmota firmware on ESP8266 with an optical power meter reader.
- An MQTT broker.
- Modbus TCP server.
git clone https://github.com/Fishy78/mqtt2modbus.git
cd mqtt2modbus
Ensure you have Python 3.x installed. Then, install the required packages using pip:
pip install pyModbusTCP paho-mqtt
Upload the following script to your Tasmota device to configure it for reading power meter data:
>D
>B
->sensor53 r
>M 1
+1,5,s,0,9600,ZAEHLER,1,10,2F3F210D0A,063035310D0A
1,77070100010800ff@1000,Gesamt kWh bezogen,kWh,Power_in,1
1,77070100020800ff@1000,Gesamt kWh geliefert,kWh,Power_out,1
1,77070100100700ff@1,Verbrauch aktuell,W,Wirkleistung,0
1,77070100200700ff@1,Voltage L1,V,Spannung_L1,1
1,77070100340700ff@1,Voltage L2,V,Spannung_L2,1
1,77070100480700ff@1,Voltage L3,V,Spannung_L3,1
1,770701001f0700ff@1,Amperage L1,A,Strom_L1,1
1,77070100330700ff@1,Amperage L2,A,Strom_L2,1
1,77070100470700ff@1,Amperage L3,A,Strom_L3,1
1,770701000e0700ff@1,Frequency,Hz,HZ,2
To configure Tasmota for MQTT, refer to the official Tasmota MQTT Setup Guide.
Edit the mqtt2modbus.conf
configuration file to match your environment:
[mbserver]
ip = 0.0.0.0
port = 502
[mqtt]
ip = 10.10.9.72
client_id = mqtt2modbus
subscription = tele/M-Stromzaehler1/SENSOR
-
Copy the service file to the systemd directory:
sudo cp mqtt2modbus.service /etc/systemd/system/
-
Reload systemd to recognize the new service:
sudo systemctl daemon-reload
-
Start the service:
sudo systemctl start mqtt2modbus.service
-
Enable the service to start on boot:
sudo systemctl enable mqtt2modbus.service
To check if the service is running correctly, use:
sudo systemctl status mqtt2modbus.service
- Ensure that your MQTT broker is receiving data from the Tasmota ESP8266.
- Verify that Modbus registers are being updated by polling the Modbus server.
The following Modbus addresses are used to query specific values. Each entry includes the Modbus register address, the length of the data, and the scaling factor (10^x
), where x
is negative for scaling down:
Metric | Modbus Address | Register Type | Length (Registers) | Faktor 10x |
---|---|---|---|---|
Voltage L1 | 40077 | Holding Register | 1 | 10-1 |
Voltage L2 | 40078 | Holding Register | 1 | 10-1 |
Voltage L3 | 40079 | Holding Register | 1 | 10-1 |
Current L1 | 40072 | Holding Register | 1 | 10-2 |
Current L2 | 40073 | Holding Register | 1 | 10-2 |
Current L3 | 40074 | Holding Register | 1 | 10-2 |
Power (Wirkleistung) | 40087 | Holding Register | 1 | 100 |
Frequency (HZ) | 40070 | Holding Register | 1 | 10-1 |
Power_in | 40090 | Holding Register | 1 | 10-1 |
Power_out | 40091 | Holding Register | 1 | 10-1 |
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please fork the repository and create a pull request with your changes.
If you encounter any issues, please create an issue in the GitHub repository.