ESP8266 - Capteur de Température et d'Humidité - OLED

Ce tutoriel vous explique comment utiliser l'ESP8266 pour lire la température et l'humidité à partir du capteur DHT11/DHT22 et les afficher sur un OLED.

Capteur de température et d'humidité ESP8266 NodeMCU DHT11/DHT22 avec affichage OLED

Préparation du matériel

1×ESP8266 NodeMCU
1×Micro USB Cable
1×SSD1306 I2C OLED Display 128x64
1×DHT11 Temperature and Humidity Sensor
1×Jumper Wires
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) ESP8266 Screw Terminal Adapter

You can use DHT22 sensor instead of DHT11 sensor.

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Divulgation : Certains des liens fournis dans cette section sont des liens affiliés Amazon. Nous pouvons recevoir une commission pour tout achat effectué via ces liens, sans coût supplémentaire pour vous. Nous vous remercions de votre soutien.

À propos de l'écran OLED, des capteurs d'humidité et de température DHT11 et DHT22

Si vous n'êtes pas familiarisé avec l'écran OLED, les capteurs de température et d'humidité DHT11 et DHT22 (y compris le brochage, les fonctionnalités, la programmation, etc.), les tutoriels suivants peuvent vous aider :

Diagramme de câblage

Câblage ESP8266 - Module DHT11 LCD

Schéma de câblage OLED du capteur DHT11 ESP8266 NodeMCU

This image is created using Fritzing. Click to enlarge image

Voir plus dans l'agencement des broches de l'ESP8266 et comment alimenter l'ESP8266 et d'autres composants.

ESP8266 - Câblage du module DHT22 avec LCD

Schéma de câblage OLED du capteur DHT22 ESP8266 NodeMCU

This image is created using Fritzing. Click to enlarge image

Code ESP8266 - Capteur DHT11 - OLED

/* * Ce code ESP8266 NodeMCU a été développé par newbiely.fr * Ce code ESP8266 NodeMCU est mis à disposition du public sans aucune restriction. * Pour des instructions complètes et des schémas de câblage, veuillez visiter: * https://newbiely.fr/tutorials/esp8266/esp8266-temperature-humidity-sensor-oled */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DHT.h> #define OLED_WIDTH 128 // OLED display width, in pixels #define OLED_HEIGHT 64 // OLED display height, in pixels #define DHT_PIN 2 // pin connected to DHT11 sensor #define DHT_TYPE DHT11 Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C DHT dht(DHT_PIN, DHT_TYPE); String displayString; void setup() { Serial.begin(9600); // initialize OLED display with address 0x3C for 128x64 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // wait for initializing oled.clearDisplay(); // clear display oled.setTextSize(2); // text size oled.setTextColor(WHITE); // text color oled.setCursor(0, 10); // position to display dht.begin(); // initialize DHT11 the temperature and humidity sensor displayString.reserve(10); // to avoid fragmenting memory when using String } void loop() { float humi = dht.readHumidity(); // read humidity float temperature_C = dht.readTemperature(); // read temperature // check if any reads failed if (isnan(humi) || isnan(temperature_C)) { displayString = "Failed"; } else { displayString = String(temperature_C, 1); // one decimal places displayString += "°C"; displayString += String(humi, 1); // one decimal places displayString += "%"; } Serial.println(displayString); // print the temperature in Celsius to Serial Monitor oled_display_center(displayString); // display temperature and humidity on OLED } void oled_display_center(String text) { int16_t x1; int16_t y1; uint16_t width; uint16_t height; oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height); // center the display both horizontally and vertically oled.clearDisplay(); // clear display oled.setCursor((OLED_WIDTH - width) / 2, (OLED_HEIGHT - height) / 2); oled.println(text); // text to display oled.display(); }

Étapes rapides

Pour commencer avec l'ESP8266 sur Arduino IDE, suivez ces étapes :

  • Consultez le tutoriel comment configurer l'environnement pour ESP8266 sur Arduino IDE si c'est la première fois que vous utilisez ESP8266.
  • Câblez les composants comme indiqué sur le schéma.
  • Connectez la carte ESP8266 à votre ordinateur à l'aide d'un câble USB.
  • Ouvrez Arduino IDE sur votre ordinateur.
  • Choisissez la bonne carte ESP8266, telle que (par exemple NodeMCU 1.0 (Module ESP-12E)), et son port COM respectif.
  • Cliquez sur l'icône Libraries dans la barre gauche de l'Arduino IDE.
  • Recherchez “SSD1306” et localisez la bibliothèque SSD1306 d'Adafruit.
  • Ensuite, appuyez sur le bouton Install pour terminer l'installation.
Bibliothèque OLED ESP8266 NodeMCU
  • Vous serez invité à installer des dépendances de bibliothèque supplémentaires.
  • Pour les installer toutes, cliquez sur le bouton Install All.
Bibliothèque de capteurs Adafruit GFX pour ESP8266 NodeMCU
  • Recherchez « DHT » et localisez la bibliothèque de capteurs DHT par Adafruit.
  • Appuyez sur le bouton Install pour installer la bibliothèque.
Bibliothèque de capteur DHT ESP8266 NodeMCU
  • Vous devrez installer d'autres dépendances de bibliothèques.
  • Cliquez sur le bouton Install All pour installer toutes les dépendances de la bibliothèque.
Bibliothèque de capteurs unifiée Adafruit ESP8266 NodeMCU
  • Copiez le code et ouvrez-le avec l'IDE Arduino.
  • Cliquez sur le bouton Upload dans l'IDE Arduino pour compiler et téléverser le code vers l'ESP8266.
  • Placez le capteur dans de l'eau chaude et de l'eau froide, ou tenez-le dans votre main.
  • Consultez le résultat sur l'écran OLED et dans le moniteur série.

※ NOTE THAT:

Le code en question centrera le texte à la fois horizontalement et verticalement sur un écran OLED.

Code ESP8266 - Capteur DHT22 - OLED

/* * Ce code ESP8266 NodeMCU a été développé par newbiely.fr * Ce code ESP8266 NodeMCU est mis à disposition du public sans aucune restriction. * Pour des instructions complètes et des schémas de câblage, veuillez visiter: * https://newbiely.fr/tutorials/esp8266/esp8266-temperature-humidity-sensor-oled */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DHT.h> #define OLED_WIDTH 128 // OLED display width, in pixels #define OLED_HEIGHT 64 // OLED display height, in pixels #define DHT_PIN 2 // pin connected to DHT22 sensor #define DHT_TYPE DHT22 Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C DHT dht(DHT_PIN, DHT_TYPE); String displayString; void setup() { Serial.begin(9600); // initialize OLED display with address 0x3C for 128x64 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // wait for initializing oled.clearDisplay(); // clear display oled.setTextSize(2); // text size oled.setTextColor(WHITE); // text color oled.setCursor(0, 10); // position to display dht.begin(); // initialize DHT22 the temperature and humidity sensor displayString.reserve(10); // to avoid fragmenting memory when using String } void loop() { float humi = dht.readHumidity(); // read humidity float temperature_C = dht.readTemperature(); // read temperature // check if any reads failed if (isnan(humi) || isnan(temperature_C)) { displayString = "Failed"; } else { displayString = String(temperature_C, 1); // one decimal places displayString += "°C"; displayString += String(humi, 1); // one decimal places displayString += "%"; } Serial.println(displayString); // print the temperature in Celsius to Serial Monitor oled_display_center(displayString); // display temperature and humidity on OLED } void oled_display_center(String text) { int16_t x1; int16_t y1; uint16_t width; uint16_t height; oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height); // center the display both horizontally and vertically oled.clearDisplay(); // clear display oled.setCursor((OLED_WIDTH - width) / 2, (OLED_HEIGHT - height) / 2); oled.println(text); // text to display oled.display(); }

※ NOTE THAT:

Le code pour le DHT11 et le DHT22 est le même, à l'exception d'une seule ligne. La bibliothèque pour les deux est également la même.

Vidéo

※ OUR MESSAGES

  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!