Subscription MQTT et stockage variable

Bonjour,

J'essaye de stocker des valeurs depuis un topic mqtt vers des variables et je n'y arrive pas.

Dans le code ci dessous les variables temperature et humidite ont la même valeur.

Les valeurs des 2 topics sont bien différentes.

Quelqu'un m'a bien aidé déjà pour en arrivé là sur le forum anglo saxon mais j'ai l'impression que tout devient très complexe et mes connaissances sont assez limités. Alors quand on me parle de fichiers json c'est clairement hors de porté pour moi. Subscribe and store value into a variable. MQTT

Est ce que vous avez une idée / astuce pour le stockage des données de topic mqtt vers de variable de manière assez simple ?

Mon but final étant d'afficher les valeurs (Température / Humidité / Pression) sur un petit OLED et et le tout sur la même page.

Merci.

#include <WiFi.h>
#include <PubSubClient.h>

const char* ssid = "****";
const char* password =  "****";
const char* mqttServer = "192.168.1.65";
const int mqttPort = 1883;
const char* mqttUser = "hatchepsout";
const char* mqttPassword = "*****";
const char* mqttTopicTemperature = "hatchepsout/feeds/temperature";
const char* mqttTopicHumidite = "hatchepsout/feeds/humidite";


volatile  bool TopicArrived = false;
const     int mqttpayloadSize = 100;
char mqttpayload [mqttpayloadSize] = {'\0'};
String mqtttopic;

char* temperature ; 
char* humidite ; 

WiFiClient espClient;
PubSubClient client(espClient);

void callback(char* topic, byte* payload, unsigned int length)
{
  if ( !TopicArrived )
  {
    memset( mqttpayload, '\0', mqttpayloadSize ); // clear payload char buffer
    mqtttopic = ""; //clear topic string buffer
    mqtttopic = topic; //store new topic
    memcpy( mqttpayload, payload, length );
    TopicArrived = true;
  }
}

void setup()
{
  mqtttopic.reserve(100);
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }

  Serial.println("Connected to the WiFi network");
  client.setServer(mqttServer, mqttPort);
  client.setCallback(callback);

  while (!client.connected())
  {
    Serial.println("Connecting to MQTT...");

    if (client.connect("ESP32Client", mqttUser, mqttPassword ))
    {
      Serial.println("connected");
    }

    else
    {
      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000);
    }
  }
  client.subscribe( mqttTopicTemperature );
  client.subscribe( mqttTopicHumidite );

}

void loop()
{

  client.loop();

  if ( TopicArrived )
  {
      if ( mqtttopic == "hatchepsout/feeds/humidite" )
      {
        humidite = mqttpayload ; 
      }
      if ( mqtttopic == "hatchepsout/feeds/temperature" )
      {
        temperature = mqttpayload ; 
      }
    TopicArrived = false;
  }
  
  Serial.print("Temperature is : ");
  Serial.println(temperature);
  Serial.print("Humidite is : ");
  Serial.println(humidite);
  
}

Bonsoir

ici un exemple pour ESP32 + librairie PubSub dans lequel on voit le principe
-souscription à un topic nommé ESP32.output
-récupération ('stockage') du contenu de ce topic dans une variable nommée messageTemp (String) puis un changement d'état d'une LED selon le contenu du message reçu.

https://randomnerdtutorials.com/esp32-mqtt-publish-subscribe-arduino-ide/

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.