🛠 Publier et s'abonner en MQTT

Arduino GIGA R1 WiFi · intermediaire · Schéma : IMG-giga-r1-wifi-intermediaire-5-mqtt-pub-sub.svg — GIGA R1 WiFi publiant des messages vers un courtier MQTT et s'abonnant à un autre sujet, flèches aller/retour entre la carte, le broker et un autre client

Objectives

Step up to IoT messaging: connect the GIGA R1 WiFi to WiFi, then use the MQTT protocol with the PubSubClient library to publish a value every 5 seconds and receive messages from another client. MQTT is the native protocol of Arduino IoT Cloud, which the board officially supports — this tutorial gives you the basics to understand what happens "under" the cloud.

Hardware

Complete Code

// GIGA R1 WiFi — MQTT publish/subscribe (MQTT brut port 1883, sans TLS)
#include <WiFi.h>
#include <PubSubClient.h>

const char SSID[] = "your-ssid";
const char PASS[] = "your-pass";
const char BROKER[] = "test.mosquitto.org";
const int  PORT_BROKER = 1883;
const char SUJET_PUB[]  = "giga/maison/salle/capteur";
const char SUJET_SUB[]  = "giga/maison/salle/consigne";

WiFiClient reseau;
PubSubClient mqtt(reseau);
unsigned long dernierEnvoi = 0;

void callback(char* sujet, byte* payload, unsigned int longueur) {
  Serial.print("Recu sur "); Serial.print(sujet); Serial.print(" : ");
  for (unsigned int i = 0; i < longueur; i++) Serial.write(payload[i]);
  Serial.println;
  digitalWrite(LED_BUILTIN, mqtt.payload[0] == '1' ? HIGH : LOW);
}

void setup {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  WiFi.begin(SSID, PASS);
  while (WiFi.status != WL_CONNECTED) { Serial.print("."); delay(200); }
  Serial.print("IP "); Serial.println(WiFi.localIP);

  mqtt.setServer(BROKER, PORT_BROKER);
  mqtt.setCallback(callback);
  while (!mqtt.connected) {
    Serial.print("Connexion MQTT...");
    if (mqtt.connect("giga-r1")) {                 // client id "giga-r1"
      Serial.println("connecte");
      mqtt.subscribe(SUJET_SUB);
      Serial.print("abonne a "); Serial.println(SUJET_SUB);
    } else {
      Serial.print("echec rc="); Serial.println(mqtt.state);
      delay(2000);
    }
  }
}

void loop {
  mqtt.loop;                                     // indispensable : keepalive + reception
  if (WiFi.status != WL_CONNECTED) { mqtt.disconnect; WiFi.reconnect; return; }
  if (!mqtt.connected) { mqtt.connect("giga-r1"); mqtt.subscribe(SUJET_SUB); return; }
  if (millis - dernierEnvoi > 5000) {
    dernierEnvoi = millis;
    String v = String(analogRead(A0));             // 12 entrees analogiques 3.3 V
    mqtt.publish(SUJET_PUB, v.c_str);
    Serial.print("publie "); Serial.println(v);
  }
}

Expected Verification

The monitor displays the WiFi connection followed by "connected / subscribed to ...", then "published <value>" every 5 seconds. By publishing from another tool (mosquitto_pub -t giga/maison/salle/consigne -m 1), the board's LED turns on and the message appears in the console. On test.mosquitto.org, you can also subscribe to the published topic from any client.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials