Maker Pro
ESP8266

ESP8266 ESP-01 Web Server with DHT11 Sensor

July 07, 2022 by AFZAL REHMANI
Share
banner

In this tutorial, we are going to make ESP8266 ESP-01 Web Server with DHT11 Sensor

Introduction

In this tutorial, we are going to make a web server using “ESP8266 ESP01 and DHT11 Sensor”. The DHT11 is a low-cost yet effective temperature and humidity sensor. It makes it possible to estimate the temperature and humidity levels in the surrounding environment. . Additionally, the humidity levels range from 20% to 80%. Additionally, the sensor only requires a very low voltage range of three to five volts to function. It uses 2.5mA of current as well.

PCBWay commits to meeting the needs of its customers from different industries in terms of quality, delivery, cost-effectiveness, and any other demanding requests. As one of the most experienced PCB manufacturers in China. They pride themselves to be your best business partners as well as good friends in every aspect of your PCB needs. 

The internal structure of the DHT11 sensor contains the thermistor and a humidity sensing element. The thermistor usually changes its resistance with the temperature change. Hence, the resistance decreases with the increase in temperature. On the other side, the humidity component has two electrodes that have a substrate between them. As the resistance gets changed, the conductivity of this substrate also gets changed.

Circuit Diagram

ESP8266-Temperature-Humidity-Webserver-with-a-DHT11-sensor.jpg

ESP8266 Code

#include <ESP8266WiFi.h>
#include "DHT.h"

#define DHTPIN 2        // what digital pin we're connected to
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);

const char* ssid     = "Circuits DIY"; // Your ssid
const char* password = "03433212601"; // Your Password
WiFiServer server(80);

void setup() {
  
// Start DHT11  
dht.begin();  
Serial.begin(115200);
delay(10);
Serial.println();

// Connect to WiFi network
WiFi.mode(WIFI_STA);
Serial.println();
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");

// Start the server
server.begin();
Serial.println("Server started");

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

void loop() {

 float h = dht.readHumidity();
 float t = dht.readTemperature(); // Read temperature as Celsius (the default)
 float f = dht.readTemperature(true); // Read temperature as Fahrenheit (isFahrenheit = 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!");
 return;
 }
   
 Serial.print("Humidity % : ");
 Serial.println(h);
 Serial.print("Temperature *C: ");
 Serial.println(t);
 Serial.print("Temperature *F: ");
 Serial.println(f);

WiFiClient client = server.available();
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");  // the connection will be closed after completion of the response
client.println("Refresh: 5");  // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE html>");
client.println("<html xmlns='http://www.w3.org/1999/xhtml'>");
client.println("<head>\n<meta charset='UTF-8'>");
client.println("<title>ESP8266 Temperature & Humidity DHT11 Sensor</title>");
client.println("</head>\n<body>");
client.println("<H2>ESP8266 & DHT11 Sensor</H2>");
client.println("<H3>Humidity / Temperature</H3>");
client.println("<pre>");
client.print("Humidity (%)         : ");
client.println((float)h, 2);
client.print("Temperature (°C)  : ");
client.println((float)t, 2);
client.print("Temperature (°F)  : ");
client.println((float)f, 2);
//client.print("Temperature (°K)  : ");
//client.println(Kelvin(temp), 2);
client.println("</pre>");
client.println("<H3>www.Circuits-DIY.com</H3>");
client.print("</body>\n</html>");
delay(2000);
}

Related Content

Comments


You May Also Like