Apprendre à coder un Esp8266 pour communiquer via MQTT

Bonjour,

Je suis en train de développer un petit soft pour piloter mon poêle, j’ai bien avancé sur la partie interface ESP8266 <=> poêle, maintenant j’aimerai pouvoir le laisser en standalone sur le poêle. Et pour faire communiquer mon D1 mini vers HA? je voudrais implémenter le protocole MQTT.
Est-ce que vous auriez des tutos / explications à me donner pour m’indiquer comment coder ceci dans le fichier .ino ? Sachant qu’il faut que mon D1 mini doit recevoir et publier des topics.
J’ai lu différents tutos, mais il y a souvent des choses assez différentes entre les uns et les autres, donc je ne suis pas certain de la bonne marche à suivre.
Aussi, vu que pour piloter le poêle, la communication se fait par le port série, je dois retirer tous les « serial.print », donc je me retrouve sans debug possible pour l’implémentation du protocole MQTT.

ca me fait pensé un peu a ce projet ce que tu recherches : Domotiser son poêle à granulés avec un ESP8266 et MQTT - SLA99

Merci pour le lien, même si son pilotage en contact sec (simple tout ou rien) n’est pas ce que j’ai fait, il y a un lien vers le Github de PubSubClient, avec des exemples (je n’avais pas pensé à chercher la librairie… :confused:)

Un autre exemple avec du mqtt in et du mqtt out…
C’est avec de l’ethernet filaire. dans les déclarations au début sont à changer
Là, l’interface c’est un clavier à code (wiegand) et un détecture de mouvement mais, la logique de la partie pub/sub est la même:

#include <SPI.h>
#include <Ethernet.h>
#include <Wiegand.h> 

#define SERRURE 4 /* PIN sur lequel est connecté le relai */
#define LUMIERE 9 /* PIN sur lequel la lumière du clavier est connectée */
#define pirPIN 6

int pirState = LOW;
int pirVal = LOW;

String wgPINCODE ="";

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x0E, 0xBE, 0xFE, 0x32, 0x12, 0x99 };

#include <PubSubClient.h>
long stayon = 500;

void callback(char* topic, byte* payload, unsigned int length);

const char* mqtt_server = "192.168.x.t";
const char* mqtt_input = "porte/input";
const char* mqtt_output = "porte/output";
IPAddress ipEth(192, 168, x, y);
IPAddress ipDNS(192, 168, x, z);
IPAddress subnet(255, 255, 255, 0);

void callback(char* topic, byte* payload, unsigned int length);

WIEGAND wg;
EthernetClient ethClient;
PubSubClient client(mqtt_server, 1883, callback, ethClient);
long lastMsg = 0;

char message_buff[100];

void setup() {
  Serial.begin(115200);
  setup_eth();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  wg.begin(2,3); /* vert et blanc sur sont 2 et 3 */
  pinMode(SERRURE, OUTPUT);
  pinMode(LUMIERE, OUTPUT);
  pinMode(pirPIN, INPUT);
  digitalWrite(SERRURE, LOW);   
  digitalWrite(LUMIERE, HIGH);  
}

void setup_eth() {

/*  
   if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
  }
*/
  Ethernet.begin(mac, ipEth, ipDNS);

  Serial.println(Ethernet.localIP());
  
}

void callback(char* topic, byte* payload, unsigned int length) {
  payload[length] = '\0';
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
  String message((char*)payload);

  if (message == "open") {
    digitalWrite(SERRURE, HIGH);   
    digitalWrite(LUMIERE, LOW);   
    delay(stayon);
    digitalWrite(SERRURE, LOW);   
    digitalWrite(LUMIERE, HIGH);   
  } 
  if (message == "confirm") {
    digitalWrite(LUMIERE, LOW);   
    delay(stayon);
    digitalWrite(LUMIERE, HIGH);   
  }
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("GarageWiegand")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.subscribe(mqtt_input);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
void loop() {
  if (!client.connected()) {
    reconnect();
  }

  pirVal = digitalRead(pirPIN);

  
  if (pirVal == HIGH) { // check if the input is HIGH
 //         Serial.println("pirVal  High!");

    delay(150);
    if (pirState == LOW) {
      Serial.println("Motion detected!");
      client.publish(mqtt_output,"moveon");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  }
  else {
 //             Serial.println("pirVal  low!");

    if (pirState == HIGH) {
        Serial.println("Motion stopped!");
        client.publish(mqtt_output,"moveoff");
        pirState = LOW;
    }
  }
  
  if(wg.available()) {
    if ( wg.getWiegandType() == 8 || wg.getWiegandType() == 4 )
       {
          unsigned long wgCODE = wg.getCode();
          if  (wgCODE == 13 )
            {
              char wgCodeChar[16];
//              sprintf(wgCodeChar, "%s", wgPINCODE);
              wgPINCODE.toCharArray(wgCodeChar, 16);
              client.publish(mqtt_output,wgCodeChar);
              Serial.println(wgPINCODE);
              Serial.println(wgCodeChar);
              wgPINCODE = "";
            }
          else if ( wgCODE == 27 )
            {
              wgPINCODE = "";
            }
          else
          {
            wgPINCODE+= String(wgCODE);
            if (wgPINCODE.length() > 15 )
              wgPINCODE = "";
          }
       }
       else
       {
          unsigned long wgCode = wg.getCode();
          char wgCodeChar[16];
          sprintf(wgCodeChar,"%lu",wgCode); 
          Serial.println(wgCodeChar); 
          Serial.println(wg.getCode());
          client.publish(mqtt_output,wgCodeChar);
       }
  }
  client.loop();
}

Peut être dur la communauté arduino ? Et aussi sur la chaine YouTube de Domotic diy