Outils pour utilisateurs

Outils du site


start:arduino:mqtt:esp32

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
start:arduino:mqtt:esp32 [2023/12/12 07:14] gerardadminstart:arduino:mqtt:esp32 [2023/12/12 07:40] (Version actuelle) – [MQTT et ESP32] gerardadmin
Ligne 2: Ligne 2:
  
 [[https://letmeknow.fr/fr/blog/180-tuto-mqtt-sur-esp32--mosquitto|tuto-mqtt-sur-esp32--mosquitto]] [[https://letmeknow.fr/fr/blog/180-tuto-mqtt-sur-esp32--mosquitto|tuto-mqtt-sur-esp32--mosquitto]]
 +
 +[[https://randomnerdtutorials-com.translate.goog/esp32-mqtt-publish-subscribe-arduino-ide/?_x_tr_sl=auto&_x_tr_tl=fr&_x_tr_hl=fr|ESP32 MQTT – Publier et s'abonner avec l'IDE Arduino]]
  
  
Ligne 51: Ligne 53:
  
 Lancez cette application et configurez une connexion avec MQTT broker comme suite:cliquez sur « connexions » et, dans la fenêtre suivante, saisissez les détails de la connexion à partir du compte CloudMQTT. Lancez cette application et configurez une connexion avec MQTT broker comme suite:cliquez sur « connexions » et, dans la fenêtre suivante, saisissez les détails de la connexion à partir du compte CloudMQTT.
 +
 +
 +
 +<code c Programme_de_simulation.ino>
 +
 +#include <WiFi.h>
 +
 +#include <PubSubClient.h>
 +
 +#include "DHT.h"
 +
 +const char* ssid = "xxxxxxxxxxxx";
 +
 +const char* password =  "xxxxxxxx";
 +
 +const char* mqttServer = "xxxxxxxxxxxxxxxxx";
 +
 +const int mqttPort = xxxxxxxx;
 +
 +const char* mqttUser = "xxxxxxx";
 +
 +const char* mqttPassword = "xxxxxxxxx";
 +
 +#define DHTPIN D2  
 +
 +#define DHTTYPE DHT11  
 +
 +DHT dht(DHTPIN, DHTTYPE);
 +
 +//#define humidity_topic "sensor/humidity"
 +
 +//#define humidity_topic "sensor/humidity"   
 +
 +WiFiClient espClient;
 +
 +PubSubClient client(espClient);
 +
 +long lastMsg = 0;
 +
 +char msg[50];
 +
 +int value = 0;
 +
 +void setup() {
 +
 +    Serial.begin(115200);
 +    
 +    dht.begin();
 +    
 +    pinMode(hsensor1,INPUT);
 +    
 +    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);
 +    
 +          }
 +    
 +    }
 +    
 +}
 +
 +void reconnect() {
 +
 +  // Loop until we're reconnected
 +
 +  while (!client.connected()) {
 +
 +    Serial.print("Attempting MQTT connection...");
 +
 +    // Attempt to connect
 +
 +    if (client.connect("ESP32Client")) {
 +
 +      Serial.println("connected");
 +
 +      // Subscribe
 +
 +      client.subscribe("esp32/output");
 +
 +    } 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();
 +
 +  }
 +
 +  client.loop();
 +  long now = millis();
 +
 +  // Send a message every minute
 +
 +  if (now - lastMsg > 100) {
 +
 +    lastMsg = now;
 +
 +   // Lire l’entrée numérique dans une valeur boolienne 
 +
 +  float humidite = dht.readHumidity();
 +
 +  float temperature = dht.readTemperature();
 +
 +    return;
 +
 +  }
 +
 +  // Les conditions d’humidité 
 +
 +  if(temperature<=30){
 +
 +   // Pompe  marche 
 +
 +   Serial.print("la temperature est bas");
 +
 +   client.publish("information", String("la temperature est bas").c_str(), true);}
 +
 +    else {
 +
 +    Serial.print("la température est élevée");
 +
 +    client.publish("information", String("la température est élevée").c_str(), true);
 +   delay(500);
 +
 +       }
 +
 +   Serial.print("Humidité:");
 +
 +   Serial.print(humidite);
 +
 +   Serial.println("%") ;
 +
 +   Serial.print(temperature);
 +
 +   Serial.println("°C") ;
 +
 +   client.publish("esp32/Humidité", String(Humidité).c_str(), true);
 +
 +   client.publish("esp32/temperature", String(temperature).c_str(), true);
 +
 +  }
 +
 +  //delay(2000);
 +</code>
/home/chanteri/www/fablab37110/data/attic/start/arduino/mqtt/esp32.1702361642.txt.gz · Dernière modification : 2023/12/12 07:14 de gerardadmin