ESP32 Chapter 10: Being A Weather Forecaster with DHT11
DHT11 Readings Transmission to Google Sheets
Hello and Welcome!
First thing first, I do hope you’re doing great and being productive in these times. If you are still reading and putting faith on my blogs, well, thank you. I wish I could keep you updated with all the upcoming projects.
Anyway, let’s get down to business. After successfully visualising the DHT11 temperature and humidity readings to charts on HTML web page, the next step is to record and store these data. This procedure is one of the most common ways to make a prediction or forecast of the upcoming events, in this case, weather forecast, by utilizing the recorded data before.
Google Sheets Data Storage
To make you understand the bigger picture of this project, you can learn further about the project overview courtesy of randomnerdtutorials.com. The easiest way to integrate with Google Sheets using the ESP32, is using a 3rd party service like IFTTT. So, the first step is to create an IFTTT account in ifttt.com.
After registering and finishing verification, create a new Applet.

Then click on “This”.

Choose Webhooks as your service and “receive a web request” trigger.


Click on “That” to select the action service.

Choose Google Sheets as your action service, connect, and choose “add row to spreadsheet” action.



Complete the action fields based on your requirements. Create action, finish, and the applet should be created and connected.



To test whether the applet works, go to webhooks documentation, modify the event by changing the JSON body, number of values, and the values themselves, and click Test It.


If successfully triggered, a green message saying “Event has been triggered” will appear.

Go to your Google Sheets on your Google Drive to check whether the testing successful.


The picture above indicates the testing mechanism is successful. Let’s move on to the next steps, the first being the schematic diagram. It is exactly the same as my previous projects, so you may have well kept it also for the upcoming projects.


Now, the codes.
#include <WiFi.h>#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>// Replace with your SSID and Password
const char* ssid = "THALIA";
const char* password = "********";// Replace with your unique IFTTT URL resource
const char* resource = "<<IFTTT-URL-Resource>>";// Maker Webhooks IFTTT
const char* server = "maker.ifttt.com";// Time to sleep
uint64_t uS_TO_S_FACTOR = 1000000; // Conversion factor for micro seconds to seconds
// sleep for 10 minutes = 600 seconds
uint64_t TIME_TO_SLEEP = 600;#define DHTPIN 4
#define DHTTYPE DHT11DHT dht(DHTPIN, DHTTYPE);void setup() {
Serial.begin(115200);
delay(2000);
dht.begin();initWifi();
makeIFTTTRequest();
#ifdef ESP32
// enable timer deep sleep
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Going to sleep now");
// start deep sleep for 3600 seconds (60 minutes)
esp_deep_sleep_start();
#else
// Deep sleep mode for 3600 seconds (60 minutes)
Serial.println("Going to sleep now");
ESP.deepSleep(TIME_TO_SLEEP * uS_TO_S_FACTOR);
#endif
}void loop() {
// sleeping so wont get here
}// Establish a Wi-Fi connection with your router
void initWifi() {
Serial.print("Connecting to: ");
Serial.print(ssid);
WiFi.begin(ssid, password);int timeout = 10 * 4; // 10 seconds
while(WiFi.status() != WL_CONNECTED && (timeout-- > 0)) {
delay(250);
Serial.print(".");
}
Serial.println("");if(WiFi.status() != WL_CONNECTED) {
Serial.println("Failed to connect, going back to sleep");
}Serial.print("WiFi connected in: ");
Serial.print(millis());
Serial.print(", IP address: ");
Serial.println(WiFi.localIP());
}// Make an HTTP request to the IFTTT web service
void makeIFTTTRequest() {
Serial.print("Connecting to ");
Serial.print(server);
WiFiClient client;
int retries = 5;
while(!!!client.connect(server, 80) && (retries-- > 0)) {
Serial.print(".");
}
Serial.println();
if(!!!client.connected()) {
Serial.println("Failed to connect...");
}
Serial.print("Request resource: ");
Serial.println(resource);// Temperature in Celsius
String jsonObject = String("{\"value1\":\"") + dht.readTemperature() + "\",\"value2\":\"" + (1.8 * dht.readTemperature() + 32) + "\",\"value3\":\"" + dht.readHumidity() + "\"}";
client.println(String("POST ") + resource + " HTTP/1.1");
client.println(String("Host: ") + server);
client.println("Connection: close\r\nContent-Type: application/json");
client.print("Content-Length: ");
client.println(jsonObject.length());
client.println();
client.println(jsonObject);
int timeout = 5 * 10; // 5 seconds
while(!!!client.available() && (timeout-- > 0)){
delay(100);
}
if(!!!client.available()) {
Serial.println("No response...");
}
while(client.available()){
Serial.write(client.read());
}
Serial.println("\nclosing connection");
client.stop();
}
I made several changes from the code provided by randomnerdtutorials.com, since I am using DHT11 not BME280. The changes include the module, library, and writing format modification. Also, as DHT11 doesn’t measure atmospheric pressure, I made a value substitution to the second value, from the supposedly BME280’s pressure to DHT11’s temperature in Fahrenheit. Also, you can change the duration of the ESP32 sleep — affecting the period of data addition the the sheets — by changing the uint64_t TIME_TO_SLEEP variable, in this case the value is 600, meaning the sensor readings will be added to Google Sheets every 10 minutes (600 seconds)
Upload the code to the board, open Serial Monitor. The message indicates that the process, including of “ESP32 sleeping” has started.

Open your Google Sheets, and you should see the sensor readings.

Wait every 10 minutes to retrieve the latest data.

And that’s it. Not so simple, but not so very time consuming. In fact, it is worth it and satisfying. Yes, if you look closely at the “Timestamp” column, you may wonder what I was doing at that time, in the middle of the night up till dawn. The answer is to finish all these projects all at once and keep the data accurate. The interesting fact is, by looking at the temperature column, Bandung at that time was terribly cold, wasn’t it?
This project, for me, is a lot easier than the previous one as the steps provided by randomnerdtutorials.com are clear and thorough. Thus, I dealt with no meaningful problems.
Thank you for reading until the very end, I hope you enjoy and look forward to another projects I’ve been planning to make. Stay safe!
Christovito Hidajat
18218043