ESP32 Weather Forecast System Using DHT and Firebase

by sarful in Circuits > Arduino

23 Views, 0 Favorites, 0 Comments

ESP32 Weather Forecast System Using DHT and Firebase

1 DHT22 - Digital Temperature and Humidity Sensors.png

In this project, the ESP32 will collect temperature and humidity data using a DHT sensor (DHT11 or DHT22). The data will then be uploaded to Firebase, a cloud platform, where it can be stored and monitored. This system can be extended to trigger automations, generate weather forecasts, or perform other actions based on the data.

Supplies

Components Required:

  1. ESP32 – Microcontroller with built-in Wi-Fi.
  2. DHT11 or DHT22 – Temperature and humidity sensor.
  3. Jumper Wires – For connecting the components.
  4. Breadboard – For prototyping.
  5. Resistor (4.7kΩ) – Pull-up resistor for the DHT sensor.
  6. Power Supply – To power the ESP32.
  7. Arduino IDE – Platform for coding the ESP32.
  8. Firebase Account – Cloud storage and database.


Wiring the Components:

  1. DHT Sensor to ESP32:
  2. VCC of DHT → 3.3V pin on ESP32 (Make sure you're using 3.3V, not 5V).
  3. GND of DHT → GND pin on ESP32.
  4. Data Pin of DHT → Pin D4 on ESP32.
  5. 4.7kΩ Resistor: Place a resistor between VCC and Data Pin of DHT to enable proper communication.

Setting Up Firebase:

Step 1: Create a Firebase Account

  1. Go to Firebase and sign up or log in.
  2. Once logged in, you’ll be directed to the Firebase Console.

Step 2: Create a Firebase Project

  1. In the Firebase Console, click on "Add Project" and give it a unique name.
  2. Select Google Analytics options according to your needs, and click Create Project.
  3. Once the project is created, click on Continue.

Step 3: Set Up Firebase Realtime Database

  1. In the Firebase Console, find and click on the Realtime Database option from the left sidebar.
  2. In the Data tab, click Create Database.
  3. Select the start in test mode for now (to allow the system to write and read data freely).
  4. Set the location of your database (you can select your nearest location for faster data access).
  5. The database will be created, and you'll be able to view the JSON structure where data will be stored.

Step 4: Set Database Rules for Access

By default, Firebase's database is locked for security reasons. However, for testing purposes, you can modify the rules to allow public read and write access.

  1. Go to the Rules tab in Realtime Database.
  2. Replace the existing rules with this code to allow public access:
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
  1. Save the rules.

Note: When you're ready to deploy in a production environment, you should change these rules to make the database more secure.

Step 5: Obtain Firebase Credentials

  1. Go to Project Settings (gear icon) in the Firebase Console.
  2. Under Your Apps, select Web and click Firebase SDK snippet.
  3. Choose Config and copy the configuration object, which includes:
  4. apiKey
  5. authDomain
  6. databaseURL
  7. projectId
  8. storageBucket
  9. messagingSenderId
  10. appId
  11. For example:
{
"apiKey": "your-api-key",
"authDomain": "your-project-id.firebaseapp.com",
"databaseURL": "https://your-project-id.firebaseio.com",
"projectId": "your-project-id",
"storageBucket": "your-project-id.appspot.com",
"messagingSenderId": "your-messaging-sender-id",
"appId": "your-app-id"
}
  1. You'll need these values later when configuring the ESP32 code.

Setting Up Arduino IDE:

  1. Install ESP32 Board Support:
  2. Open Arduino IDE and go to File → Preferences.
  3. Add the following URL in Additional Boards Manager URLs:
https://dl.espressif.com/dl/package_esp32_index.json
  1. Then go to Tools → Board → Board Manager, search for ESP32, and install it.
  2. Install Necessary Libraries:
  3. Go to Sketch → Include Library → Manage Libraries.
  4. Install the following libraries:
  5. DHT sensor library (by Adafruit)
  6. Firebase ESP32 library (by Firebase ESP32)

Complete Arduino Code:

#include <WiFi.h>
#include <FirebaseESP32.h>
#include <DHT.h>

// Firebase configuration
#define FIREBASE_HOST "your-firebase-database.firebaseio.com" // Your Firebase Realtime Database URL
#define FIREBASE_AUTH "your-firebase-auth-key" // Your Firebase Authentication Key

// WiFi credentials
const char* ssid = "your-SSID";
const char* password = "your-password";

// DHT sensor setup
#define DHTPIN 4 // Pin connected to DHT sensor
#define DHTTYPE DHT11 // DHT 11 or DHT22
DHT dht(DHTPIN, DHTTYPE);

// Firebase object
FirebaseData firebaseData;

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

// Connect to Firebase
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);

// Start DHT sensor
dht.begin();
}

void loop() {
// Read temperature and humidity values
float temperature = dht.readTemperature(); // Celsius
float humidity = dht.readHumidity(); // Percentage

// Check if readings are valid
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

// Print readings to serial monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C ");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");

// Send data to Firebase
if (Firebase.setFloat(firebaseData, "/weather/temperature", temperature)) {
Serial.println("Temperature data sent to Firebase");
} else {
Serial.println("Error sending temperature data");
}

if (Firebase.setFloat(firebaseData, "/weather/humidity", humidity)) {
Serial.println("Humidity data sent to Firebase");
} else {
Serial.println("Error sending humidity data");
}

// Delay for 10 seconds before sending new data
delay(10000);
}

Code Explanation:

  1. Wi-Fi Setup:
  2. The WiFi.begin(ssid, password) command connects the ESP32 to your Wi-Fi network. The system waits until it successfully connects to the network.
  3. DHT Sensor Setup:
  4. The DHT sensor library is used to read temperature and humidity values.
  5. dht.readTemperature() returns the current temperature in Celsius.
  6. dht.readHumidity() returns the current humidity as a percentage.
  7. Firebase Setup:
  8. The Firebase ESP32 library is used to send data to Firebase.
  9. Firebase.setFloat() uploads the temperature and humidity data to Firebase.
  10. Looping:
  11. The data is read and uploaded every 10 seconds, using delay(10000).

Testing the Project:

  1. Upload the Code:
  2. Connect your ESP32 to your computer and upload the code using Arduino IDE.
  3. Monitor the Serial Output:
  4. Open the Serial Monitor to view the temperature and humidity values that are being read from the sensor.
  5. Check Firebase:
  6. Go to your Firebase Console and navigate to the Realtime Database. You should see the temperature and humidity data being updated under the /weather/temperature and /weather/humidity nodes.

Conclusion:

With this ESP32 Weather Forecast System using DHT and Firebase, you can easily collect and store weather data on the cloud. You can later analyze this data, generate forecasts, or integrate it into other IoT projects for automation based on temperature or humidity conditions.

Affiliate Disclaimer: Some links in this article are affiliate links, meaning that I may earn a small commission if you make a purchase through them, at no additional cost to you. Thank you for supporting my work!