I’m working with an EM540 energy meter, which is connected to a Waveshare Modbus RTU to TCP converter. My goal is to display energy consumption data on a CerboGX running VenusOS.
To achieve this, I’ve updated the default carlo_gavazzi.py Modbus client Python script for the dbus-modbus-client module. You can find the script here on GitHub. Initially, I integrated it by replacing the default module on the CerboGX (via SSH), which worked fine and allowed me to get things up and running. However, after updating VenusOS, my changes were overridden, reverting the module to its standard behavior and losing the EM540 support over TCP.
Now, I would like to integrate my custom client more consistently with the dbus-modbus-client module, ensuring it loads properly with the system. Specifically, I’m looking for guidance on how to import and load my custom client into the dbus-modbus-client.py file so that it persists through updates.
Any help or pointers would be greatly appreciated!
—
I took a different approach to integrating my EM530.
My waveshare is setup in raw mode, so ModbusRTU is sent over TCP.
I’ve got scripts that run services to remap a tcp/ip port to a serial device (socat).
Then the Cerbo GX can’t tell that the port isn’t local and it works with the existing driver.
I posted this in the old community, should still be around.
Unfortunately this isn’t working with my KNX system which connects to a heatpump over Modbus TCP. So still need to figure out how to load the adjusted client definition to work with the EM540 over Modbus TCP.
With a little bit of tinkering and the help of ChatGPT, I’ve landed on the following solution to integrate a custom Modbus TCP client (in this case, a modified version of an existing client) into VenusOS running on the Cerbo GX.
Steps to Implement:
Create a Script to Register the Custom Client:
Save the following script as /data/scripts/register_custom_modbus_client.sh. This script will:
Clone or update a Git repository containing your custom client.
Symlink the custom client to the appropriate location (/opt/victronenergy/dbus-modbus-client/).
#!/bin/bash
# Set working directory
WORK_DIR="/data/dbus-modbus-client"
CUSTOM_SCRIPT_URL="<<<YOUR GIT REPO HERE>>>"
CUSTOM_CLIENT_NAME="carlo_gavazzi.py"
# Ensure git is installed (you might need to install git if not present)
if ! command -v git &> /dev/null; then
echo "git not found, installing..."
opkg update && opkg install git
fi
# Check if the repository already exists
if [ ! -d "$WORK_DIR" ]; then
echo "Cloning the repository..."
git clone "$CUSTOM_SCRIPT_URL" "$WORK_DIR"
else
echo "Repository already exists. Pulling latest changes..."
cd "$WORK_DIR" && git pull
fi
# Check if the custom client exists in the repo, if not, exit with error
if [ ! -f "$WORK_DIR/$CUSTOM_CLIENT_NAME" ]; then
echo "Error: Custom client script '$CUSTOM_CLIENT_NAME' not found in repository."
exit 1
fi
# Register custom client
echo "Registering the custom client..."
ln -sf "$WORK_DIR/$CUSTOM_CLIENT_NAME" "/opt/victronenergy/dbus-modbus-client/"
# Re-register dbus-modbus-client.py
echo "Registering dbus-modbus-client.py..."
ln -sf "$WORK_DIR/dbus-modbus-client.py" "/opt/victronenergy/dbus-modbus-client/dbus-modbus-client.py"
echo "Custom client registration complete."
Make the Script Executable:
After creating the script, ensure it is executable by running:
VenusOS does not persist /etc/rc.local across updates, but /data/rc.local is persistent. To ensure the custom client is registered on every boot, create the following file:
Finally, ensure that /data/rc.local is executable by running:
chmod +x /data/rc.local
Important Note:
Syncing with the Original Repo: If you are using a forked version of the dbus-modbus-client, it’s important to keep it in sync with the original repository. This will ensure that you benefit from any updates, bug fixes, and improvements made to the core dbus-modbus-client code. You can periodically pull changes from the original repo into your fork to stay up to date.
This is the solution that I implemented, pointers, corrections, and the like are most definately welcome!