====== Capteur Température / humidité : Wemos D1 DHT12 ====== * [[https://www.wemos.cc/en/latest/d1/index.html| Wemos]] ESP8266 puce WiFi * [[https://www.wemos.cc/en/latest/d1_mini_shield/dht.html]] ajout capteur DHT12 * relai de mesure de la tension , résistance 180k ( pas sur la V1 ) * batterie 18650 ===== Codes ===== V1 #include #include #include #include #include "EspMQTTClient.h" const char* ssid = "kerminy"; const char* password = "passtruc"; const char* mqtt_server = "192.168.1.13"; const char* host = "ESP-C49200"; // = ausi nom client mqtt const char* mqtt_user = "kerminy"; const char* mqtt_pass = "passtruc"; int mqtt_port = 1883; int prog_version = 1; int version_maj = 1; // a faire la maj OTA EspMQTTClient client(NULL, NULL, mqtt_server, mqtt_user, mqtt_pass, host, mqtt_port); double batterie = 0; // resistance 180 k A0<->GND float minute_dodo = 10; int boucle1 = 0; unsigned long lastMsg = 0; #define MSG_BUFFER_SIZE (50) char msg[MSG_BUFFER_SIZE]; int value = 0; bool donnee_prete = false; bool connexion_ok = false; bool attend_flash = false; #define capteur_dht12 float temperature = 0; float humidite = 0; float pression = 0; #ifdef capteur_dht12 #include DHT12 dht12; #endif void setup_wifi() { delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); if ((char)payload[0] == 'F') { // FLASH si F digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level // A FAIRE } else { digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH } } void dodo() { Serial.print("dodo min :"); Serial.println((float)minute_dodo); // Connect D0 to RST to wake up ! ESP.deepSleep(minute_dodo * 60 * 1000000); } bool recup_donnees() { int boucle = 0; while (donnee_prete != true && boucle < 5) { // lescture des donnees if (dht12.get() == 0) { donnee_prete = true; temperature = dht12.cTemp; humidite = dht12.humidity; Serial.print("Température: "); Serial.println(temperature); Serial.print("Humidité : "); Serial.println(humidite); Serial.println(); return true; } Serial.println("relecture capteur"); delay(1000); boucle++; } Serial.println("pb capteur = reboot"); dodo(); return false; } void onConnectionEstablished() { connexion_ok = true; Serial.println("connected"); } void setup() { Serial.begin(115200); pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output Serial.begin(115200); Serial.println(""); Serial.print("ChipID : "); Serial.println(ESP.getChipId(), HEX); batterie = analogRead(A0) ; batterie = batterie / 214.5; // 209.66 assumes external 180K resistor / 1024) * 6.13; Serial.println(batterie); recup_donnees(); setup_wifi(); client.enableDebuggingMessages(); // Enable debugging messages sent to serial output } void loop() { // premiere lecture if (dht12.get() == 0) { donnee_prete = true; temperature = dht12.cTemp; humidite = dht12.humidity; Serial.println(""); Serial.print("Température: "); Serial.println(temperature); Serial.print("Humidité : "); Serial.println(humidite); Serial.println(); } client.loop(); // on a au moins une valeur unsigned long now = millis(); if (now - lastMsg > 15000) { Serial.println("pas d'acces au serveur MQTT , dodo min :"); dodo(); } if (now - lastMsg > 5000) { // attend 5s avant de faire lastMsg = now; ++value; if (donnee_prete == true && connexion_ok == true) { // tout est pret Serial.println("publie"); // Publish a message to "mytopic/test" String topic1 = "pgvrs/"; topic1 += host; String topic2 = "temp/" ; topic2 += host; String topic3 = "humid/"; topic3 += host; String topic4 = "batt/" ; topic4 += host; Serial.println(topic1); Serial.println(topic2); Serial.println(topic3); Serial.println(topic4); if ( client.publish(String(topic1).c_str(), String(prog_version) .c_str() ) && client.publish(String(topic2).c_str(), String(dht12.cTemp).c_str() ) && client.publish(String(topic3).c_str(), String(dht12.humidity).c_str() ) && client.publish(String(topic4).c_str(), String(batterie).c_str() ) ) { Serial.println("donnees transmises"); delay(5000); /* // Subscribe to "mytopic/test" and display received message to Serial String topic = ""; topic = String(host) + "/" + String(prog_version); if ( client.subscribe(topic, [](const String & payload) { version_maj = atoi(payload.c_str()) ; }) ) { if (version_maj != prog_version) { client.enableHTTPWebUpdater(); // Enable the web updater. User and password default to values of MQTTUsername and MQTTPassword. These can be overrited with enableHTTPWebUpdater("user", "password"). Serial.println("attend flash"); attend_flash = true; } } */ dodo(); } } } delay(1000); } {{tag>ESP8266}} {{tag>MQTT}}