How-to
Sending pump commands from a Raspberry Pi to a CLICK PLC
Short answer
Skip the MQTT gateway and VPN: a CLICK PLC with its Modbus TCP server enabled answers a Raspberry Pi directly on a wired Ethernet link. The Pi writes a start request and run duration to agreed coils and holding registers, and the PLC logic validates, latches and consumes the command so pump permissives and timing never leave the controller.

On this page
You do not need an MQTT gateway, a VPN appliance or any extra hardware: a CLICK PLC with its built-in Modbus TCP server enabled takes pump commands straight from a Raspberry Pi over one wired Ethernet link. Give the Pi and the PLC addresses on the same subnet, run a single Modbus TCP client on the Pi (pyModbusTCP in Python or the Modbus nodes in Node-RED), and write a start request plus a run duration into PLC memory the PLC logic itself owns. The PLC validates the request, checks pump permissives, latches the duration into its timer and sets an acknowledge bit, so the pump timing and safety decisions never leave the controller. What you must buy: nothing beyond the Ethernet cable and switch you already have.
Confirm the PLC side before writing any code #
The deciding detail is the installed CLICK CPU and its communication configuration. Modbus TCP server support is enabled in the PLC's communication setup, together with its IP address and subnet mask; read those settings back after you save them. Not every small controller exposes the same memory as Modbus objects, so check the CLICK Modbus addressing table for your CPU: the internal C bits are read/write discrete objects, and data registers are read/write holding registers. That table is also where you confirm which ranges accept writes from an external client.
If reachability works but a read fails, work through the service enablement and connection settings rather than changing IP addresses; the PLC-side checklist in what you must configure on the PLC side for Modbus TCP covers the usual omissions.
Connect the Pi and prove the path #
Put both devices on a restricted local network through a switch, or cable them directly if their interfaces support it. For example, give the PLC 192.168.1.10 and the Pi 192.168.1.50, both with a 255.255.255.0 mask, and check for duplicate addresses. Then test in this order:
- Confirm link status on both interfaces and the switch.
- Ping the PLC address from the Pi.
- Read a documented register with your client library on the Modbus TCP port, 502 by default.
- Read the same value in the PLC programming software and compare.
A ping proves the network; only a successful Modbus read proves you have reached the server, the right object type and the right address. Keep this link on a network that does not touch the internet: Modbus TCP has no authentication, so anything that can reach port 502 can write the command registers.
Define the command map in unused memory #
Treat PLC memory as the interface and never let an external write directly drive a physical output. Pick addresses and fix their meaning before enabling anything:
| Item | Writer | Object | Meaning |
|---|---|---|---|
| Start request | Pi | coil (a C bit) | New run request, set true |
| Request identifier | Pi | holding register | Increments per command, so the PLC can consume once |
| Run duration | Pi | holding register | Seconds, Pi side |
| Acknowledge | PLC | coil (a C bit) | Request accepted, permissives passed |
| Rejection code | PLC | holding register | 0 none, 1 already running, 2 duration invalid, 3 permissive fault |
| Running | PLC | coil (a C bit) | Pump output commanded on |
The coil range C1 through C2000 is read/write in the CLICK Modbus map, which makes a C bit a natural start-request bit; the matching Modbus coil number is the bit number minus one, because clients number coils from 0. That one-off is the same trap as the coil-offset case in why a Modbus TCP coil write lands one address off the map, so prove the map first: write a recognizable test value, for example 12345, into an unused holding register from the Pi and read it back in the PLC software before any real command moves.
Worked example: a 30-minute run #
Say the Pi is asked for a 1800-second pump run. The Pi writes the duration in seconds, 1800, to the duration holding register, then increments the request identifier and sets the request coil. On the PLC side, the timer preset is in time base units, so a CLICK timer with a 100 ms base needs a preset of 1800 divided by 0.1, which is 18000. The PLC logic computes that from the received 1800, so the Pi never sends time-base values.
Validate in the PLC: accept only durations from 60 to 3600 seconds. Anything below 60 returns rejection code 2 without starting; anything above 3600 does too, because a one-hour cap is the design limit for this pump and its duty. If the pump is already running, return code 1 rather than queueing, unless your process genuinely needs a queue. The arithmetic checks: 1800 s at a 0.1 s base gives 18000 counts, and 3600 s gives 36000 counts, both inside the counter range of the timer instruction.
Run one writer and a proper handshake #
Use exactly one command writer on the link. Two client instances, or a retrying script racing an operator screen, can overwrite the duration or submit conflicting requests even when both connections look healthy; parallel clients speed up reads, not writes to one command area, as the case for parallel Modbus TCP clients shows when the roles are separated.
- Configure the client with the PLC address and port 502.
- Read the acknowledge, rejection and running coils before every command.
- Write the duration, then the new request identifier, then set the request coil, in that order.
- Wait for the PLC to set acknowledge or a rejection code; poll on a read cycle, for example every 500 ms.
- On acknowledge, leave the request alone: the PLC clears it after consuming it.
- On a lost response, read status before retrying, and change the identifier if you retry so the PLC can tell the commands apart.
In pyModbusTCP, that is a write_holding_register call for the duration and identifier and a write_coil call for the request, with types fixed as an integer seconds value and booleans. In Node-RED, the Modbus write node does the same, one node per object. Neither stack needs a gateway.
Keep the pump decisions in the PLC #
When the PLC sees a new request with an identifier it has not consumed, it checks permissives, checks the duration range, copies the duration into the active timer preset, sets acknowledge and clears the request. Latching the preset means a later Pi write cannot change a run already accepted. When the timer expires or a stop condition arrives, the PLC drops the pump output and sets completion or rejection status for the Pi to read.
Decide deliberately what happens on communication loss. Letting an accepted timed run finish is a different outcome from tripping the pump when the Pi disappears; pick one from the pump's operating review and implement it in the PLC, not in the Pi. A pump output commanded on is not proof of water moving, so use motor run feedback or a flow or level signal in the run condition where the process has one. During logic testing, keep the physical outputs inhibited or the pump disconnect open until the handshake has been proven end to end.
End state #
Local Pi on the same Ethernet segment: use the CLICK's Modbus TCP server directly, one client, agreed coils and holding registers, PLC-side validation and acknowledge, no gateway and no VPN. Pi off-site across the internet: keep the same Modbus design but carry it through a restricted network path rather than exposing port 502. Either way, the pump permissives, the duration limits and the consume-once logic live in the PLC, and the Pi is only a command source with a handshake it must respect.
Questions asked
Do I need an MQTT gateway or a VPN between the Raspberry Pi and the CLICK PLC?
Not on a local link. A CLICK PLC with its Modbus TCP server enabled answers a Pi client directly over wired Ethernet on port 502, so a gateway adds a device and a failure point without adding function. A VPN only carries the traffic across a routed network; it does not define the commands. Reach for a gateway or VPN only when the Pi is off-site across the internet, and even then put the PLC behind a restricted network, never exposed directly.
Which Modbus objects does the CLICK PLC expose to the Pi?
The internal C bits, C1 through C2000, are read/write discrete objects, so they appear as coils to a Modbus client and are the right place for a start request and its acknowledge bits. Data registers appear as holding registers and carry the run duration. Confirm each object's type, address and direction in the CLICK Modbus addressing table for your CPU before the first write, because the Pi library numbers coils from 0 while CLICK documentation numbers its bits from 1.
Why should the PLC consume the start request instead of the Pi clearing it?
If the Pi clears the request bit, the PLC cannot tell a new command from a retained value after a power cycle or a lost connection, and a start level left asserted can restart the pump when the timed run ends. Let the PLC logic accept the command once, latch the duration into the running timer, set an acknowledge bit and clear the request itself. The Pi then watches the acknowledge and status bits instead of owning the request state.
What does a successful write response from the PLC actually prove?
It proves only that the Modbus TCP transaction was accepted and the value landed in the register. It does not prove the pump started, or even that the PLC logic accepted the command. The acceptance must come from a PLC-set acknowledge bit that only asserts after the permissives pass. If the Pi loses the response, read the status words before retrying; a blind retry of the same request can turn one intended run into two unless the request identifier changes.


