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:25] – [Utiliser le capteur DHT11 avec ESP32 et mqtt broker] 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 57: Ligne 59:
  
 #include <WiFi.h> #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/temperatur#include <WiFi.h> 
  
 #include <PubSubClient.h> #include <PubSubClient.h>
Ligne 417: Ligne 241:
  
   //delay(2000);   //delay(2000);
- 
 </code> </code>
/home/chanteri/www/fablab37110/data/attic/start/arduino/mqtt/esp32.1702362324.txt.gz · Dernière modification : 2023/12/12 07:25 de gerardadmin