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
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:
- ESP32 – Microcontroller with built-in Wi-Fi.
- DHT11 or DHT22 – Temperature and humidity sensor.
- Jumper Wires – For connecting the components.
- Breadboard – For prototyping.
- Resistor (4.7kΩ) – Pull-up resistor for the DHT sensor.
- Power Supply – To power the ESP32.
- Arduino IDE – Platform for coding the ESP32.
- Firebase Account – Cloud storage and database.
Wiring the Components:
- DHT Sensor to ESP32:
- VCC of DHT → 3.3V pin on ESP32 (Make sure you're using 3.3V, not 5V).
- GND of DHT → GND pin on ESP32.
- Data Pin of DHT → Pin D4 on ESP32.
- 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
- Go to Firebase and sign up or log in.
- Once logged in, you’ll be directed to the Firebase Console.
Step 2: Create a Firebase Project
- In the Firebase Console, click on "Add Project" and give it a unique name.
- Select Google Analytics options according to your needs, and click Create Project.
- Once the project is created, click on Continue.
Step 3: Set Up Firebase Realtime Database
- In the Firebase Console, find and click on the Realtime Database option from the left sidebar.
- In the Data tab, click Create Database.
- Select the start in test mode for now (to allow the system to write and read data freely).
- Set the location of your database (you can select your nearest location for faster data access).
- 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.
- Go to the Rules tab in Realtime Database.
- Replace the existing rules with this code to allow public access:
- 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
- Go to Project Settings (gear icon) in the Firebase Console.
- Under Your Apps, select Web and click Firebase SDK snippet.
- Choose Config and copy the configuration object, which includes:
- apiKey
- authDomain
- databaseURL
- projectId
- storageBucket
- messagingSenderId
- appId
- For example:
- You'll need these values later when configuring the ESP32 code.
Setting Up Arduino IDE:
- Install ESP32 Board Support:
- Open Arduino IDE and go to File → Preferences.
- Add the following URL in Additional Boards Manager URLs:
- Then go to Tools → Board → Board Manager, search for ESP32, and install it.
- Install Necessary Libraries:
- Go to Sketch → Include Library → Manage Libraries.
- Install the following libraries:
- DHT sensor library (by Adafruit)
- Firebase ESP32 library (by Firebase ESP32)
Complete Arduino Code:
Code Explanation:
- Wi-Fi Setup:
- The WiFi.begin(ssid, password) command connects the ESP32 to your Wi-Fi network. The system waits until it successfully connects to the network.
- DHT Sensor Setup:
- The DHT sensor library is used to read temperature and humidity values.
- dht.readTemperature() returns the current temperature in Celsius.
- dht.readHumidity() returns the current humidity as a percentage.
- Firebase Setup:
- The Firebase ESP32 library is used to send data to Firebase.
- Firebase.setFloat() uploads the temperature and humidity data to Firebase.
- Looping:
- The data is read and uploaded every 10 seconds, using delay(10000).
Testing the Project:
- Upload the Code:
- Connect your ESP32 to your computer and upload the code using Arduino IDE.
- Monitor the Serial Output:
- Open the Serial Monitor to view the temperature and humidity values that are being read from the sensor.
- Check Firebase:
- 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!