Wi-Fi Weather Station with ESP8266

Difficulty Level: Intermediate

This project demonstrates how to build a Wi-Fi-enabled weather station using an ESP8266 module and a DHT22 sensor to measure temperature and humidity. The data will be displayed on a web page hosted by the ESP8266.

Components Required

Wiring the Circuit

Connect the components to the ESP8266 as follows:

Code Explanation

The following code will read the temperature and humidity from the DHT22 sensor and display the data on a web interface.

Arduino Code


#include 
#include 

// DHT sensor settings
#define DHTPIN D2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

// Wi-Fi settings
const char* ssid = "your-SSID";
const char* password = "your-PASSWORD";

// Create a Wi-Fi server
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  dht.begin();

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to Wi-Fi...");
  }
  Serial.println("Connected to Wi-Fi");
  server.begin();
}

void loop() {
  // Wait for client connection
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  Serial.println("New client connected");
  
  // Read sensor data
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  // Send HTML response to client
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println();
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
  client.println("<head><title>ESP8266 Weather Station</title></head>");
  client.println("<body>");
  client.println("<h1>Wi-Fi Weather Station</h1>");
  client.print("<p>Temperature: ");
  client.print(temperature);
  client.println("°C</p>");
  client.print("<p>Humidity: ");
  client.print(humidity);
  client.println("%</p>");
  client.println("</body></html>");
  
  delay(1000);
}
        

How the Code Works

The code starts by including the necessary libraries for Wi-Fi and the DHT sensor. You need to define the data pin where the DHT22 is connected and specify its type as DHT22. The Wi-Fi credentials (SSID and password) are stored as constants.

In the setup() function, the ESP8266 connects to the Wi-Fi network, and the DHT sensor is initialized. Once the ESP8266 is connected, it starts a web server on port 80.

In the loop() function, the ESP8266 listens for incoming client connections. When a client connects, it reads the temperature and humidity from the DHT22 sensor and sends the data as an HTML response. The data is displayed on a simple web page, showing both temperature and humidity values.

Testing the Weather Station

Upload the code to your ESP8266 and open the Serial Monitor. After the ESP8266 connects to your Wi-Fi, it will display its local IP address. Type this IP address into your web browser, and you will see the temperature and humidity readings displayed in real time.

Conclusion

With this project, you've built a Wi-Fi-enabled weather station that reads temperature and humidity from a DHT22 sensor and displays the data on a web page. This basic setup can be expanded by sending the data to cloud services, displaying the data on an LCD, or adding additional sensors like a barometer.

Next Steps: Consider integrating this system with cloud services like Blynk or ThingSpeak to remotely monitor your weather station data!