ESP32 Weather Data Logger With DHT Sensor (ThingSpeak + Google Sheets)
by sarful in Circuits > Arduino
42 Views, 2 Favorites, 0 Comments
ESP32 Weather Data Logger With DHT Sensor (ThingSpeak + Google Sheets)
In this project, you will create an ESP32-based weather data logger that reads temperature and humidity from a DHT22 sensor (connected to Pin D4). The data is uploaded to ThingSpeak for real-time visualization and logged into Google Sheets for storage and further analysis. This project is perfect for monitoring weather conditions over time and analyzing the data using Google Sheets' built-in capabilities.
Supplies
Components Required:
- ESP32 – Microcontroller with Wi-Fi capabilities.
- DHT22 Sensor – Temperature and humidity sensor.
- Jumper Wires – For connecting components.
- Breadboard – For prototyping.
- 4.7kΩ Resistor – Pull-up resistor for the DHT22 data pin.
- Power Supply – To power the ESP32.
- ThingSpeak Account – Platform to visualize the data.
- Google Sheets Account – For storing the data.
Wiring the Components:
- DHT22 to ESP32:
- VCC of DHT22 → 3.3V on ESP32.
- GND of DHT22 → GND on ESP32.
- Data Pin of DHT22 → Pin D4 on ESP32.
- 4.7kΩ Resistor between VCC and Data Pin of the DHT22.
Setting Up ThingSpeak for Data Upload
Step 1: Create a ThingSpeak Account
- Sign up at ThingSpeak:
- Go to ThingSpeak and sign up for a free account if you don't already have one. It will require basic details such as your name, email, and password.
- Create a New Channel:
- After logging in, click on the Channels tab in the navigation bar.
- Click on + New Channel to create a new channel for storing your weather data.
- Configure Channel Fields:
- Field 1: Name it "Temperature (°C)" for storing the temperature data.
- Field 2: Name it "Humidity (%)" for storing the humidity data.
- You can leave the other options as default for now.
- Save your channel.
- Get the Channel ID and Write API Key:
- After saving the channel, ThingSpeak will provide you with a Channel ID and a Write API Key.
- Note these down. You’ll need these to upload data to your ThingSpeak channel.
- Set Up Visualization (Optional):
- Once your channel is created, you can create a Dashboard in ThingSpeak to visualize the data. This will allow you to monitor the temperature and humidity in real-time.
- You can go to the Visualization tab and create widgets like Gauge for temperature and humidity.
Setting Up Google Sheets for Data Logging
Step 1: Create a Google Sheet
- Open Google Sheets by going to Google Sheets.
- Create a new spreadsheet:
- Name the sheet "Weather Data Logger" or something similar to indicate it will be used for logging weather data.
- In the first row, create the following column headers:
- Column A: Timestamp – For storing the time at which the data was logged.
- Column B: Temperature (°C) – For storing the temperature readings from the DHT22 sensor.
- Column C: Humidity (%) – For storing the humidity readings from the DHT22 sensor.
- This spreadsheet will now be the destination for your weather data.
Step 2: Create a Google Apps Script for Data Logging
To automatically pull data from ThingSpeak and insert it into your Google Sheets, we need to create a Google Apps Script.
- Open Apps Script:
- Go to Extensions → Apps Script in your Google Sheets document.
- This will open the script editor where you can write a custom script to fetch data from ThingSpeak.
- Write the Apps Script Code:
- Delete any default code and replace it with the following script:
- Replace the placeholders:
- Replace YOUR_CHANNEL_ID with your ThingSpeak Channel ID.
- Replace YOUR_API_KEY with your ThingSpeak Write API Key.
- Save the Script:
- After writing the script, click File → Save and name your script (e.g., "ThingSpeakLogger").
Step 3: Set Up Triggers to Run the Script Automatically
- Click on the Clock icon in the toolbar, or go to Triggers from the Apps Script editor.
- Click + Add Trigger at the bottom right.
- Select the following options:
- Choose function: logWeatherData
- Choose deployment: Head
- Event source: Time-driven
- Type of time-based trigger: Set this to run every 10 minutes (or any other interval you prefer).
- Click Save.
This setup will now automatically fetch the latest weather data from ThingSpeak and log it into Google Sheets at the interval you set.
Complete Arduino Code
Here is the complete Arduino code that connects the ESP32 to Wi-Fi, reads data from the DHT22 sensor, uploads the data to ThingSpeak, and logs it into Google Sheets via Apps Script.
Steps to Test the Project:
- Upload the Code:
- Connect the ESP32 to your computer via USB.
- Open Arduino IDE, select the correct board and port under the Tools menu.
- Upload the code to the ESP32.
- Monitor the Serial Output:
- Open the Serial Monitor in Arduino IDE (set the baud rate to 115200) to see the status of the Wi-Fi connection, temperature, and humidity readings.
- Check ThingSpeak:
- Go to your ThingSpeak Channel to monitor the uploaded data. You should see temperature and humidity values being updated every 15 seconds.
- Check Google Sheets:
- Open your Google Sheets document. The data should start populating with the timestamp, temperature, and humidity values logged at regular intervals as per the trigger you set up.
Conclusion:
This project successfully sets up an ESP32 Weather Data Logger with a DHT22 sensor. The data is uploaded to ThingSpeak for real-time monitoring and visualization, while also being logged into Google Sheets for storage and analysis. This system can be used for various applications, such as home automation, environmental monitoring, and data analysis.