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
- Solar Cloud opens one connection to the gateway (its address, port 5020), keeps it open and shares it between all systems that use this gateway. After a gateway restart it reconnects by itself.
- It sends standard Modbus TCP requests. The unit ID in each request is the device's address on the RS485 bus, the field Device address (Modbus unit ID) in Solar Cloud. So one gateway serves several devices, such as batteries 1 and 2 and inverter 5.
- It only reads (Modbus functions 3 and 4), in blocks of neighbouring registers with a short pause between requests, once per read interval (60 seconds by default). It never changes anything on a device.
- The gateway puts one request at a time on the bus (RS485 can't do two at once) and queues the rest, so other programs can use the same gateway.
- A device that doesn't answer comes back as gateway exception
0x0B. Solar Cloud shows "the device did not answer" and tries again at the next reading. - The bus settings (baud rate, parity, stop bits) are set on the gateway, not in Solar Cloud.
1. What you need
- A Linux computer on the same network as the Solar Cloud server, with Python 3.8 or newer.
- A USB-RS485 adapter. Wire A to A and B to B (sometimes marked D+ and D−) and, if the device has one, GND to GND.
- Your devices' RS485 settings (baud rate, parity, stop bits) and addresses, from their manuals or display menus.
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|2 | Your devices' bus settings. 9600 8N1 is common; 8E1 and 8N2 too. |
--listen 0.0.0.0:5020 | Where Solar Cloud connects. Use a port above 1024 (502 needs root on Linux). |
--gap 50 | Milliseconds 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.0 | How long the gateway waits for a device's answer (seconds). |
--idle-timeout 600 | Closes 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 8 | The most connections at once (default 8; 0 = no limit). More are refused and logged. |
--rtu-listen 0.0.0.0:5021 | Optional: raw RTU frames over TCP for other programs (e.g. minimalmodbus). Solar Cloud doesn't use it. |
-v | Log 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
- The gateway has no password and passes on writes. Anyone who can reach port 5020 can read and change the
devices on the bus. Keep it on your home network: never forward port 5020 on your router, and limit it with a firewall
if others use your network, e.g.
sudo ufw allow from <Solar Cloud server> to any port 5020. - Only one program may open the serial port. While the gateway runs, other programs must go through the gateway too.
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 request | Wrong 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 502 | Ports 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 log | Another 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 log | The USB adapter was unplugged or failed. |