start:arduino:esp32:esp01
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:esp32:esp01 [2022/06/08 15:21] – [Une video pour expliquer] gerardadmin | start: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:// | ||
| < | < | ||
| - | + | [ESP01 et DHT11 temperature](https:// | |
| - | + | ||
| - | [ESP01 et DHT11 temperature](https:// | + | |
| </ | </ | ||
| Ligne 73: | Ligne 70: | ||
| {{ : | {{ : | ||
| - | [[https:// | + | < |
| + | [Pour l' | ||
| + | [Pour l' | ||
| + | </ | ||
| ==== Code pour esp01 et capteur temperature DHT11 via serveur Web (simplifié)==== | ==== Code pour esp01 et capteur temperature DHT11 via serveur Web (simplifié)==== | ||
| Ligne 215: | 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: | ||
| - | | ||
| - | | ||
| - | * InstructableS: | ||
| - | */ | ||
| - | // includes | + | /********* |
| + | Rui Santos | ||
| + | Complete project details at https:// | ||
| + | *********/ | ||
| + | |||
| + | // Import required libraries | ||
| + | #include < | ||
| #include < | #include < | ||
| - | #include <WiFiClientSecure.h> | + | #include <Hash.h> |
| + | #include < | ||
| + | #include < | ||
| #include < | #include < | ||
| #include < | #include < | ||
| - | #include < | ||
| - | // user config: TODO | + | // Replace with your network credentials |
| - | const char* wifi_ssid | + | const char* ssid = "xxxxxxxxxx"; |
| - | const char* wifi_password | + | const char* password |
| - | const char* apiKeyIn = " | + | |
| - | const unsigned int writeInterval = 25000; | + | #define DHTPIN 2 // Digital pin connected to the DHT sensor |
| - | // ASKSENSORS config. | ||
| - | const char* https_host = " | ||
| - | 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"; | ||
| - | // DHT config. | ||
| - | #define DHTPIN | ||
| // Uncomment the type of sensor in use: | // Uncomment the type of sensor in use: | ||
| - | #define DHTTYPE | + | #define DHTTYPE |
| - | //#define DHTTYPE | + | //#define DHTTYPE |
| - | //#define DHTTYPE | + | //#define DHTTYPE |
| - | 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(" | + | |
| - | Serial.println(wifi_ssid); | + | |
| - | WiFi.begin(wifi_ssid, | + | |
| - | while (WiFi.status() != WL_CONNECTED) { | + | |
| - | delay(500); | + | |
| - | Serial.print(" | + | |
| - | } | + | |
| - | Serial.println("" | + | |
| - | Serial.println(" | + | |
| - | Serial.println(" | + | |
| - | Serial.println(WiFi.localIP()); | + | |
| - | // Initialize device. | + | |
| - | dht.begin(); | + | |
| - | Serial.println(" | + | |
| - | // Print temperature sensor details. | + | |
| - | sensor_t sensor; | + | |
| - | dht.temperature().getSensor(& | + | |
| - | Serial.println(" | + | |
| - | Serial.println(" | + | |
| - | Serial.print | + | |
| - | Serial.print | + | |
| - | Serial.print | + | |
| - | Serial.print | + | |
| - | Serial.print | + | |
| - | Serial.print | + | |
| - | Serial.println(" | + | |
| - | // Print humidity sensor details. | + | |
| - | dht.humidity().getSensor(& | + | |
| - | Serial.println(" | + | |
| - | Serial.println(" | + | |
| - | Serial.print | + | |
| - | Serial.print | + | |
| - | Serial.print | + | |
| - | Serial.print | + | |
| - | Serial.print | + | |
| - | Serial.print | + | |
| - | 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() |
| - | | + | float t = 0.0; |
| - | // Get temperature event and print its value. | + | float h = 0.0; |
| - | | + | |
| - | dht.temperature().getEvent(&event); | + | // Create AsyncWebServer object on port 80 |
| - | if (isnan(event.temperature)) { | + | AsyncWebServer server(80); |
| - | Serial.println(" | + | |
| + | // Generally, you should use " | ||
| + | // The value will quickly become too large for an int to store | ||
| + | unsigned long previousMillis = 0; | ||
| + | |||
| + | // Updates DHT readings every 10 seconds | ||
| + | const long interval = 10000; | ||
| + | |||
| + | const char index_html[] PROGMEM = R" | ||
| + | < | ||
| + | < | ||
| + | | ||
| + | <link rel=" | ||
| + | | ||
| + | html { | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | } | ||
| + | h2 { font-size: 3.0rem; } | ||
| + | p { font-size: 3.0rem; } | ||
| + | .units { font-size: 1.2rem; } | ||
| + | .dht-labels{ | ||
| + | font-size: 1.5rem; | ||
| + | vertical-align: | ||
| + | padding-bottom: | ||
| + | } | ||
| + | </ | ||
| + | </ | ||
| + | < | ||
| + | | ||
| + | <p> | ||
| + | <i class=" | ||
| + | <span class=" | ||
| + | <span id="temperature"> | ||
| + | <sup class=" | ||
| + | </ | ||
| + | <p> | ||
| + | <i class=" | ||
| + | <span class=" | ||
| + | <span id=" | ||
| + | <sup class=" | ||
| + | </ | ||
| + | </ | ||
| + | < | ||
| + | setInterval(function | ||
| + | var xhttp = new XMLHttpRequest(); | ||
| + | | ||
| + | | ||
| + | document.getElementById(" | ||
| + | } | ||
| + | }; | ||
| + | xhttp.open(" | ||
| + | xhttp.send(); | ||
| + | }, 10000 ) ; | ||
| + | |||
| + | setInterval(function ( ) { | ||
| + | var xhttp = new XMLHttpRequest(); | ||
| + | xhttp.onreadystatechange = function() { | ||
| + | | ||
| + | document.getElementById(" | ||
| + | } | ||
| + | }; | ||
| + | xhttp.open(" | ||
| + | xhttp.send(); | ||
| + | }, 10000 ) ; | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | // 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 | + | |
| - | myTemperature = (float)event.temperature; | + | |
| - | Serial.print(" | + | |
| - | Serial.print(myTemperature); | + | |
| - | Serial.println(" | + | |
| - | } | + | |
| - | // Get humidity event and print its value. | + | |
| - | dht.humidity().getEvent(& | + | |
| - | | + | |
| - | Serial.println("Error reading humidity!"); | + | |
| - | } | + | |
| - | else { | + | |
| - | | + | |
| - | | + | |
| - | 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 | + | |
| - | | + | |
| - | Serial.println(https_host); | + | Serial.println(" |
| - | | + | |
| - | Serial.println(" | + | delay(1000); |
| - | //return; | + | Serial.println(" |
| } | } | ||
| - | /* ignore certificate verification | ||
| - | if (client.verify(https_fingerprint, | ||
| - | Serial.println(" | ||
| - | } else { | ||
| - | Serial.println(" | ||
| - | } | ||
| - | */ | ||
| - | // Create a URL for the request | ||
| - | String url = "/ | ||
| - | url += apiKeyIn; | ||
| - | url += "? | ||
| - | url += myTemperature; | ||
| - | url += "& | ||
| - | url += myHumidity; | ||
| - | | ||
| - | Serial.print(" | ||
| - | Serial.println(url); | ||
| - | | + | |
| - | " | + | Serial.println(WiFi.localIP()); |
| - | " | + | |
| - | | + | |
| - | | + | server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ |
| - | String | + | request->send_P(200, " |
| - | if (line == " | + | |
| - | | + | server.on("/ |
| - | Serial.println(" | + | request-> |
| - | Serial.println(line); | + | }); |
| - | Serial.println(" | + | server.on("/ |
| - | + | | |
| - | break; | + | }); |
| + | |||
| + | // Start server | ||
| + | server.begin(); | ||
| + | } | ||
| + | |||
| + | void loop(){ | ||
| + | unsigned long currentMillis = millis(); | ||
| + | | ||
| + | // save the last time you updated the DHT values | ||
| + | previousMillis | ||
| + | // 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)) { | ||
| + | | ||
| + | } | ||
| + | else { | ||
| + | t = newT; | ||
| + | | ||
| + | } | ||
| + | // Read Humidity | ||
| + | float newH = dht.readHumidity(); | ||
| + | // if humidity read failed, don't change h value | ||
| + | if (isnan(newH)) { | ||
| + | | ||
| + | } | ||
| + | else { | ||
| + | | ||
| + | | ||
| } | } | ||
| } | } | ||
| - | |||
| - | delay(writeInterval ); // delay in msec | ||
| } | } | ||
| + | |||
| </ | </ | ||
| Ligne 542: | Ligne 562: | ||
| const char* ssid = " | const char* ssid = " | ||
| const char* password = " | const char* password = " | ||
| - | */ | + | |
| ESP8266WebServer server(80); | ESP8266WebServer server(80); | ||
| - | // Initialize | + | //Initialiser le capteur |
| - | // NOTE: For working with a faster than ATmega328p 16 MHz Arduino chip, like an ESP8266, | + | // REMARQUE |
| - | // you need to increase the threshold for cycle counts considered a 1 or 0. | + | // vous devez augmenter le seuil pour les comptages de cycles considérés comme 1 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 seuil. C'est un peu |
| - | // of fiddling to find the right value, but in general the faster the CPU the | + | // de bidouiller pour trouver la bonne valeur, mais 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 valeur. La 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 | + | // C'est pour le processeur |
| DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266 | DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266 | ||
| - | float humidity, | + | float humidity, |
| String webString=""; | String webString=""; | ||
| // Generally, you should use " | // Generally, you should use " | ||
| Ligne 592: | Ligne 613: | ||
| server.on("/ | server.on("/ | ||
| gettemperature(); | gettemperature(); | ||
| - | webString=" | + | webString=" |
| server.send(200, | server.send(200, | ||
| }); | }); | ||
| Ligne 598: | Ligne 619: | ||
| server.on("/ | server.on("/ | ||
| gettemperature(); | gettemperature(); | ||
| - | webString=" | + | webString=" |
| server.send(200, | server.send(200, | ||
| }); | }); | ||
| Ligne 625: | Ligne 646: | ||
| // Sensor readings may also be up to 2 seconds ' | // Sensor readings may also be up to 2 seconds ' | ||
| humidity = dht.readHumidity(); | humidity = dht.readHumidity(); | ||
| - | | + | |
| + | à 32 degrés Fahrenheit (°F)et le point d' | ||
| + | Cela sépare les points d' | ||
| + | un degré sur l' | ||
| + | le point d' | ||
| + | |||
| + | Celsius :Bien qu' | ||
| + | fusion de la glace), | ||
| + | rapport à la l’échelle de température Kelvin .Sur l' | ||
| + | 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' | ||
| + | préalablement défini comme le point d' | ||
| + | 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 | ||
| + | temp_c = ((dht.readTemperature(true) -32)/1.800) ; // Read temperature as | ||
| // 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(" | Serial.println(" | ||
| return; | return; | ||
/home/chanteri/www/fablab37110/data/attic/start/arduino/esp32/esp01.1654694508.txt.gz · Dernière modification : (modification externe)
