Random Nerd Tutorials
Shares

ESP32 Publish Sensor Readings to Google Sheets (ESP8266 Compatible)

Shares

In this tutorial we’re going to show you how to publish sensor readings to Google Sheets using ESP32 or ESP8266 board. As an example, we’ll publish temperature, humidity, and pressure readings using the BME280 sensor to a Google Sheets spreadsheet every 30 minutes – we’ll be using IFTTT.

Note: Integrating directly with Google Sheets requires an HTTPS authentication. There are implemented methods to use HTTPS both with the ESP8266 and ESP32. However, some of those libraries are no longer supported or have very little documentation. The easiest way to integrate with Google Sheets using the ESP8266 or ESP32, is using a 3rd party service like IFTTT.

I understand that some of you don’t like to rely on third-party services like this or don’t want, but in my opinion this is the easiest and most reliable way of accomplishing this project.

Project Overview

The following figure shows an overview of what you’ll achieve by the end of this project.

  • First, the ESP connects to your Wi-Fi network;
  • Then, the BME280 takes the temperature, humidity, and pressure readings;
  • Your ESP32 or ESP8266 communicates with the IFTTT Webhooks service that publishes the readings to a spreadsheet on Google Sheets that is saved in your Google Drive’s folder;
  • After publishing the readings, the ESP goes into deep sleep mode for 30 minutes;
  • After 30 minutes the ESP wakes up;
  • After waking up, the ESP connects to Wi-Fi, and the process repeats.

Creating Your IFTTT Account

For this project we’ll be using IFTTT to integrate with Google Sheets. So, the first step is creating an account on IFTTT if you don’t have one. Creating an account on IFTTT is free!

Go the official site: ifttt.com and enter your email to get started.

Creating an Applet

Next, you need to create a new applet. Follow the next steps to create a new applet:

1) Go to “My Applets”and create a new applet by clicking the “New Applet” button.

2) Click on the “this” word that is in a blue color – as highlighted in the figure below.


3) Search for the “Webhooks” service and select the Webhooks icon.

4) Choose the “Receive a web request” trigger.

5) Give a name to the event. In this case “bme280_readings” as shown in the figure below. Then, click the “Create trigger” button.

6) Click the “that” word to proceed.

7) Search for the “Google Sheets” service, and select the Google Sheets icon.

8) If you haven’t connected with the Google Sheets service yet, you need to click the “Connect” button.

9) Choose the “Add a row to spreadsheet” action.

10) Then, complete the action fields. Give the spreadsheet a name, leave the “Formatted row” field as default, and then, choose a Google Drive folder path. If you leave this field empty, IFTTT will create a folder called “IFTTT” in your Google Drive folder to save the spreadsheet. Finally, click the “Create action” button.

11) Your applet should be created after you press the “Finish” button.

 

Testing Your Applet

Before proceeding with the project, it is very important to test your applet first. Follow the next steps to test your applet.

1) Go to the Webhooks Service page, and click the “Documentation” button.

2) A page as shown in the following figure will appear. The page shows your unique API key. You shouldn’t share your unique API key with anyone.

Fill the “To trigger an Event” section as shown below – it is highlighted with red rectangles. Then, click the “Test it” button.

3) The event should be successfully triggered, and you’ll get a green message as shown below saying “Event has been triggered”.

4) Go to your Google Drive. The IFTTT service should have created a folder called “IFTTT” with the “BME280_Readings” spreadsheet inside.

5) Open the spreadsheet, and you should see the values you’ve filled previously to test the applet.

Continue reading this post to see how to integrate the IFTTT Google Sheets service with your ESP32 or ESP8266.

Parts Required

For this example we’ll take sensor readings from the BME280 sensor. Here’s a list of parts you need to build the circuit for this project:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Schematics

The BME280 sensor we’re using in this example can communicate with the ESP32/ESP8266 using I2C communication protocol. So, we’re going to use the ESP I2C pins.

BME280 with ESP32

Follow the next schematic diagram to wire the BME280 sensor if you’re using an ESP32.

(This schematic uses the ESP32 DEVKIT V1 module version with 36 GPIOs – if you’re using another model, please check the pinout for the board you’re using.)

BME280 with ESP8266

Follow the next schematic diagram if you’re using an ESP8266 12E.

Note: to use deep sleep with the ESP8266, you need to wire D0 (GPIO16) to the RST pin.

Installing the BME280 library

Before going straight to the code, you need to install the BME280 library to read from the BME280 sensor. We’ll be using the Adafruit_BME280 library. Follow the next steps to install the library in your Arduino IDE:

  1. Click here to download the Adafruit-BME280 library. You should have a .zip folder in your Downloads folder
  2. Unzip the .zip folder and you should get Adafruit-BME280-Library-master folder
  3. Rename your folder from Adafruit-BME280-Library-master to Adafruit_BME280_Library
  4. Move the Adafruit_BMPE280_Library folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Alternatively, you can go to Sketch > Include Library > Manage Libraries and type “adafruit bme280” to search for the library. Then, click install.

Installing the Adafruit_Sensor library

To use the BME280 library, you also need to install the Adafruit_Sensor library. Follow the next steps to install the library in your Arduino IDE:

  1. Click here to download the Adafruit_Sensor library. You should have a .zip folder in your Downloads folder
  2. Unzip the .zip folder and you should get Adafruit_Sensor-master folder
  3. Rename your folder from Adafruit_Sensor-master to Adafruit_Sensor
  4. Move the Adafruit_Sensor folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Code

There’s an add-on for the Arduino IDE allows you to program the ESP32 using the Arduino IDE and its programming language. Follow one of the next tutorials to prepare your Arduino IDE, if you haven’t already.

After making sure you have the ESP32 add-on installed, you can copy the following code to your Arduino IDE. But don’t upload it yet! You need to make a few modifications to make it work for you.

Note: this code works both with the ESP32 and the ESP8266

Including your SSID and password

The first thing you need to modify in the code is writing your network credentials: the SSID and password on the following lines:

// Replace with your SSID and Password
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

Including your unique IFTTT URL resource

Then, you need to write your unique IFTTT URL resource. Go back to “Testing your Applet” section bullet 2) to get your unique IFTTT URL resource.

// Replace with your unique IFTTT URL resource
const char* resource = "REPLACE_WITH_YOUR_IFTTT_URL_RESOURCE";

In my case, my resource is:

/trigger/bme280_readings/with/key/nAZjOphL3d-ZO4N3k64-1A7gTlNSrxMJdmqy3

So, that line in the code looks as follows:

const char* resource = "/trigger/bme280_readings/with/key/nAZjOphL3d-ZO4N3k64-1A7gTlNSrxMJdmqy3";

Setting the sleep time

In this example we’ve set the sleep time to 30 minutes. This means that every 30 minutes the ESP wakes up, takes the readings, and publishes in your Google Sheets spreadsheet. The sleep time is set in the TIME_TO_SLEEP variable in seconds:

// sleep for 30 minutes = 1800 seconds
uint64_t TIME_TO_SLEEP = 1800;

If you want to change the sleep time, you change need to change the TIME_TO_SLEEP variable. Note that you should enter the sleep time in the TIME_TO_SLEEP variable in seconds.

Warning: be careful setting the sleep time. If you set a very short period, you may exceed the limit of requests imposed the IFTTT service.

Sending the BME280 readings

The BME280 sensor readings are sent using the jsonObject variable as shown in the following line (highlighted in bold):

String jsonObject = String("{\"value1\":\"") + bme.readTemperature() + "\",\"value2\":\"" + (bme.readPressure()/100.0F) + "\",\"value3\":\"" + bme.readHumidity() + "\"}";

If you want to send other sensor readings, you just need to replace the BME280 readings in bold with the readings for the sensor you’re using.

Publish temperature in Fahrenheit

In order to publish the temperature in Fahrenheit, you need to comment and uncomment the code like this:

// Temperature in Celsius
/*String jsonObject = String("{\"value1\":\"") + bme.readTemperature() + "\",\"value2\":\"" + (bme.readPressure()/100.0F) + "\",\"value3\":\"" + bme.readHumidity() + "\"}";*/

// Comment the previous line and uncomment the next line to publish temperature readings in Fahrenheit 
String jsonObject = String("{\"value1\":\"") + (1.8 * bme.readTemperature() + 32) + "\",\"value2\":\"" + (bme.readPressure()/100.0F) + "\",\"value3\":\"" + bme.readHumidity() + "\"}";

Demonstration

After making all the necessary changes. Upload the code to your ESP32 or ESP8266. Make sure you select the right board and COM port.

Every 30 minutes, the ESP32 or ESP8266 wakes up to take sensor readings and publishes the readings in a spreadsheet on Google Sheets.

The ESP32 chip has a built-in clock, so the readings are very accurate and it publishes to the spreadsheet every 30 minutes. On the other hand, the ESP8266 publishes new readings approximately every 28 to 29 minutes.

Wrapping Up

In this post we’ve shown you how to publish your sensor readings with your ESP32 or ESP8266 to a spreadsheet on Google Sheets using the IFTTT platform. As an example, we’ve published readings from the BME280 sensor. We’ve also used the ESP deep sleep capabilities to save power. This way, the ESP is awake only when we need to take readings. You should be able to take this project example and apply it to your own projects.

Please note that this method has some limitations: first, it uses a third party service, and second, you need to be careful with the amount of requests you make in one day. However, this method works very well and it is easy to implement.

If you like ESP32 and you want to learn more, make sure you check our course exclusively dedicated to the ESP32: Learn ESP32 with Arduino IDE.

Thanks for reading.


20 Easy Raspberry Pi Projects

Our new book is a beginner-friendly collection of electronics projects, perfectly suited for hobbyists, parents, kids, parents, and educators looking to level up their hardware skills - GET THE BOOK »

Recommended Resources

Home Automation using ESP8266 »
Build IoT projects and home automation gadgets with the ESP8266 Wi-Fi module.

Build a Home Automation System »
Learn how to build a automation system using open-source hardware and software from scratch.

Arduino Step-by-Step Projects »
Build 25 cool Arduino projects with our couse even with no prior experience!

Leave a Comment:

Add Your Reply