Вы находитесь на странице: 1из 6

ESP8266 DHT11 Humidity Temperature

Data logger
January 25, 2019 ESP8266, Internet of Things, IoT Projects, IoT Tutorials dht11,
ESP8266, NodeMCU
In this tutorial we are interfacing DHT11 or DHT22 Humidity temperature sensor with ESP8266
NodeMCU. We are making complete Humidity and Temperature data logger with DHT11 and
NodeMCU. Data logger web server with real time graphs and tables, mostly seen on thingspeak.
But this time we are putting complete graphs and tables all inside NodeMCU without using
thingspeak. For this we need graphs library that we are using from chartsjs.org an open source Chart
drawing library and Knowledge of alax, html and javascript.

The DHT11 (or DHT22 and similar) are cheap temperature and humidity sensors. The communicate
with a ESP8266 is over a single wire, but unfortunately it is not compatible with the 1-Wire protocol
defined by Dallas Semiconductors.

The electric connection to the NodeMCU is very simple, as the DHT series can be powered direct with
3.3V. Only 3 wires are needed: VCC, GND and the data line.

Things we need
1. NodeMCU ESP8266
2. USB Cable
3. Laptop
4. Internet and wifi router
5. DHT11 Humidity Temperature Sensor

DHT11 Connections with NodeMCU


DHT11 Sensor Specifications
DHT11 Temperature & Humidity Sensor features a temperature & humidity sensor complex with a
calibrated digital signal output. By using the exclusive digital-signal-acquisitiontechnique and
temperature & humidity sensing technology, it ensures high reliability and excellent long-term
stability. This sensor includes a resistive-type humidity measurement component and an NTC
temperature measurement component, and connects to a high-performance 8-bit microcontroller,
o ering excellent quality, fast response, anti-interference ability and cost-e ectiveness.

If you are using module it comes with a 4.7K or 10K resistor. You need external 10K or 4.7K pull
up on data pin to VCC.
DHT11 Specifications:

Low cost
3 to 5V power and I/O
2.5mA max current use during conversion (while requesting data)
Good for 20-80% humidity readings with 5% accuracy
Good for 0-50°C temperature readings ±2°C accuracy
No more than 1 Hz sampling rate (once every second)
Body size 15.5mm x 12mm x 5.5mm
4 pins with 0.1″ spacing

NodeMCU Program for Temperature


Humidity Data Logger
Before moving directly on coding install required libraries.

Go to menu Sketch >> Include Library >> Manage Library

Install DHT sensor library for ESPx by beegee library. or Download


A er installing library

To draw graphs we use chart.js from CDN. you can use char.js file directly inside the NodeMCU for this
see : AJAX Web Server

To get knowledge of real time data update without refresh read this.

Program consists of two parts one is HTML, Javascipts and Arduino Code

Arduino IDE Code For Graphs ESP8266 and Data Logging

Before directly uploading make changes in WiFi SSID and Password as per yours WiFi. most of the
things are explained inside code comments. also make index.h header file and save it near to your ino
file.

In this example we are creating web server inside ESP8266 for data logging. It has two parts one part
that displays HTML web GUI and second is that takes AJAX request and reads ADC data.

DataLogger.ino

56 Serial.print(dht.toFahrenheit(temperature), 1);
57 }
58
59 //==============================================================
60 // SETUP
61 //==============================================================
62 void setup()
63 {
64 Serial.begin(115200);
65 Serial.println();
66
67 //Ref 3: https://circuits4you.com/2019/01/25/interfacing-dht11-wit
68 // Autodetect is not working reliable, don't use the following lin
69 // dht.setup(17);
70
71 // use this instead:
72 dht.setup(DHTpin, DHTesp::DHT11); //for DHT11 Connect DHT sensor t
73 //dht.setup(DHTpin, DHTesp::DHT22); //for DHT22 Connect DHT sensor
74
75 WiFi.begin(ssid, password); //Connect to your WiFi router
76 Serial.println("");
77
78 //Onboard LED port Direction output
79 pinMode(LED,OUTPUT);
80
81 // Wait for connection
82 while (WiFi.status() != WL_CONNECTED) {
83 delay(500);
84 Serial.print(".");
85 }
86
87 //If connection successful show IP address in serial monitor
88 Serial.println("");
89 Serial.print("Connected to ");
90 Serial.println(ssid);
91 Serial.print("IP address: ");
92 Serial.println(WiFi.localIP()); //IP address assigned to your ESP
93
94 server.on("/", handleRoot); //Which routine to handle at root
95 server.on("/readADC", handleADC); //This page is called by java Sc
96
97 server.begin(); //Start server
98 Serial.println("HTTP server started");
99 }
100
101 //==============================================================
102 // LOOP

index.h HTML Code File for data logger


88 },
89 options: {
90 title: {
91 display: true,
92 text: "ADC Voltage"
93 },
94 maintainAspectRatio: false,
95 elements: {
96 line: {
97 tension: 0.5 //Smoothening (Curved) of data line
98 }
99 },
100 scales: {
101 yAxes: [{
102 ticks: {
103 beginAtZero:true
104 }
105 }]
106 }
107 }
108 });
109
110 }
111
112 //On Page load show graphs
113 window.onload = function() {
114 console.log(new Date().toLocaleTimeString());
115 };
};
116
117 //Ajax script to get ADC voltage at every 5 Seconds
118 //Read This tutorial https://circuits4you.com/2018/02/04/esp8266-aja
119
120 setInterval(function() {
121 // Call a function repetatively with 5 Second interval
122 getData();
123 }, 5000); //5000mSeconds update rate
124
125 function getData() {
126 var xhttp = new XMLHttpRequest();
127 xhttp.onreadystatechange = function() {
128 if (this.readyState == 4 && this.status == 200) {
129 //Push the data in array
130 var time = new Date().toLocaleTimeString();
131 var txt = this.responseText;
132 var obj = JSON.parse(txt); //Ref: https://www.w3schools.com/js/js_
133 ADCvalues.push(obj.ADC);
134 T l h( bj T t )

Results
A er uploading program in NodeMCU open serial monitor with 115200 baud rate and get IP address of
NodeMCU. Open it in web browser.

References:
Data Sheet: DHT11
NodeMCU PinOut
String with double quotes in quotes
Update part of webpage without refresh

Вам также может понравиться