ESP8266 Nodemcu Temperature Monitoring Using DHT11 on a Local Webserver | Get Room Temperature & Humidity on Your Browser

by Utsource in Circuits > Arduino

8475 Views, 5 Favorites, 0 Comments

ESP8266 Nodemcu Temperature Monitoring Using DHT11 on a Local Webserver | Get Room Temperature & Humidity on Your Browser

esp8266-dht.png
Hi guys today we will making a humidity & temperature monitoring system using ESP 8266 NODEMCU & DHT11 temperature sensor. Temperature and humidity will obtained from DHT11 Sensor & it can be seen on a browser which webpage will be managed by esp 8266 by hosting it on a local Webserver.

Things You Need

images(10).jpg
images(9).jpg

Circuit

esp8266-dht.png
The circuit is very easy connect everything According as shown in schmatics

Get the Libraries

adafruit_dht_library(1).png
adafruit_unified_sensor_library(1).png
Open your Arduino IDE and go to Sketch > Include Library > Manage Libraries. The Library Manager should open.

Search for “DHT” on the Search box and install the DHT library from Adafruit.


After installing the DHT library from Adafruit, type “Adafruit Unified Sensor” in the search box. Scroll all the way down to find the library and install it.


After installing the libraries, restart your Arduino IDE.

Code

select-esp8266-board-arduino-ide.png
After doing above things upload the following code to ESP8266 nodemcu ( please select proper port & board ) & before uploading the code please put ssid & password of your wifi in the code :


// Including the ESP8266 WiFi library
#include
#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

// Replace with your network details
const char* ssid = "YOUR_NETWORK_NAME";
const char* password = "YOUR_NETWORK_PASSWORD";

// Web Server on port 80
WiFiServer server(80);

// DHT Sensor
const int DHTPin = 5;
// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);

// Temporary variables
static char celsiusTemp[7];
static char fahrenheitTemp[7];
static char humidityTemp[7];

// only runs once on boot
void setup() {
// Initializing serial port for debugging purposes
Serial.begin(115200);
delay(10);

dht.begin();

// Connecting to WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");

// Starting the web server
server.begin();
Serial.println("Web server running. Waiting for the ESP IP...");
delay(10000);

// Printing the ESP IP address
Serial.println(WiFi.localIP());
}

// runs over and over again
void loop() {
// Listenning for new clients
WiFiClient client = server.available();

if (client) {
Serial.println("New client");
// bolean to locate when the http request ends
boolean blank_line = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();

if (c == '\n' && blank_line) {
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
strcpy(celsiusTemp,"Failed");
strcpy(fahrenheitTemp, "Failed");
strcpy(humidityTemp, "Failed");
}
else{
// Computes temperature values in Celsius + Fahrenheit and Humidity
float hic = dht.computeHeatIndex(t, h, false);
dtostrf(hic, 6, 2, celsiusTemp);
float hif = dht.computeHeatIndex(f, h);
dtostrf(hif, 6, 2, fahrenheitTemp);
dtostrf(h, 6, 2, humidityTemp);
// You can delete the following Serial.print's, it's just for debugging purposes
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.print(" *F");
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");
}
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// your actual web page that displays temperature and humidity
client

Get the IP

Arduino-IDE.png
You view the temperature & humidity we need to get the IP of webpage. So for that make sure your esp8266 is connected to your PC and then open the serial monitor and on the serial monitor you can see the IP of your ESP8266 webserver webpage.

Check Your Temperature & Humidity on the Browser

web-server(1).png
So after getting the IP of your ESP8266 nodemcu , just open browser in PC or Mobile but make sure your PC/mobile is connected with same network as your Nodemcu/ESP8266 and then go to your browser ( if you are using mobile please use default browser i.e. for Android use chrome ) and then type the IP we got in previous step and the local webpage will b display with humidity & temperature as mine shown in image.
So have fun making your room temperature & humidity monitor.