PLC Solution Library · PLC gate control simulator
Turn database rows into gate movements, and keep a known state when the network drops
Your database already knows which gate should open. In this simulator, two ESP32 PLC 42 units turn those rows into the movement of 18 pneumatic gates through an example valve island, and 9 pilot lamps show the state on site.
Write a command, cut the network or send a burst of rows. Watch what your PLCs do, and what they write back to your table. You see every step.
Choose your scenario
INSERT INTO gate_commands (gate_id, command) VALUES ('G01', 'OPEN');
PLC A · gates G01 to G09
PLC B · gates G10 to G18
Click a gate to select it. Double click a gate to send its command at once.
Write a command
INSERT INTO gate_commands (gate_id, command) VALUES ('G01', 'OPEN');
Simulation controls
These three settings are the design choices you make in your own project, explained below. Here, your simulated gate travel time is 4 s and your PLC watchdog is 5 s.
Table gate_commands
| id | gate_id | command | created_at | status | updated_by |
|---|
PENDING: written by your database client. SENT: your PLC has read the row. DONE: your PLC confirms the gate reached its position. SKIPPED: your PLC did not run the row, and says why.
What happens to the gates when the database link drops?
The gates go to the state that your PLC program defines for a lost link. Your PLC cannot know by itself that the database is gone, so you give it a watchdog. If your PLC has no contact with your table for a set time (5 s in this simulator), it moves its gates to the default state you chose, for example hold every gate or close every gate, and switches on an alarm lamp.
During the outage, your database still accepts new rows. They stay PENDING, so you lose no command. Your PLC keeps its own confirmations (DONE, SKIPPED) in a queue. When the link is back, it writes those confirmations to your table and asks again for every open row before it accepts new commands.
This default state is a process default set in your PLC program. Protection of people near moving gates (emergency stop, pressure dump valve, guards) needs a safety-rated circuit, separate from this logic.
Run the Network drop scenario and try both default states. Then compare what you see in your table.
The architecture in short
Your database stores each command as a row with a status. Your PLC gets the new rows over the network. You choose how: your PLC asks for new rows at a fixed interval, for example through a small API in front of your table, or a small service reads your table and passes the commands on over MQTT or Modbus TCP. Your PLC switches the valve output for that gate and writes the result back to the same row, so your table becomes the log of every movement.
A real deployment
Real case
A customer controls 18 pneumatic gates and 9 signal pilots with 2 ESP32 PLC 42 units. The open and close commands come over the network from a MySQL database. The customer's own team programmed and commissioned the system with the Industrial Shields documentation and the MQTT and Modbus courses on this website. The system works without problems.
Design choices you make
1. Default state when the link is lost
Decide, for each gate, which state your process needs when your PLC stops hearing from your database: hold the last position, close, or open. Use a watchdog: if your PLC has no contact for a set time, it goes to that state and switches on an alarm lamp. Rows written during the outage stay PENDING in your table, so nothing is lost.
This default state is a process default set in the PLC program. Protection of people near moving gates (emergency stop, pressure dump valve, guards) needs a safety-rated circuit, separate from this logic.
Loss of network and loss of power are different cases. A single solenoid valve with spring return goes back to its rest position when the coil has no power. A double solenoid valve keeps its last switched position when both coils are off, so the cylinder completes or stays at that stroke. To stop a cylinder mid stroke, you need a 5/3 valve with closed centre. You choose the valve type with the power loss case in mind, and you handle the network loss case in your PLC program.
2. Polling or push
With polling, your interval sets the worst delay between a new row and the movement, and the load on your database. With push over MQTT, your delay is shorter, but you need a broker and a plan for messages sent while your PLC was offline. In both cases, after a reconnection your PLC should ask again for every open row. Your table is the reference, not the last message.
3. Command confirmation
You need at least three states: written, received and done. In the simulator, you see DONE after the gate travel time. In your system, confirm the position with a limit switch or a position sensor on the cylinder. A timer only proves to you that the valve got the signal. Add a timeout state, for example FAULT, for a gate that does not reach its position in time.
4. Two commands for the same gate
You decide if the newest row wins or if rows run in order. Use the row id from your database to decide the order, not the clock of the client that wrote it. Write SKIPPED with a reason for every row you do not run, so your table always tells the truth. Check with your mechanical designer if a cylinder may reverse while it moves. Store the last row id each of your PLCs has processed, so a row read twice never moves a gate twice.
Learn the protocols
These tutorials and examples cover the communication parts you need for this pattern on the ESP32 PLC.
- MQTT remote commands with a response topic (PLC Solution Library)
- Building an MQTT over TLS connection: a guide for ESP32 PLC
- Modbus TCP with Arduino and ESP32 based PLCs
- Modbus RTU with Arduino and ESP32 based PLCs
- Modbus RTU master over RS-485 (PLC Solution Library)
- RS-485 with Arduino and ESP32 based PLCs
- How to use SQLite with ESP32 PLC
- MSSQL library for Arduino and ESP32 industrial PLC
More examples: PLC Solution Library · all simulators.
ESP32 PLC 42: the specs that matter here
| Outputs | 16: 10 digital optoisolated and 6 analog/digital/PWM (0-10 V, 12-bit), 5 to 24 Vdc. No relay outputs. |
|---|---|
| Inputs | 26: 14 digital optoisolated and 12 analog/digital (0-10 V, 11-bit), 5 to 24 Vdc |
| Network | Ethernet 10/100 Mbit (RJ45), Wi-Fi 2.4 GHz, Bluetooth LE |
| Serial | RS-485 half duplex, RS-232 and TTL, I2C, SPI |
| Power supply | 12 to 24 Vdc with polarity protection |
| Mounting | DIN rail TS35, IP20, inside a cabinet |
| Programming | Arduino IDE with the Industrial Shields ESP32 boards package, over USB or Ethernet |
| Expansion | Up to 127 modules over I2C. One optional communication board: 4G, LoRa or CAN |
Count your valve coils and pilot lamps before you choose the wiring. As an example: with direct wiring or interface relays, each coil needs one PLC output, so a single solenoid valve needs one output and a double solenoid valve needs two. With a bus interface on a valve island, the PLC writes the coil states over the network, so check that the island supports a protocol your PLC can use, for example Modbus TCP. Then check your electrical ratings in the ESP32 PLC family datasheet. Full specs: ESP32 PLC 42 · ESP32 PLC family.
FAQ
What happens to the PLC outputs when the network is lost?
The outputs do what your program tells them to do. Your PLC does not know by itself that your database is gone. You add a watchdog: if there is no contact for a set time, your PLC moves every gate to a default state that you define (hold, close or open) and switches on an alarm lamp. When the link is back, it asks again for every open row before it accepts new commands. This default state is a process default set in the PLC program. Protection of people near moving gates (emergency stop, pressure dump valve, guards) needs a safety-rated circuit, separate from this logic.
What do the PENDING, SENT, DONE and SKIPPED states mean?
PENDING means the row is in your table and no PLC has read it yet. SENT means your PLC has read the row. DONE means your PLC confirms that the gate reached its position. SKIPPED means your PLC did not run the row, and the row says why, for example a newer row for the same gate. With these four states, your table shows what really happened to every command.
What does the poll interval change?
The poll interval is how often your PLC asks for new rows. It sets the worst delay between a new row and the start of the movement, and the number of queries your database receives. In the simulator you can choose 1, 2 or 5 s and compare. If you need a shorter delay, a service can push your commands to your PLC, for example over MQTT.
How are the valve coils connected to the PLC?
It depends on the wiring you choose. With direct wiring or interface relays, each coil needs one PLC output. With a bus interface on a valve island (one example of wiring), your PLC writes the coil states over the network, so check that your island supports a protocol your PLC can use, for example Modbus TCP. For direct wiring, check your coil voltage and current against the output ratings in the datasheet.
Which gate and valve layout does the simulator use?
You test an example layout. PLC A drives gates G01 to G09 and PLC B drives G10 to G18, through an example valve island with one valve per gate. Each valve is a single solenoid valve with spring return, so when your PLC switches the output ON, the gate opens. The 9 pilot lamps show example functions that you can map to your own panel, such as network OK, link alarm and rows pending. In the simulator, your gate travel time is 4 s and your watchdog is 5 s. The real deployment uses 18 pneumatic gates, 9 signal pilots and 2 ESP32 PLC 42 units, but we do not publish its split, valves or wiring.
Do I need to be a programmer to build this?
You write your PLC program in C++ with the Arduino IDE. For the communication parts (MQTT, Modbus TCP, Modbus RTU), you have public tutorials and examples, linked above. In the real deployment on this page, the customer's own team programmed and commissioned the system with that documentation and those courses. If you prefer, an integrator can write the program for you from your table design.
Send me this architecture
Tell us about your gates, your signals and your database. Our team replies with a proposed architecture for your case. Your simulator settings go with the request.