In this project, we’ll create a standalone web server using an ESP8266 that serves as a weather station, displaying current temperature and humidity readings from a DHT22 sensor.
The server will be programmed using Arduino IDE and can be accessed from any device with a web browser on your local network. It will use a simple HTTP setup to serve a raw HTML page that displays the latest sensor readings each time the page is refreshed.
Now, let’s go through the steps to build this weather station…
Materials
- ESP8266 (e.g., NodeMCU): A Wi-Fi microcontroller module for IoT projects
- DHT22 Temperature and Humidity Sensor: A digital sensor for measuring temperature and humidity
- Breadboard: A platform for prototyping electronics without soldering
- Jumper wires: For making electrical connections on a breadboard
- USB cable: Connects the ESP8266 to a computer for programming and power
- Resistor (4.7k ohms): Used as a pull-up
-
Computer or laptop: With Arduino IDE installed, which is required for programming the ESP8266
Building the circuit
Connect the DHT22 sensor’s:
- VCC pin to Arduino’s 3.3V pin
- GND pin to Arduino’s GND pin
- Data pin to Arduino’s D4 (GPIO2) pin
Next, place a 4.7k ohm resistor between the sensor’s VCC and data pins.
To assemble the breadboard:
- Place the ESP8266 and DHT22 on the breadboard
- Use jumper wires to make the connections as described above
The required libraries
Before uploading the code, make sure Arduino IDE is set up with the correct board definitions and libraries needed for the ESP8266 and DHT22 sensor.
- Install Arduino IDE: Download and install Arduino IDE from the official website if you haven’t already.
- Add ESP8266 board to Arduino IDE:
◦ Open Arduino IDE and go to File > Preferences.
◦ In the “Additional Board Manager URLs” field, add: http://arduino.esp8266.com/stable/package_esp8266com_index.json
◦ Go to Tools > Board > Board Manager, search for ESP8266, and install it. -
Install DHT libraries:
◦ Go to Sketch > Include Library > Manage Libraries.
◦ Search for and install the DHT sensor library by Adafruit.
◦ Also install the Adafruit Unified Sensor library.
Write the code
-
Open Arduino IDE and create a new sketch.
-
Include the necessary libraries at the top of your code:
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <DHT.h> -
Define the DHT sensor type and pin:
#define DHTPIN 2
#define DHTTYPE DHT22 -
Replace with your Wi-Fi credentials:
const char* ssid = “Your_SSID”;
const char* password = “Your_PASSWORD”; -
Upload the code:
◦ Connect the ESP8266 to your computer using a USB cable.
◦ In Arduino IDE, select the correct board and port from the Tools menu.
◦ Click the Upload button.
Power the weather station
-
Power the ESP8266:
◦ You can use a 5-V power supply or a battery pack to power ESP8266. -
Monitor serial output:
◦ Open the Serial Monitor in Arduino IDE to view the Wi-Fi connection status and IP address.
Access the weather data
-
Find the IP address:
◦ Once ESP8266 connects to Wi-Fi, it will display an IP address in the Serial Monitor. -
Access data via browser:
◦ Open a web browser and enter the IP address shown in the Serial Monitor.
◦ You should see the current temperature and humidity readings displayed.
Conclusion
By following these steps, you can build a simple and effective weather station using ESP8266 and a DHT22 sensor. This project introduces the basics of working with sensors and microcontrollers while providing real-time access to local temperature and humidity data.
You can also take the project further by adding more sensors or integrating the system with other IoT platforms to expand its functionality.
You may also like:
Filed Under: Tutorials
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.