Standalone Web Server with ESP8266 and DHT Sensor for Beginners
Objective
In this beginner-friendly guide, you’ll learn how to build a standalone web server using ESP8266 and a DHT sensor to monitor temperature and humidity data.
Project Goals
- Create a standalone web server with the ESP8266.
- Read temperature and humidity data from a DHT sensor.
- Display sensor data on a web page accessible through a browser.
Required Components
Component | Description | Amazon Link |
---|---|---|
ESP8266 (NodeMCU) | WiFi microcontroller | Buy on Amazon |
DHT11 or DHT22 Sensor | Temperature and humidity sensor | Buy on Amazon |
Jumper Wires | Wiring components for connections | Buy on Amazon |
Breadboard | For prototyping connections | Buy on Amazon |
Sensor Basics: DHT Sensor
The DHT sensor is used to measure temperature and humidity. It has three pins:
Pin | Function |
---|---|
VCC | Connects to 3.3V on ESP8266 |
GND | Connects to GND on ESP8266 |
Data | Connects to digital pin D4 on ESP8266 |
Circuit Connection
Component | Connection |
---|---|
ESP8266 | Connect to computer via micro-USB for programming |
DHT Sensor VCC | Connect to 3.3V on ESP8266 |
DHT Sensor GND | Connect to GND on ESP8266 |
DHT Sensor Data | Connect to D4 on ESP8266 |
Circuit Connection Analysis
The ESP8266 acts as a web server, which collects temperature and humidity data from the DHT sensor and serves it to a connected web browser over WiFi.
Safety Tips
- Ensure that the ESP8266 operates at 3.3V to avoid damage.
- Handle the sensor carefully to prevent physical damage.
Arduino Programming Section
Arduino Syntax
- #include <ESP8266WiFi.h>: Library for WiFi functionality.
- #include <ESP8266WebServer.h>: Library for web server functionality.
- #include <DHT.h>: Library for DHT sensor functionality.
Arduino Board and Library Configuration
Before uploading the code, ensure that you have installed the following libraries in the Arduino IDE:
- ESP8266 Board Package
- ESP8266WiFi
- ESP8266WebServer
- DHT Sensor Library
Arduino Code
#include
#include
#include
#define DHTPIN D4 // Pin connected to DHT sensor
#define DHTTYPE DHT11 // Sensor type: DHT11 or DHT22
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
DHT dht(DHTPIN, DHTTYPE);
ESP8266WebServer server(80);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("Connected to WiFi");
Serial.println(WiFi.localIP());
dht.begin();
server.on("/", handleRoot);
server.begin();
}
void loop() {
server.handleClient();
}
void handleRoot() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
server.send(500, "text/plain", "Failed to read from DHT sensor!");
return;
}
String html = "ESP8266 DHT Web Server
";
html += "Temperature: " + String(t) + " °C
";
html += "Humidity: " + String(h) + " %
";
html += "";
server.send(200, "text/html", html);
}
Steps to Upload
- Connect the ESP8266 to your computer using a USB cable.
- Open the Arduino IDE and select the "NodeMCU 1.0" board.
- Enter the correct WiFi credentials in the code.
- Upload the code to the ESP8266.
Run and Check Output
- Open the Serial Monitor to get the IP address of the ESP8266.
- Enter the IP address in a web browser to see temperature and humidity data.
Troubleshooting Tips
- If the ESP8266 fails to connect, check WiFi credentials and signal strength.
- If sensor readings are invalid, ensure the wiring is correct and secure.
Suggestion for Beginners
Start by testing simple code to read the DHT sensor values before integrating with the ESP8266 web server setup.
Book Recommendation
Arduino Programming for Absolute Beginners – This book offers step-by-step projects for beginners, making it ideal for building foundational knowledge in Arduino programming.
For more tutorials, visit MechatronicsLab.net for resources on Arduino, ESP8266, ESP32, and Raspberry Pi projects.
No comments