🛠 Remonter des données machine en MQTT via Ethernet

Arduino Opta (micro PLC, STM32H747) · intermediaire · Schéma : IMG-opta-plc-intermediaire-3-opta-mqtt-industriel.svg — Opta publiant sur un broker MQTT les états de ses entrées vers un serveur de supervision et un dashboard dans le cloud via le réseau 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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials