Jump to content
  • Entries

    16114
  • Comments

    7952
  • Views

    86382267

Contributors to this blog

  • HireHackking 16114

About this blog

Hacking techniques include penetration testing, network security, reverse cracking, malware analysis, vulnerability exploitation, encryption cracking, social engineering, etc., used to identify and fix security flaws in systems.

In this tutorial, you will learn how to use the DHT11 temperature and humidity sensor on a NodeMCU. And understand how the temperature and humidity sensor works, and how to view the values read by the sensor through the serial monitor.

Equipment List

NodeMCU development board one DHT11 temperature and humidity sensor one 请输入图片描述

(DHT11 module)

请输入图片描述

(NodeMcu board)

DHT11 detects water vapor by measuring the resistance between two electrodes. A moisturizing substrate with electrodes on the surface of the humidity detection component.

When water vapor is absorbed by the substrate, ions are released by the substrate, and this process will increase the conductivity between the electrodes.

The resistance change between the two electrodes is proportional to the relative humidity.

Higher relative humidity reduces the resistance between electrodes, while lower relative humidity increases the resistance between electrodes.

How to connect DHT11 on NodeMCU

请输入图片描述

Connecting the DHT11 to the NodeMCU is simple, but the connection method varies depending on whether you are using a 3-pin sensor or a 4-pin sensor.

The connection method is as follows:

The +3V pin of the nodemcu is marked on the DHT11 with the (+ or VCC) pin.

DHT11 is marked with the (S or OUT) pin to connect to the D4V pin of nodemcu.

The GND pin of the nodemcu connected to the GND pin marked with the (- or GND) pin on DHT11.

The code is as follows

#include SimpleDHT.h

//for DHT11,

//VCC: 5V or 3V

//GND: GND

//DATA: 2

int pinDHT11=2;

SimpleDHT11 dht11(pinDHT11);

void setup() {

Serial.begin(115200);

}

void loop() {

//start working.

Serial.println('=================================');

Serial.println('Sample DHT11.');

//read without samples.

byte temperature=0;

byte humidity=0;

int err=SimpleDHTErrSuccess;

if ((err=dht11.read(temperature, humidity, NULL)) !=SimpleDHTErrSuccess) {

Serial.print('Read DHT11 failed, err='); Serial.println(err); delay(1000);

return;

}

Serial.print('Sample OK:');

Serial.print((int)temperature); Serial.print(' *C, ');

Serial.print((int)humidity); Serial.println(' H');

//DHT11 sampling rate is 1HZ.

delay(1500);

}github address

User Demo

请输入图片描述

DHT11+WEB version

Let's take a look at the effect first 请输入图片描述

Feedback the DHT11 data through the web server.

Development ideas

We first need to install two libraries DHT and Adafruit Unified Sensor 请输入图片描述 请输入图片描述

The code is as follows:

#include ESP8266WiFi.h

#include ESP8266WebServer.h

#include 'DHT.h'

//Uncomment one of the lines below for whatever DHT sensor type you're using!

#define DHTTYPE DHT11 //DHT 11

//#define DHTTYPE DHT21 //DHT 21 (AM2301)

//#define DHTTYPE DHT22 //DHT 22 (AM2302), AM2321

/*Put your SSID Password*/

const char* ssid='kali'; //Enter SSID here

const char* password='12345678900'; //Enter Password here

ESP8266WebServer server(80);

//DHT Sensor

uint8_t DHTPin=D4;

//Initialize DHT sensor.

DHT dht(DHTPin, DHTTYPE);

float Temperature;

float Humidity;

void setup() {

Serial.begin(115200);

delay(100);

pinMode(DHTPin, INPUT);

dht.begin();

Serial.println('Connecting to ');

Serial.println(ssid);

//connect to your local wi-fi network

WiFi.begin(ssid, password);

//check wi-fi is connected to wi-fi network

while (WiFi.status() !=WL_CONNECTED) {

delay(1000);

Serial.print('.');

}

Serial.println('');

Serial.println('WiFi connected.');

Serial.print('Got IP:'); Serial.println(WiFi.localIP());

server.on('/', handle_OnConnect);

server.onNotFound(handle_NotFound);

server.begin();

Serial.println('HTTP server started');

}

void loop() {

server.handleClient();

}

void handle_OnConnect() {

Temperature=dht.readTemperature(); //Gets the values of the temperature

Humidity=dht.readHumidity(); //Gets the values of the humidity

server.send(200, 'text/html', SendHTML(Temperature, Humidity));

}

void handle_NotFound(){

server.send(404, 'text/plain', 'Not found');

}

String SendHTML(float Temperaturestat,float Humiditystat){

String ptr='!DOCTYPE html html\n';

ptr +='head meta name=\'viewport\' content=\'width=device-width, initial-scale=1.0, user-scalable=no\'\n';

ptr +='link href=\'https://fonts.googleapis.com/css?family=Open+Sans:300,400,600\' rel=\'stylesheet\'\n';

ptr +='meta charset=\'UTF-8\'\n';

ptr +='titlePriess's nest—smart thermometer/title\n';

ptr +='stylehtml { font-family: 'Open Sans', sans-serif; display: block; margin: 0px auto; text-align: center;color: #333333;}\n';

ptr +='body{margin-top: 50px;}\n';

ptr +='h1 {margin: 50px auto 30px;}\n';

ptr +=' .wd {margin: 50px auto 30px;width: auto;color: #f39c12}\n';

ptr +=' .wd1 {margin: 50px auto 30px;width: auto;color: #3498db}\n';

ptr +='.side-by-side{display: inline-block;vertical-align: middle;position: relative;}\n';

ptr +='.humidity-icon{background-color: #3498db;width: 30px;height: 30px;border-radius: 50%;line-height: 36px;}\n';

ptr +='.humidity-text{font-weight: 600;padding-left: 15px;font-size: 19px;width: 160px;text-align: left;}\n';

ptr +='.humidity{font-weight: 300;font-size: 60px;color: #3498db;}\n';

ptr +='.temperature-icon{background-color: #f39c12;width: 30px;height: 30px;border-radius: 50%;line-height: 40px;}\n';

ptr +='.temperature-text{font-weight: 600;padding-left: 15px;font-size: 19px;width: 160px;text-align: left;}\n';

ptr +='.temperature{font-weight: 300;font-size: 60px;color: #f39c12;}\n';

ptr +='.superscript{font-size: 17px;font-weight: 600;position: absolute;right: -20px;top: 15px;}\n';

ptr +='.data{padding: 10px;}\n';

ptr +='/style\n';

ptr +='/head\n';

ptr +='body\n';

ptr +='div id=\'webpage\'\n';

ptr +='h1 indoor greenhouse detection system/h1\n';

ptr +='div class=\'data\'\n';

ptr +='div class=\'side-by-side temperature-icon\'\n';

ptr +='svg version=\'1.1\' id=\'Layer_1\' xmlns=\'http://www.w3.org/2000/svg\' xmlns:xlink=\'http://www.w3.org/1999/xlink\' x=\'0px\' y=\'0px\'\n';

ptr +='width=\'9.915px\' height=\'22px\' viewBox=\'0 0 9.915 22\' enable-background=\'new 0 0 9.915 22\' xml:space=\'preserve\'\n';

ptr +='path fill=\'#FFFFFF\' d=\'M3.498,0.53c0.377-0.331,0.877-0.501,1.374-0.527C5.697-0.04,6.522,0.421,6.924,1.142\n';

ptr +='c0.237,0.399,0.315,0.871,0.311,1.33C7.229,5.856,7.245,9.24,7.227,12.625c1.019,0.539,1.855,1.424,2.301,2.491\n';

ptr +='c0.491,1.163,0.518,2.514,0.062,3.693c-0.414,1.102-1.24,2.038-2.276,2.594c-1.056,0.583-2.331,0.743-3.501,0.463\n';

ptr +='c-1.417-0.323-2.659-1.314-3.3-2.617C0.014,18.26-0.115,17.104,0.1,16.022c0.296-1.443,1.274-2.717,2.58-3.394\n';

ptr +='c0.013-3.44,0-6.881,0.007-10.322C2.674,1.634,2.974,0.955,3.498,0.53z\'/\n';

ptr +='/svg\n';

ptr +='/div\n';

ptr +='div class=\'side-by-side temperature-text\'Indoor temperature: /div\n';

ptr +='div class=\'side-by-side temperature\'';

ptr +=(int)Temperaturestat;

ptr +='span class=\'superscript\'°C/span/div\n';

ptr +='/div\n';

ptr +='div class=\'data\'\n';

ptr +='div class=\'side-by-side humidity-icon\'\n';

ptr +='svg version=\'1.1\' id=\'Layer_2\' xmlns=\'http://www.w3.org/2000/svg\' xmlns:xlink=\'http://www.w3.org/1999/xlink\' x=\'0px\' y=\'0px\'\n\'; width=\'12px\' height=\'17.955px\' viewBox=\'0 0 13 17.955\' enable-background=\'new 0 0 13 17.955\' xml:space=\'preserve\'\n';

ptr +='path fill=\'#FFFFFF\' d=\'M1.819,6.217C3.139,4.064,6.5,0,6.5,0s3.363,4.064,4.681,6.217c1.793,2.926,2.133,5.05,1.571,7.057\n';

ptr +='c-0.438,1.574-2.264,4.681-6.252,4.681c-3.988,0-5.813-3.107-6.252-4.681C-0.313,11.267,0.026,9.143,1.819,6.217\'/path\n';

ptr +='/svg\n';

ptr +='/div\n';

ptr +='div class=\'side-by-side humidity-text\'Indoor humidity: /div\n';

ptr +='div class=\'side-by-side humidity\'';

ptr +=(int)Humiditystat;

ptr +='span class=\'superscript\'%/span/div\n';

ptr +='/div\n';

//Define the temperature variable and assign the value to use for logical judgment.

int wd=Temperaturestat ;

if (wd=30){

ptr +='div class=\'wd\'Hi~ Baby, the weather is so hot today, so inject heat prevention! /div\n';

}

if (29=wdwd20){

ptr +='div class=\'wd1\'Hi~ Baby, the weather is nice today, so let's have fun! /div\n';

}

if (wd10){

ptr +='div class=\'side-by-side humidity-text\'Hi~ The weather is cold today, so wear more clothes and be careful to catch a cold! /div\n';

}

ptr +='/div\n';

ptr +='/body\n';

ptr +='/html\n';

return ptr;

}I added a warm reminder based on the source code! When the temperature is in different ranges, different greetings are prompted.

//Define the temperature variable and assign the value to use for logical judgment.

int wd=Temperaturestat ;

if (wd=30){

ptr +='div class=\'wd\'Hi~ Baby, the weather is so hot today, so inject heat prevention! /div\n';

}

if (29=wdwd10){

ptr +='div class=\'wd1\'Hi~ Baby, the weather is nice today, so let's have fun! /div\n';

}

if (wd=10){

ptr +='div class=\'side-by-side humidity-text\'Hi~ The weather is cold today, so wear more clothes and be careful to catch a cold! /div\n';

} 请输入图片描述