🛠 Nœud MQTT : télémétrie IMU et commandes à distance

M5StickC Plus2 (M5Stack) — ESP32-PICO-V3-02 "stick" wearable kit (SKU K016-P2) · avance · Schéma : IMG-m5stack-stick-c-plus-2-avance-3-m5stick-c2-mqtt-dashboard.svg — M5StickC Plus2 publiant ses données IMU vers un broker MQTT, un téléphone et un ordinateur abonnés aux topics, et recevant un ordre de bipeur en retour.

Objectives

Turn the M5StickC Plus2 into a complete IoT node: periodic publication of telemetry (acceleration, battery voltage via the G38 detection pin) to MQTT topics, and subscription to a command topic to trigger the buzzer remotely. Using the PubSubClient library on 2.4 GHz Wi-Fi.

Hardware

Complete code

// Noeud MQTT : publie accel + batterie, recoit des ordres de bip
#include <M5Unified.h>
#include <WiFi.h>
#include <PubSubClient.h>

const char* SSID = "VOTRE_WIFI_24GHZ";
const char* PASS = "motdepasse";
const char* BROKER = "test.mosquitto.org";   // port 1883
const char* BASE = "stickc2/atelier/";
WiFiClient wifiNet;
PubSubClient mqtt(wifiNet);
uint32_t dernierEnvoi = 0;

void callback(char* topic, byte* payload, unsigned int len) {
  String msg((char*)payload, len);
  M5.Display.setCursor(0, 90);
  M5.Display.printf("<-%s", msg.c_str);
  int freq = msg.toInt;
  if (freq > 40 && freq < 8000) M5.Speaker.tone(freq, 400);  // buzzer G2
}

void setup {
  auto cfg = M5.config;
  M5.begin(cfg);
  M5.Display.setRotation(1);
  M5.Display.setTextSize(1);
  WiFi.mode(WIFI_STA);
  WiFi.begin(SSID, PASS);
  while (WiFi.waitForConnectResult != WL_CONNECTED) delay(2000);
  mqtt.setServer(BROKER, 1883);
  mqtt.setCallback(callback);
  while (!mqtt.connect("stickc2plus2")) delay(1000);
  String cmd = String(BASE) + "cmd";
  mqtt.subscribe(cmd.c_str);
}

void loop {
  M5.update;
  mqtt.loop;
  if (!mqtt.connected) {            // reconnexion + re-abonnement
    if (mqtt.connect("stickc2plus2")) {
      String cmd = String(BASE) + "cmd";
      mqtt.subscribe(cmd.c_str);
    }
  }
  if (millis - dernierEnvoi > 2000) {
    dernierEnvoi = millis;
    float ax, ay, az;
    M5.Imu.getAccel(&ax, &ay, &az);
    String t = String(BASE) + "accel";
    char json[96];
    snprintf(json, sizeof json, "{\"ax\":%.2f,\"ay\":%.2f,\"az\":%.2f}", ax, ay, az);
    mqtt.publish(t.c_str, json);
    // Tension batterie : broche de detection G38 (ADC1) — la conversion
    // brute -> pourcentage n'est pas documentee :
    // > 
    uint32_t batt = mqtt.publish((String(BASE) + "batt").c_str,
                                 String(analogRead(38)).c_str);
    (void)batt;
    M5.Display.setCursor(0, 20);
    M5.Display.printf("pub accel OK  RSSI %d\n", WiFi.RSSI);
  }
  vTaskDelay(20 / portTICK_PERIOD_MS);
}

Expected verification

With mosquitto_sub -h test.mosquitto.org -t 'stickc2/atelier/#', you will see the acceleration JSON and the raw battery value arrive every 2 seconds. Publishing 880 to stickc2/atelier/cmd makes the buzzer sound for 400 ms and displays the command on the screen.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials