Automated agricultural irrigationRaspberry Pi (Docker)Modbus TCPAcquisition
Reading a Fronius inverter over SunSpec Modbus TCP
Why pay for grid power to irrigate when the panels are already producing? This example reads a Fronius inverter over SunSpec Modbus TCP from a Raspberry Pi: AC power generated (model 113, float32) plus the consumption measured by the Smart Meter (model 211). With both values you compute the live solar surplus and authorize the pump only when photovoltaics can carry it. It is the exact acquisition layer running in a real automated irrigation deployment.
SunSpec does the heavy lifting
Instead of hunting raw register addresses in the inverter manual, the pysunspec2 client calls scan() and autodiscovers every SunSpec model the Fronius exposes on port 502. Generated power is then one attribute away: inverter_three_phase_float[0].W.value. The same device object exposes the Smart Meter as meter[0], so a single TCP connection gives you both ends of the energy balance.
Surplus, not just production
Generation alone is misleading — the farm may already be consuming most of it. The script subtracts the meter reading from inverter output to get the true exportable surplus, and only authorizes irrigation above a configurable threshold (4 kW in the example, sized to the pump). That decision feeds the Modbus command to the Schneider Altivar 320 driving the pump.
Built to survive the night
Fronius inverters power down their Modbus interface at dusk, so naive pollers crash every evening. The read loop catches failures, logs them and retries on the next cycle instead of aborting. In production the same code runs inside a Docker container on the Raspberry Pi and publishes values to Node-RED, where the irrigation logic and the Telegram bot consume them.
A snippet from the implementation
Straight from the example as deployed on the Raspberry Pi (Docker) — copy it freely:
def connect():
"""Opens the Modbus TCP connection and scans the device's SunSpec map."""
d = SunSpecModbusClientDeviceTCP(
slave_id=SLAVE_ID, ipaddr=INVERTER_IP, ipport=INVERTER_PORT
)
d.scan() # auto-discovers the available SunSpec models
return d
The full example is a complete program — wiring header, setup and main loop — ready to adapt to your application.
Frequently asked questions
Do I need to configure anything on the Fronius inverter?
Yes, enable the Modbus TCP slave in the inverter settings, select float as the SunSpec data type and keep the default port 502. The Smart Meter must be registered in the inverter so model 211 is exposed.
What is the difference between SunSpec models 113 and 211?
Model 113 is the three-phase inverter block with float32 registers, reporting what the inverter generates. Model 211 is the three-phase meter block, reporting what the installation imports or exports at the grid connection point.
Can I run this against other inverter brands?
Any inverter implementing the SunSpec Modbus standard works with the same code, since pysunspec2 discovers the models dynamically. Only the slave id, IP address and available model names may change.