Outils pour utilisateurs

Outils du site


start:arduino:esp32:esp01

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:esp32:esp01 [2022/06/08 15:23] – [Ou trouver ce module tout cablé] gerardadminstart:arduino:esp32:esp01 [2023/01/27 16:08] (Version actuelle) – modification externe 127.0.0.1
Ligne 58: Ligne 58:
 === Une video pour expliquer === === Une video pour expliquer ===
  
-[[https://www.youtube.com/watch?v=4BCJyZn8v4k|ESP01 et DHT11 temperature]] 
 <markdown> <markdown>
  
- +[ESP01 et DHT11 temperature](https://www.youtube.com/watch?v=4BCJyZn8v4k)
- +
-[ESP01 et DHT11 temperature](https://www.cdiscount.com/bricolage/electricite/module-de-capteur-de-temperature-et-d-humidite-esp/f-16614-auc4657561066264.html?idOffre=1948814156#mpos=0|mp)+
  
 </markdown> </markdown>
Ligne 73: Ligne 70:
 {{ :start:arduino:esp32:htb1u6_sxzrrk1rjy1zeq6xalfxac.jpg?direct&400 |}} {{ :start:arduino:esp32:htb1u6_sxzrrk1rjy1zeq6xalfxac.jpg?direct&400 |}}
  
-<markdown<+<markdown>
  
-[[https://www.banggood.com/fr/ESP8266-ESP-01-ESP-01S-DHT11-Sensor-Temperature-and-Humidity-WiFi-Node-Module-p-1469256.html?utm_source=googleshopping&utm_medium=cpc_organic&gmcCountry=FR&utm_content=minha&utm_campaign=minha-fr-fr-pc&currency=EUR&cur_warehouse=CN&createTmp=1&utm_source=googleshopping&utm_medium=cpc_bgs&utm_content=sandra&utm_campaign=sandra-ssc-fr-css-all-20bf-1007-11sale&ad_id=471173849183&gclid=EAIaIQobChMI3uDwjcGf9gIVhPXVCh1NzASlEAQYASABEgKmkPD_BwE|pour l'achat du module ]]+[Pour l'achat du module sur Cdiscount ](https://www.cdiscount.com/bricolage/electricite/esp-01s-dht11-module-wifi-capteur-d-humidite-et-te/f-1661416-tim3760330340869.html#mpos=0|mp)
  
-[ESP01 et DHT11 temperature](https://www.cdiscount.com/bricolage/electricite/module-de-capteur-de-temperature-et-d-humidite-esp/f-16614-auc4657561066264.html?idOffre=1948814156#mpos=0|mp)+[Pour l'achat du module sur Aliexpress](https://fr.aliexpress.com/item/4001238206972.html?spm=a2g0o.productlist.0.0.17e356d5eXKO39&algo_pvid=496d1fdb-9096-4be6-8bc1-4152ea671243&algo_exp_id=496d1fdb-9096-4be6-8bc1-4152ea671243-9&pdp_ext_f=%7B%22sku_id%22%3A%2210000015418483354%22%7D&pdp_npi=2%40dis%21EUR%21%213.11%21%21%21%21%21%402100bdd816546950451153642ee2c7%2110000015418483354%21sea)
 </markdown> </markdown>
  
Ligne 218: Ligne 215:
  
 <code c Esp01-DTH11-002.ino> <code c Esp01-DTH11-002.ino>
-/* 
- * DHT Temperature and humidity monitoring using ESP8266 and the askSensors  
- * Description: This examples connects the ESP to wifi, and sends Temperature and humidity to askSensors IoT platfom over HTTPS GET Request. 
-  Author: https://asksensors.com, 2018 - 2019 
-  github: https://github.com/asksensors 
- * InstructableS: https://www.instructables.com/id/DHT11-Temperature-and-Humidity-Monitoring-Using-th/ 
- */ 
  
-// includes+/********* 
 +  Rui Santos 
 +  Complete project details at https://randomnerdtutorials.com/esp8266-dht11dht22-temperature-and-humidity-web-server-with-arduino-ide/ 
 +*********/ 
 + 
 +// Import required libraries 
 +#include <Arduino.h>
 #include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
-#include <WiFiClientSecure.h>+#include <Hash.h> 
 +#include <ESPAsyncTCP.h> 
 +#include <ESPAsyncWebServer.h>
 #include <Adafruit_Sensor.h> #include <Adafruit_Sensor.h>
 #include <DHT.h> #include <DHT.h>
-#include <DHT_U.h> 
  
-// user config: TODO +// Replace with your network credentials 
-const char* wifi_ssid = "..........";             // SSID +const char* ssid = "xxxxxxxxxx"; 
-const char* wifi_password = ".........";         // WIFI +const char* password = "xxxxxxxxxxxxxxxxxxxx"; 
-const char* apiKeyIn = ".........";      // API KEY IN + 
-const unsigned int writeInterval = 25000; // write interval (in ms)+#define DHTPIN 2     // Digital pin connected to the DHT sensor
  
-// ASKSENSORS config. 
-const char* https_host = "api.asksensors.com";         // ASKSENSORS host name 
-const int https_port = 443;                        // https port 
-const char* https_fingerprint =  "B5 C3 1B 2C 0D 5D 9B E5 D6 7C B6 EF 50 3A AD 3F 9F 1E 44 75";     // ASKSENSORS HTTPS SHA1 certificate 
-// DHT config. 
-#define DHTPIN            2         // Pin which is connected to the DHT sensor. 
 // Uncomment the type of sensor in use: // Uncomment the type of sensor in use:
-#define DHTTYPE           DHT11     // DHT 11  +#define DHTTYPE    DHT11     // DHT 11 
-//#define DHTTYPE           DHT22     // DHT 22 (AM2302) +//#define DHTTYPE    DHT22     // DHT 22 (AM2302) 
-//#define DHTTYPE           DHT21     // DHT 21 (AM2301) +//#define DHTTYPE    DHT21     // DHT 21 (AM2301)
-DHT_Unified dht(DHTPIN, DHTTYPE); +
-uint32_t delayMS; +
-int status = WL_IDLE_STATUS; +
-float myTemperature = 0, myHumidity = 0;  +
-// create ASKSENSORS client +
-WiFiClientSecure client; +
-//  +
-void setup() { +
-  Serial.begin(115200); +
-  Serial.println(); +
-  Serial.print("********** connecting to WIFI : "); +
-  Serial.println(wifi_ssid); +
-  WiFi.begin(wifi_ssid, wifi_password); +
-  while (WiFi.status() != WL_CONNECTED) { +
-    delay(500); +
-    Serial.print("."); +
-  } +
-  Serial.println(""); +
-  Serial.println("-> WiFi connected"); +
-  Serial.println("-> IP address: "); +
-  Serial.println(WiFi.localIP()); +
-  // Initialize device. +
-  dht.begin(); +
-  Serial.println("DHTxx Unified Sensor Example"); +
-  // Print temperature sensor details. +
-  sensor_t sensor; +
-  dht.temperature().getSensor(&sensor); +
-  Serial.println("------------------------------------"); +
-  Serial.println("Temperature"); +
-  Serial.print  ("Sensor:       "); Serial.println(sensor.name); +
-  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version); +
-  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id); +
-  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" *C"); +
-  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" *C"); +
-  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" *C");   +
-  Serial.println("------------------------------------"); +
-  // Print humidity sensor details. +
-  dht.humidity().getSensor(&sensor); +
-  Serial.println("------------------------------------"); +
-  Serial.println("Humidity"); +
-  Serial.print  ("Sensor:       "); Serial.println(sensor.name); +
-  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version); +
-  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id); +
-  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println("%"); +
-  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println("%"); +
-  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println("%");   +
-  Serial.println("------------------------------------"); +
-  // Set delay between sensor readings based on sensor details. +
-  delayMS = sensor.min_delay / 1000; +
-  // Set Client to insecure +
-  client.setInsecure(); +
-}+
  
-void loop() { +DHT dht(DHTPIN, DHTTYPE); 
-// Read data from DHT + 
-// Delay between measurements. +// current temperature & humidity, updated in loop() 
-  delay(delayMS); +float t = 0.0; 
-  // Get temperature event and print its value+float h = 0.0; 
-  sensors_event_t event;   + 
-  dht.temperature().getEvent(&event); +// Create AsyncWebServer object on port 80 
-  if (isnan(event.temperature)) { +AsyncWebServer server(80); 
-    Serial.println("Error reading temperature!");+ 
 +// Generally, you should use "unsigned long" for variables that hold time 
 +// The value will quickly become too large for an int to store 
 +unsigned long previousMillis = 0;    // will store last time DHT was updated 
 + 
 +// Updates DHT readings every 10 seconds 
 +const long interval = 10000;   
 + 
 +const char index_html[] PROGMEM = R"rawliteral( 
 +<!DOCTYPE HTML><html> 
 +<head> 
 +  <meta name="viewport" content="width=device-width, initial-scale=1"> 
 +  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous"> 
 +  <style> 
 +    html { 
 +     font-family: Arial; 
 +     display: inline-block; 
 +     margin: 0px auto; 
 +     text-align: center; 
 +    } 
 +    h2 { font-size: 3.0rem; } 
 +    p { font-size: 3.0rem; } 
 +    .units { font-size: 1.2rem; } 
 +    .dht-labels{ 
 +      font-size: 1.5rem; 
 +      vertical-align:middle; 
 +      padding-bottom: 15px; 
 +    } 
 +  </style> 
 +</head> 
 +<body> 
 +  <h2>ESP8266 DHT Server</h2> 
 +  <p> 
 +    <i class="fas fa-thermometer-half" style="color:#059e8a;"></i>  
 +    <span class="dht-labels">Temperature</span>  
 +    <span id="temperature">%TEMPERATURE%</span> 
 +    <sup class="units">&deg;C</sup> 
 +  </p> 
 +  <p> 
 +    <i class="fas fa-tint" style="color:#00add6;"></i>  
 +    <span class="dht-labels">Humidity</span> 
 +    <span id="humidity">%HUMIDITY%</span> 
 +    <sup class="units">%</sup> 
 +  </p> 
 +</body> 
 +<script> 
 +setInterval(function ( ) 
 +  var xhttp = new XMLHttpRequest(); 
 +  xhttp.onreadystatechange = function() { 
 +    if (this.readyState == 4 && this.status == 200) { 
 +      document.getElementById("temperature").innerHTML = this.responseText; 
 +    } 
 +  }; 
 +  xhttp.open("GET", "/temperature", true)
 +  xhttp.send(); 
 +}, 10000 ) ; 
 + 
 +setInterval(function ( ) { 
 +  var xhttp = new XMLHttpRequest(); 
 +  xhttp.onreadystatechange = function() { 
 +    if (this.readyState == 4 && this.status == 200) { 
 +      document.getElementById("humidity").innerHTML = this.responseText; 
 +    } 
 +  }; 
 +  xhttp.open("GET", "/humidity", true); 
 +  xhttp.send(); 
 +}, 10000 ) ; 
 +</script> 
 +</html>)rawliteral"; 
 + 
 +// Replaces placeholder with DHT values 
 +String processor(const String& var){ 
 +  //Serial.println(var); 
 +  if(var == "TEMPERATURE"){ 
 +    return String(t);
   }   }
-  else +  else if(var == "HUMIDITY"){ 
-    // Update temperature and humidity +    return String(h);
-    myTemperature = (float)event.temperature; +
-    Serial.print("Temperature: "); +
-    Serial.print(myTemperature); +
-    Serial.println(" C"); +
-  } +
-  // Get humidity event and print its value. +
-  dht.humidity().getEvent(&event); +
-  if (isnan(event.relative_humidity)) { +
-    Serial.println("Error reading humidity!")+
-  } +
-  else { +
-     myHumidity = (float)event.relative_humidity; +
-    Serial.print("Humidity: "); +
-    Serial.print(myHumidity); +
-    Serial.println("%");+
   }   }
 +  return String();
 +}
 +
 +void setup(){
 +  // Serial port for debugging purposes
 +  Serial.begin(115200);
 +  dht.begin();
      
-// Use WiFiClientSecure class to create TLS connection +  // Connect to Wi-Fi 
-  Serial.print("********** connecting to HOST : "); +  WiFi.begin(ssid, password); 
-  Serial.println(https_host); +  Serial.println("Connecting to WiFi"); 
-  if (!client.connect(https_host, https_port)) { +  while (WiFi.status() != WL_CONNECTED) { 
-    Serial.println("-> connection failed")+    delay(1000); 
-    //return;+    Serial.println(".");
   }   }
-  /* ignore certificate verification  
-  if (client.verify(https_fingerprint, https_host)) { 
-    Serial.println("-> certificate matches"); 
-  } else { 
-    Serial.println("-> certificate doesn't match"); 
-  } 
-  */ 
-  // Create a URL for the request 
-  String url = "/write/"; 
-  url += apiKeyIn; 
-    url += "?module1="; 
-  url += myTemperature; 
-  url += "&module2="; 
-  url += myHumidity; 
-   
-  Serial.print("********** requesting URL: "); 
-  Serial.println(url); 
  
-  client.print(String("GET "+ url + " HTTP/1.1\r\n"+  // Print ESP8266 Local IP Address 
-               "Host: " + https_host + "\r\n"+  Serial.println(WiFi.localIP());
-               "Connection: close\r\n\r\n");+
  
-  Serial.println("> Request sent to ASKSENSORS"); +  // Route for root / web page 
-  while (client.connected()) { +  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ 
-    String line = client.readStringUntil('\n'); +    request->send_P(200, "text/html", index_html, processor); 
-    if (line == "\r") { +  }); 
-        String line client.readStringUntil('\n'); +  server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){ 
-        Serial.println("********** ASKSENSORS replay:"); +    request->send_P(200, "text/plain", String(t).c_str()); 
-        Serial.println(line); +  }); 
-        Serial.println("********** closing connection"); +  server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){ 
-       +    request->send_P(200, "text/plain", String(h).c_str()); 
-        break;+  }); 
 + 
 +  // Start server 
 +  server.begin(); 
 +
 +  
 +void loop(){   
 +  unsigned long currentMillis = millis(); 
 +  if (currentMillis - previousMillis >interval) { 
 +    // save the last time you updated the DHT values 
 +    previousMillis currentMillis; 
 +    // Read temperature as Celsius (the default) 
 +    float newT = dht.readTemperature(); 
 +    // Read temperature as Fahrenheit (isFahrenheit = true) 
 +    //float newT = dht.readTemperature(true); 
 +    // if temperature read failed, don't change t value 
 +    if (isnan(newT)) { 
 +      Serial.println("Failed to read from DHT sensor!"); 
 +    } 
 +    else { 
 +      t = newT; 
 +      Serial.println(t); 
 +    } 
 +    // Read Humidity 
 +    float newH = dht.readHumidity(); 
 +    // if humidity read failed, don't change h value  
 +    if (isnan(newH)) { 
 +      Serial.println("Failed to read from DHT sensor!"); 
 +    } 
 +    else { 
 +      h = newH; 
 +      Serial.println(h);
     }     }
   }   }
- 
-  delay(writeInterval );     // delay in msec 
 } }
 +
 </code> </code>
  
Ligne 545: Ligne 562:
 const char* ssid     = "YourRouterID"; const char* ssid     = "YourRouterID";
 const char* password = "YourRouterPassword"; const char* password = "YourRouterPassword";
-*/+
 ESP8266WebServer server(80); ESP8266WebServer server(80);
    
-// Initialize DHT sensor  +//Initialiser le capteur DHT 
-// NOTEFor working with a faster than ATmega328p 16 MHz Arduino chiplike an ESP8266, +// REMARQUE Pour travailler avec une puce Arduino ATmega328p 16 MHz plus rapidecomme un ESP8266, 
-// you need to increase the threshold for cycle counts considered a or 0. +// vous devez augmenter le seuil pour les comptages de cycles considérés comme ou 0. 
-// You can do this by passing a 3rd parameter for this threshold It's a bit +// Vous pouvez le faire en passant un 3ème paramètre pour ce seuilC'est un peu 
-// of fiddling to find the right valuebut in general the faster the CPU the +// de bidouiller pour trouver la bonne valeurmais en général plus le CPU est rapide plus 
-// higher the value The default for a 16mhz AVR is a value of 6.  For an +// augmente la valeurLa valeur par défaut pour un AVR 16 MHz est une valeur de 6. Pour un 
-// Arduino Due that runs at 84mhz a value of 30 works+// Arduino Due qui tourne à 84mhz une valeur de 30 fonctionne
-// This is for the ESP8266 processor on ESP-01 +// C'est pour le processeur ESP8266 sur ESP-01 
 DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266 DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266
    
-float humidity, temp_f;  // Values read from sensor+float humidity, temp_c;  // Values read from sensor
 String webString="";     // String to display String webString="";     // String to display
 // Generally, you should use "unsigned long" for variables that hold time // Generally, you should use "unsigned long" for variables that hold time
Ligne 595: Ligne 613:
   server.on("/temp", [](){  // if you add this subdirectory to your webserver call, you get text below :)   server.on("/temp", [](){  // if you add this subdirectory to your webserver call, you get text below :)
     gettemperature();       // read sensor     gettemperature();       // read sensor
-    webString="Temperature: "+String((int)temp_f)+" F";   // Arduino has a hard time with float to string+    webString="Temperature: "+String((int)temp_c)+" C";   // Arduino has a hard time with float to string
     server.send(200, "text/plain", webString);            // send to someones browser when asked     server.send(200, "text/plain", webString);            // send to someones browser when asked
   });   });
Ligne 601: Ligne 619:
   server.on("/humidity", [](){  // if you add this subdirectory to your webserver call, you get text below :)   server.on("/humidity", [](){  // if you add this subdirectory to your webserver call, you get text below :)
     gettemperature();           // read sensor     gettemperature();           // read sensor
-    webString="Humidity: "+String((int)humidity)+"%";+    webString="Humidite: "+String((int)humidity)+"%";
     server.send(200, "text/plain", webString);               // send to someones browser when asked     server.send(200, "text/plain", webString);               // send to someones browser when asked
   });   });
Ligne 628: Ligne 646:
     // Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)     // Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
     humidity = dht.readHumidity();          // Read humidity (percent)     humidity = dht.readHumidity();          // Read humidity (percent)
-    temp_f = dht.readTemperature(true);     // Read temperature as Fahrenheit+    /*Fahrenheit est une échelle de température thermodynamique, où le point de congélation de l'eau est  
 +    à 32 degrés Fahrenheit (°F)et le point d'ébullition à 212 °F (sous une pression atmosphérique normale). 
 +    Cela sépare les points d'ébullition et de congélation de l'eau d’exactement 180 degrés. Par conséquent, 
 +    un degré sur l'échelle Fahrenheit représente 1/180 de l'intervalle entre le point de congélation et 
 +    le point d'ébullition de l'eau. Le zéro absolu est défini comme égal      à-459,67 °F.    
 +     
 +    Celsius :Bien qu'initialement défini comme le point de congélation de l'eau (et plus tard le point de  
 +    fusion de la glace),  l'échelle Celsius est maintenant officiellement une échelle dérivée, définie par 
 +    rapport à la l’échelle de température Kelvin .Sur l'échelle Celsius le zéro (0 °C) est maintenant  
 +    défini comme égal à 273,15 K, avec une différence de température de 1 deg C équivalent à une différence 
 +    de 1 K, c’est-à-dire que la taille de l'unité sur chaque échelle est la même.Cela signifie que 100 °C,  
 +    préalablement défini comme le point d'ébullition de l'eau, est maintenant défini comme l'équivalent de  
 +    373,15 K.Une différence de température d’1 °F équivaut à une différence de température de 0,556 °C. 
 +    */ 
 +    //Pour avoir la temperature en °Celcius à partir de ° Fahrenheit appliquons la formule : 
 +    // °Celius (°F -32)/1.800  
 +    temp_c = ((dht.readTemperature(true) -32)/1.800) ;     // Read temperature as  °Celcius
     // Check if any reads failed and exit early (to try again).     // Check if any reads failed and exit early (to try again).
-    if (isnan(humidity) || isnan(temp_f)) {+    if (isnan(humidity) || isnan(temp_c)) {
       Serial.println("Failed to read from DHT sensor!");       Serial.println("Failed to read from DHT sensor!");
       return;       return;
/home/chanteri/www/fablab37110/data/attic/start/arduino/esp32/esp01.1654694611.txt.gz · Dernière modification : 2023/01/27 16:08 (modification externe)