start:arduino:mqtt:esp32
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine 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] gerardadmin | start:arduino:mqtt:esp32 [2023/12/12 07:40] (Version actuelle) – [MQTT et ESP32] gerardadmin | ||
---|---|---|---|
Ligne 2: | Ligne 2: | ||
[[https:// | [[https:// | ||
+ | |||
+ | [[https:// | ||
Ligne 57: | Ligne 59: | ||
#include < | #include < | ||
- | |||
- | #include < | ||
- | |||
- | #include " | ||
- | |||
- | const char* ssid = " | ||
- | |||
- | const char* password = " | ||
- | |||
- | const char* mqttServer = " | ||
- | |||
- | const int mqttPort = xxxxxxxx; | ||
- | |||
- | const char* mqttUser = " | ||
- | |||
- | const char* mqttPassword = " | ||
- | |||
- | #define DHTPIN D2 | ||
- | |||
- | #define DHTTYPE DHT11 | ||
- | |||
- | DHT dht(DHTPIN, DHTTYPE); | ||
- | |||
- | //#define humidity_topic " | ||
- | |||
- | //#define humidity_topic " | ||
- | |||
- | WiFiClient espClient; | ||
- | |||
- | PubSubClient client(espClient); | ||
- | |||
- | long lastMsg = 0; | ||
- | |||
- | char msg[50]; | ||
- | |||
- | int value = 0; | ||
- | |||
- | void setup() { | ||
- | |||
- | Serial.begin(115200); | ||
- | |||
- | dht.begin(); | ||
- | |||
- | pinMode(hsensor1, | ||
- | |||
- | WiFi.begin(ssid, | ||
- | |||
- | while (WiFi.status() != WL_CONNECTED) { | ||
- | |||
- | delay(500); | ||
- | |||
- | Serial.println(" | ||
- | |||
- | } | ||
- | |||
- | Serial.println(" | ||
- | |||
- | client.setServer(mqttServer, | ||
- | |||
- | // | ||
- | |||
- | while (!client.connected()) { | ||
- | |||
- | Serial.println(" | ||
- | |||
- | if (client.connect(" | ||
- | |||
- | Serial.println(" | ||
- | |||
- | } else { | ||
- | |||
- | Serial.print(" | ||
- | |||
- | Serial.print(client.state()); | ||
- | |||
- | delay(2000); | ||
- | |||
- | } | ||
- | |||
- | } | ||
- | |||
- | } | ||
- | |||
- | void reconnect() { | ||
- | |||
- | // Loop until we're reconnected | ||
- | |||
- | while (!client.connected()) { | ||
- | |||
- | Serial.print(" | ||
- | |||
- | // Attempt to connect | ||
- | |||
- | if (client.connect(" | ||
- | |||
- | Serial.println(" | ||
- | |||
- | // Subscribe | ||
- | |||
- | client.subscribe(" | ||
- | |||
- | } else { | ||
- | |||
- | Serial.print(" | ||
- | |||
- | Serial.print(client.state()); | ||
- | |||
- | Serial.println(" | ||
- | |||
- | // 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< | ||
- | |||
- | // Pompe marche | ||
- | |||
- | | ||
- | |||
- | | ||
- | |||
- | else { | ||
- | |||
- | Serial.print(" | ||
- | |||
- | client.publish(" | ||
- | | ||
- | |||
- | } | ||
- | |||
- | | ||
- | |||
- | | ||
- | |||
- | | ||
- | |||
- | | ||
- | |||
- | | ||
- | |||
- | | ||
- | |||
- | | ||
#include < | #include < | ||
Ligne 417: | Ligne 241: | ||
// | // | ||
- | |||
</ | </ |
/home/chanteri/www/fablab37110/data/attic/start/arduino/mqtt/esp32.1702362324.txt.gz · Dernière modification : 2023/12/12 07:25 de gerardadmin