An RS485 bus on the network

Many inverters, chargers and batteries talk Modbus over RS485: two wires, A and B. The Modbus gateway is a small program that runs on a computer next to them (for example a Raspberry Pi) with a USB-RS485 adapter. It puts the bus on your network, so Solar Cloud can read the devices from another machine.

Solar Cloud server ──┐
other programs     ──┴─▶ network, TCP port 5020 ─▶ modbus_gateway.py ─▶ USB-RS485 adapter ─▶ RS485 bus ─▶ devices

Log in to download the gateway Free account: sign up.

A gateway on your home network can only be read by a Solar Cloud server on that same network, i.e. when you run Solar Cloud yourself. On a public Solar Cloud, accounts may only connect to public internet addresses.

How Solar Cloud talks to the gateway

1. What you need

2. Install

sudo mkdir -p /opt/modbusgateway && sudo chown $USER /opt/modbusgateway
cp modbus_gateway.py /opt/modbusgateway/
pip3 install --user pyserial            # or: sudo apt install python3-serial
sudo usermod -aG dialout $USER          # access to the serial port; log out and in again
ls /dev/serial/by-id/                   # the adapter's fixed name (better than /dev/ttyUSB0, which can change)

3. Try it by hand

python3 /opt/modbusgateway/modbus_gateway.py /dev/serial/by-id/YOUR-ADAPTER --baud 9600 --listen 0.0.0.0:5020 --gap 50 -v
--baud, --parity N|E|O, --stopbits 1|2Your devices' bus settings. 9600 8N1 is common; 8E1 and 8N2 too.
--listen 0.0.0.0:5020Where Solar Cloud connects. Use a port above 1024 (502 needs root on Linux).
--gap 50Milliseconds of quiet on the bus between requests. Real devices often need far more than the Modbus minimum; 40–50 is a safe start.
--timeout 1.0How long the gateway waits for a device's answer (seconds).
--idle-timeout 600Closes a connection that sends nothing for this many seconds (default 600; 0 = never), so programs that vanished without closing don't pile up. Solar Cloud reconnects by itself.
--max-clients 8The most connections at once (default 8; 0 = no limit). More are refused and logged.
--rtu-listen 0.0.0.0:5021Optional: raw RTU frames over TCP for other programs (e.g. minimalmodbus). Solar Cloud doesn't use it.
-vLog every request and answer, to see what happens.

Then in Solar Cloud: Add a system → Read my inverter directly, connection RS485 gateway, the gateway computer's address, port 5020, the device address, and press Test. Stop the gateway with Ctrl+C when you're done trying.

4. Start it automatically

Download the service file template, fill in the user, folder, serial port and baud rate, and install it:

sudo cp modbusGateway.service.example /etc/systemd/system/modbusGateway.service
sudo nano /etc/systemd/system/modbusGateway.service
sudo systemctl daemon-reload
sudo systemctl enable --now modbusGateway
systemctl status modbusGateway          # running?
journalctl -u modbusGateway -f          # its log

Safety

When it doesn't work

"Gateway … can't be reached"The gateway isn't running, the address or port is wrong, or a firewall is in between.
"The device did not answer" for every requestWrong device address, device off, wrong baud rate / parity / stop bits on the gateway, or A and B swapped.
Now and then "did not answer", mostly with several devices--gap too small: try 50.
PermissionError on port 502Ports below 1024 need root: use 5020.
Permission denied: '/dev/ttyUSB0'The user isn't in the dialout group (log out and in after adding).
refused: already 8 connections in the gateway's logAnother program opens a new connection for every reading and doesn't close them: fix that program, or raise --max-clients.
gateway error: … in the gateway's logThe USB adapter was unplugged or failed.