🛠 Publier et s'abonner en MQTT
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
- Arduino GIGA R1 WiFi board + u.FL antenna
- WiFi 2.4 GHz WPA2
- PubSubClient library (Michael Schwartzkopff / Nick O'Leary) installed via the IDE Library Manager
- An accessible MQTT broker: the public test broker
test.mosquitto.orgon port 1883, or your own local Mosquitto
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
mqtt.loopforgotten inloop: without regular calls, the keepalive expires and the broker disconnects the board every 60 s; received messages are never distributed.- Port 8883 used without TLS: mosquitto's port 8883 uses TLS; with a simple
WiFiClient, use port 1883 (see the TLS tutorial for port 8883). - Overloaded public broker:
test.mosquitto.orgis shared and sometimes slow; for a serious project, set up Mosquitto locally. - Non-ASCII or too long topics: keep identifiers simple (letters, numbers, /); accents in topics break some tools.
- Poorly handled reconnection: after a WiFi drop, you must reconnect MQTT AND resubscribe — the subscription does not survive reconnection.
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton.inoArduinorespiration.inoArduinoanalogique.inoArduinodac-onde.inoArduinooled-i2c.inoArduino