ESP8266 WiFi – Temperatuur en luchtvochtigheid sensor DHT 11 (ArduinoIDE)
Op deze pagina vind je een voorbeeld om via de ESP-01 module een DHT 11 module uit te lezen via een GPIO pin.
Hardware
Met deze module kun je de temperatuur en luchtvochtigheid meten, hij is door de fabrikant al behoorlijk geijkt, eventuele aanpassingen (bijvoorbeeld ijken) kunnen softwarematig gedaan worden.
- Werkt op: 5V.
- Temperatuur bereik: 0 – +50 ºC.
- Temperatuur nauwkeurigheid: ±2,0 ºC.
- Vochtigheid bereik: 20-95% RH.
- Vochtigheid nauwkeurigheid: ±5,0% RH.
- Response tijd: < 5 sec.
Pinout
Pin: | Functie: |
---|---|
01 | +5V |
02 | Data |
03 | NC (niet aangesloten) |
04 | GND |
De Keyes versie (met weerstand):
[table “6” not found /]LET OP: Er zijn ook andere versie in omloop, controleer altijd de pinout of raadpleeg de handleiding!
Wat heb je nodig?
1) Firmware flashen met ArduinoIDE
2) PietteTech DHT8266 bibliotheek
Sluit de DHT module aan op GPIO 2 volgens onderstaand schema:
De code om de DHT module uit te lezen:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
#include "PietteTech_DHT.h" // system defines #define DHTTYPE DHT11 // Sensor type DHT11/21/22/AM2301/AM2302 #define DHTPIN 2 // Digital pin for communications #define REPORT_INTERVAL 5000 // in msec must > 2000 // to check dht unsigned long startMills; float t, h, d; int acquireresult; int acquirestatus; //declaration void dht_wrapper(); // must be declared before the lib initialization // Lib instantiate PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper); // globals bool bDHTstarted; // flag to indicate we started acquisition // This wrapper is in charge of calling // must be defined like this for the lib work void dht_wrapper() { DHT.isrCallback(); } void setup() { startMills = millis(); Serial.begin(115200); /* while (!Serial) { yield(); // wait for serial port to connect. } */ Serial.println(""); Serial.println("DHT Example program using DHT.acquire and DHT.acquireAndWait"); Serial.println(""); // delay 2 sec before start acquire while ( (millis() - startMills) < 2000 ) { yield(); } // blocking method acquirestatus = 0; acquireresult = DHT.acquireAndWait(1000); if ( acquireresult == 0 ) { t = DHT.getCelsius(); h = DHT.getHumidity(); d = DHT.getDewPoint(); } else { t = h = d = 0; } } void loop() { if (bDHTstarted) { acquirestatus = DHT.acquiring(); if (!acquirestatus) { acquireresult = DHT.getStatus(); if ( acquireresult == 0 ) { t = DHT.getCelsius(); h = DHT.getHumidity(); d = DHT.getDewPoint(); } bDHTstarted = false; } } if ((millis() - startMills) > REPORT_INTERVAL) { if ( acquireresult == 0 ) { Serial.println(""); Serial.print("Humidity (%): "); Serial.println(h); Serial.print("Temperature (oC): "); Serial.println(t); Serial.print("Dew Point (oC): "); Serial.println(d); } else { Serial.println("Is dht22 connected"); } startMills = millis(); // to remove lock if (acquirestatus == 1) { DHT.reset(); } if (!bDHTstarted) { // non blocking method DHT.acquire(); bDHTstarted = true; } } } |
Console output:
1 2 3 |
Humidity (%): 36.00 Temperature (oC): 24.00 Dew Point (oC): 8.00 |
De code om de DHT module uit te lezen via een website
LET OP: Gebaseerd op de Adafruit DHT.H bibliotheek!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
/* DHTServer - ESP8266 Webserver with a DHT sensor as an input Based on ESP8266Webserver, DHTexample, and BlinkWithoutDelay (thank you) Version 1.0 5/3/2014 Version 1.0 Mike Barela for Adafruit Industries */ #include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer.h> #include <DHT.h> #define DHTTYPE DHT11 #define DHTPIN 2 const char* ssid = "SSID"; const char* password = "WACHTWOORD"; ESP8266WebServer server(80); // Initialize DHT sensor // NOTE: For working with a faster than ATmega328p 16 MHz Arduino chip, like an ESP8266, // you need to increase the threshold for cycle counts considered a 1 or 0. // You can do this by passing a 3rd parameter for this threshold. It's a bit // of fiddling to find the right value, but in general the faster the CPU the // higher the value. The default for a 16mhz AVR is a value of 6. For an // Arduino Due that runs at 84mhz a value of 30 works. // This is for the ESP8266 processor on ESP-01 DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266 float humidity, temp_f; // Values read from sensor String webString=""; // String to display // Generally, you should use "unsigned long" for variables that hold time unsigned long previousMillis = 0; // will store last temp was read const long interval = 2000; // interval at which to read sensor void handle_root() { server.send(200, "text/plain", "Hello from the weather esp8266, read from /temp or /humidity"); delay(100); } void gettemperature() { // Wait at least 2 seconds seconds between measurements. // if the difference between the current time and last time you read // the sensor is bigger than the interval you set, read the sensor // Works better than delay for things happening elsewhere also unsigned long currentMillis = millis(); if(currentMillis - previousMillis >= interval) { // save the last time you read the sensor previousMillis = currentMillis; // Reading temperature for humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor) humidity = dht.readHumidity(); // Read humidity (percent) temp_f = dht.readTemperature(true); // Read temperature as Fahrenheit // Check if any reads failed and exit early (to try again). if (isnan(humidity) || isnan(temp_f)) { Serial.println("Failed to read from DHT sensor!"); return; } } } void setup(void) { // You can open the Arduino IDE Serial Monitor window to see what the code is doing Serial.begin(115200); // Serial connection from ESP-01 via 3.3v console cable dht.begin(); // initialize temperature sensor // Connect to WiFi network WiFi.begin(ssid, password); Serial.print("\n\r \n\rWorking to connect"); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("DHT Weather Reading Server"); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); server.on("/", handle_root); server.on("/temp", [](){ // if you add this subdirectory to your webserver call, you get text below :) gettemperature(); // read sensor webString="Temperature: "+String((int)temp_f)+" F"; // Arduino has a hard time with float to string server.send(200, "text/plain", webString); // send to someones browser when asked }); server.on("/humidity", [](){ // if you add this subdirectory to your webserver call, you get text below :) gettemperature(); // read sensor webString="Humidity: "+String((int)humidity)+"%"; server.send(200, "text/plain", webString); // send to someones browser when asked }); server.begin(); Serial.println("HTTP server started"); } void loop(void) { server.handleClient(); } |
Console output:
1 2 3 4 5 |
Working to connect....... DHT Weather Reading Server Connected to SSID IP address: 192.168.0.117 HTTP server started |
Server HTTP output:
192.168.0.117
Hello from the weather esp8266, read from /temp or /humidity
192.168.0.117/temp
Temperature: 70 F
192.168.0.117/humidity
Humidity: 57%
Bron:
learn.adafruit.com