🛠 Remonter des données machine en MQTT via Ethernet
Objectives
Connect the Opta to an MQTT broker to report machine status to a supervisor (Node-RED, Mosquitto, Tasmota, cloud). The embedded 10/100 Ethernet ensures a reliable link in the workshop; the MQTT protocol, lightweight and publish/subscribe, is the IIoT standard. You will publish digital inputs every second to a hierarchical topic.
Hardware
- Arduino Opta powered by 24 VDC, Ethernet cable
- An accessible MQTT broker (Mosquitto on a Raspberry Pi or your chosen server)
- A test MQTT client (MQTT Explorer, mosquitto_sub)
- Arduino IDE + "Ethernet" and "PubSubClient" libraries
Complete code
// opta_mqtt.ino — publication des entrees I1..I4 en MQTT sur Ethernet
#include <Ethernet.h>
#include <PubSubClient.h>
EthernetClient ethClient;
PubSubClient mqtt(ethClient);
const char* BROKER = "192.168.1.50"; // IP de votre broker Mosquitto
const int ENTREES[] = {A0, A1, A2, A3}; // bornes I1..I4
char topic[40], payload[8];
void setup {
Serial.begin(9600);
if (Ethernet.begin == 0) { Serial.println("Echec DHCP"); while (1); }
Serial.println(Ethernet.localIP);
mqtt.setServer(BROKER, 1883);
for (int i = 0; i < 4; i++) pinMode(ENTREES[i], INPUT);
}
void boucleMqtt {
while (!mqtt.connected) {
if (mqtt.connect("opta-atelier")) { Serial.println("MQTT connecte"); return; }
delay(2000); // nouvelle tentative toutes les 2 s
}
}
void loop {
boucleMqtt;
for (int i = 0; i < 4; i++) {
snprintf(topic, sizeof(topic), "usine/opta1/E%d", i + 1);
snprintf(payload, sizeof(payload), "%d", digitalRead(ENTREES[i]) ? 1 : 0);
mqtt.publish(topic, payload, true); // retain = true
}
mqtt.loop;
delay(1000);
}
Expected verification
With mosquitto_sub -t 'usine/opta1/#', messages usine/opta1/E1 = 0/1 scroll every second and follow your 24 V inputs. The retain flag allows the supervisor to know the last state as soon as it connects.
Common errors
- Broker unreachable: check firewall and port 1883; the serial port displays the IP, test ping before blaming the sketch.
- Forgetting
mqtt.loop: without it, keep-alive messages are not sent and the broker disconnects the Opta every minute. - Flooding the network: publishing on every loop iteration without timing saturates the broker; pace it (1 Hz is sufficient for states).
- MQTT without TLS in production: port 1883 is unencrypted; for a corporate network, document segmentation or use Arduino Cloud / AWS IoT, for which official Opta tutorials exist.
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
- Server Modbus TCP : exposer les relais sur Ethernet
- Lire un capteur analogique 0–10 V sur les entrées Opta
- 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