Remote Data Logger with Google Sheets using ESP32

Hey there, fellow DIY enthusiasts! Have you ever wished you could remotely log sensor data and store it neatly in a Google Sheets spreadsheet? Well, I did, and that’s how I ended up building my very own ESP32 remote data logger. The ESP32 is an incredible little microcontroller, perfect for projects that require Wi-Fi connectivity. So, I thought, why not use it to gather sensor data and have it all automatically logged in a Google Sheet for easy access? In this guide, I'll walk you through every step I took to bring this project to life, from setting up the ESP32 to coding it to talk with Google Sheets.


Why This Project?


Before diving into the build, let me tell you why I found this project exciting. As someone who’s into home automation, I love keeping track of environmental data like temperature and humidity. But running to the sensor to get readings? Not so much. By setting up the ESP32 to log data to Google Sheets, I can access those readings from anywhere—whether I’m on the couch or halfway across the world. Plus, Google Sheets is free and easy to use, which made it the perfect choice for storing my data.


Materials I Used:


Here’s everything I needed to get started:

- ESP32 Development Board (I used a standard ESP32 DevKitC)

- USB Cable (for programming the ESP32)

- DHT22 Sensor (measures temperature and humidity)

- Breadboard and some jumper wires

- Laptop with Arduino IDE installed (v2.0 or later)

- A Google account (for setting up Google Sheets)


Step 1: Setting Up the ESP32 with the Arduino IDE


First things first, I needed to make sure my Arduino IDE was ready for the ESP32. Here’s how I did it:


1. Install the ESP32 Board:

   - I opened up my Arduino IDE, went to File > Preferences, and pasted this URL into the Additional Board Manager URLs field:

https://dl.espressif.com/dl/package_esp32_index.json

   - Then, I headed to Tools > Board > Board Manager, searched for "ESP32", and clicked Install.


2. Connect the ESP32:

   - With my ESP32 plugged into the laptop via a USB cable, I selected the correct board and port from Tools > Board and Tools > Port.

   - For my setup, I used the "ESP32 Dev Module."


3. Test the Setup:

   - To make sure everything was working, I uploaded the WiFiScan example sketch from File > Examples > WiFi > WiFiScan. If you see a list of available networks in the Serial Monitor, you’re good to go!


Step 2: Wiring the Sensor


Next, it was time to connect the DHT22 temperature and humidity sensor to the ESP32. Here’s the wiring setup I used:


- VCC of the DHT22 connected to the 3.3V pin on the ESP32.

- GND of the DHT22 to the GND of the ESP32.

- Data Pin of the DHT22 to GPIO 4 on the ESP32 (you can use a different pin if you like, but remember to update the code).


I opted for the DHT22 because it’s a bit more accurate than the DHT11, but the DHT11 would work fine for this project too.


Step 3: Preparing Google Sheets for Data Logging


With the ESP32 and sensor ready, I turned my attention to Google Sheets. Here’s how I set up the spreadsheet:


1. Create a New Google Sheet:

   - I went to [Google Sheets](https://sheets.google.com) and created a new sheet titled "ESP32 Data Logger". 

   - I kept the first row for headers: Timestamp, Temperature, and Humidity.


2. Create a Google Apps Script:

   - From the Google Sheets interface, I navigated to Extensions > Apps Script. This is where I wrote a simple script that would allow my ESP32 to send data directly to the sheet.

   - I deleted any existing code in the script editor and replaced it with this:


     function doGet(e) {

         var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

         var row = [];

         var d = new Date();

         row.push(d);

         for (var param in e.parameter) {

             row.push(e.parameter[param]);

         }

         sheet.appendRow(row);

         return ContentService.createTextOutput("Success");

     }


   - I saved the script and deployed it as a Web App by clicking on Deploy > New Deployment, selecting Web App, and setting access to Anyone. This generated a Web App URL—make sure to copy it, as we’ll need it in the next step!


Step 4: Writing the Arduino Code


With my Google Sheets ready to receive data, it was time to code the ESP32. Here’s the code I used:


#include <WiFi.h>

#include <HTTPClient.h>

#include "DHT.h"


#define DHTPIN 4  // Pin where the DHT22 is connected

#define DHTTYPE DHT22


const char* ssid = "your_SSID";  // Replace with your Wi-Fi SSID

const char* password = "your_PASSWORD";  // Replace with your Wi-Fi Password

const char* serverURL = "your_google_script_url";  // Replace with your Web App URL


DHT dht(DHTPIN, DHTTYPE);


void setup() {

    Serial.begin(115200);

    WiFi.begin(ssid, password);

    

    while (WiFi.status() != WL_CONNECTED) {

        delay(1000);

        Serial.println("Connecting to WiFi...");

    }

    

    Serial.println("Connected to WiFi");

    dht.begin();

}


void loop() {

    if (WiFi.status() == WL_CONNECTED) {

        float temperature = dht.readTemperature();

        float humidity = dht.readHumidity();

        

        if (!isnan(temperature) && !isnan(humidity)) {

            HTTPClient http;

            String url = String(serverURL) + "?temp=" + temperature + "&humidity=" + humidity;

            

            http.begin(url);

            int httpResponseCode = http.GET();

            

            if (httpResponseCode > 0) {

                String response = http.getString();

                Serial.println("Response: " + response);

            } else {

                Serial.println("Error sending request");

            }

            

            http.end();

        } else {

            Serial.println("Failed to read from DHT sensor!");

        }

    } else {

        Serial.println("WiFi Disconnected");

    }

    

    delay(60000);  // Log data every 60 seconds

}


Step 5: Testing the ESP32 Data Logger


With the code uploaded, I opened the Serial Monitor and watched as my ESP32 connected to Wi-Fi and started sending data to my Google Sheets. After a few seconds, I switched to my Google Sheet, and there it was—fresh rows of temperature and humidity data, complete with timestamps!


Troubleshooting Tips


Along the way, I ran into a couple of hiccups, so here are a few tips if you encounter similar issues:

- Wi-Fi Connection Issues: Double-check your SSID and password. I also found that bringing the ESP32 closer to my router helped stabilize the connection.

- Google Script Issues: If you get "permission denied" errors, make sure your Web App URL has access set to "Anyone."

- Sensor Readings Not Displaying: Ensure that the sensor is wired correctly, and double-check that you’ve specified the right GPIO pin in the code.


Conclusion: What’s Next?


And that’s it! I now have a fully functional ESP32 remote data logger that sends sensor data straight to Google Sheets. This setup is a great foundation for future projects—imagine monitoring plant soil moisture, tracking energy consumption, or even logging weather data. With a bit of creativity, you can tailor this project to fit a wide range of use cases.


Happy logging, and see you in the next project!