🛠 Server Modbus TCP : exposer les relais sur Ethernet
Objectives
Turn the Opta into a Modbus TCP slave on its integrated 10/100BASE-T Ethernet port. All variants have this port (RMII, MDIX); the ArduinoModbus library provides ModbusTCPServer. You will expose the 4 relays as coils 0..3, controllable from any supervisor (SCADA, PLC, Python script).
Hardware
- Arduino Opta (any variant) powered by 24 VDC
- RJ45 Ethernet cable to your network (DHCP or static IP)
- A test client: QModMaster, ModScan, or your BMS gateway
- Arduino IDE + "Ethernet" and "ArduinoModbus" libraries
Complete code
// opta_modbus_tcp_server.ino — coils 0..3 = les 4 relais, port 502
#include <Ethernet.h>
#include <ArduinoModbus.h>
EthernetServer modbusServer(502); // port standard Modbus TCP
ModbusTCPServer modbusTCPServer;
void setup {
Serial.begin(9600);
if (Ethernet.begin == 0) { // DHCP ; sinon Ethernet.begin(ip)
Serial.println("Echec DHCP Ethernet");
while (1);
}
Serial.print("IP Opta : "); Serial.println(Ethernet.localIP);
if (!modbusTCPServer.begin) { Serial.println("Defaillance Modbus TCP"); while (1); }
modbusTCPServer.configureCoils(0x00, 4); // coils 0..3
modbusServer.begin;
}
void loop {
EthernetClient client = modbusServer.available;
if (client) {
modbusTCPServer.poll(client);
while (modbusTCPServer.available) {
uint16_t addr = modbusTCPServer.readAddress;
uint16_t value = modbusTCPServer.readValue;
if (addr < 4) digitalWrite(RELAY1 + addr, value ? HIGH : LOW);
}
client.stop;
}
}
Expected verification
The Opta's IP address is displayed on the serial port. From the Modbus client, connect to port 502, write to coil 0: relay 1 clicks. >
Common errors
- Confusing it with Modbus RTU: RTU goes through RS-485 (RS485/WiFi variants) with ArduinoRS485, not through Ethernet; the Opta does not expose CAN on its terminals.
- No network termination: port 502 must be reachable; corporate firewalls and industrial VLANs often block it — test first on a direct network.
- Silent DHCP failure: in a workshop without DHCP, switch to a static IP via Ethernet.begin(ip).
- Not reading back the state: this server writes coils to the relays but does not synchronize reading; add reverse register logic for a true slave mode.
Other tutorials for this board
- Premier sketch : faire clignoter une LED sur Arduino Opta
- Lire une entrée numérique 24 V (capteur de proximité)
- Commander une charge 230 V avec un relais de sortie
- Lire un capteur analogique 0–10 V sur les entrées Opta
- Remonter des données machine en MQTT via Ethernet
- Exploiter le double cœur : M7 pour le PLC, M4 en coprocesseur
Code examples for this board
relais_securise.inoArduinoentrees_numeriques.inoArduinoanalogique_0_10v.inoArduinomodbus_tcp_esclave.inoArduinomodbus_rtu_maitre.inoArduinomqtt_telemetrie.inoArduino