ESP32 + OpenPLC + Modbus TCP

🚀 ESP32 + OpenPLC + Modbus TCP: From Setup to Successful Connection

This guide walks through everything I did to get an ESP32 board running as a Modbus TCP slave that can communicate with OpenPLC Runtime. I’m keeping it as a personal log/blog, so if I or anyone else ever gets stuck, this document can serve as a quick reference.


1. Installing Required Software

PlatformIO (ESP32 Development)

PlatformIO makes flashing firmware on the ESP32 much easier.

pip install platformio

Or use the PlatformIO IDE extension in VS Code.

OpenPLC Runtime & Editor

Python (Modbus Client Test)

We’ll use Python’s pymodbus library to test the ESP32 Modbus server.

pip install pymodbus

2. Flashing ESP32 with Modbus TCP Slave Code

Create a new PlatformIO project:

Inside src/main.cpp, flash this code (adapt WiFi credentials):

Upload the firmware:

If successful, ESP32 should print:


3. Testing ESP32 Modbus TCP with Python

Create modbus_test.py:

Run it:

Expected output:


4. Connecting ESP32 to OpenPLC

  1. Open the OpenPLC Web UI → Slave Devices → Add New Device.

  2. Select Modbus TCP/IP Slave.

  3. Enter ESP32’s IP (192.168.0.117) and port (1502).

  4. Map registers:

    • Holding Registers (%QW100): Start 0, Size 1 → maps HR[0]

    • Coils (%QX100.0): Start 0, Size 1 → maps Coil[0] (LED)

Run your PLC program, and OpenPLC will talk to ESP32 directly.


✅ Final Result

  • ESP32 serves as a Modbus TCP slave (HR[0] increments, Coil[0] controls LED).

  • Python client can read/write registers and coils.

  • OpenPLC Runtime connects and uses ESP32 as an I/O device.

👉 With this setup, ESP32 becomes a fully usable field device in your industrial automation project.


📝 Notes & Troubleshooting

  • If OpenPLC shows Connection Refused, check:

    • ESP32 is on the same network.

    • Correct IP and port are used.

    • Port 502 requires root privileges; 1502 is safer.

  • Use pymodbus to test before integrating with OpenPLC.


That’s it 🎉 — ESP32 is now integrated with OpenPLC over Modbus TCP!

Last updated

Was this helpful?