ESP8266 Publish Temperature Sensor Readings to Google Sheets (Using IFTTT)

Oct 16, 2019

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 readings using the DS18B20 temperature sensor to a Google Sheets spreadsheet – we’ll be using IFTTT Webhooks service.

Hardware Required

  1. NodeMCU ESP8266 or ESP32
  2. DS18B20 temperature sensor
  3. Jumper Wires

IFTTT Setup

Step 1: 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.
Go the official site: IFTTT and enter your email to get started.
Step 2: After signing up go to your dashboard and create new applet by clicking on Create

1

Step 3: Click on "this" word as shown in the figure below

2

  • After clicking on "this" word, search for the “Webhooks” service and select the Webhooks icon.

3

  • Choose the “Receive a web request” trigger.

4

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

Screenshot-2019-10-16-at-3.51.07-AM

Step 4: Now, click on "that" word

Screenshot-2019-10-16-at-4.16.48-AM

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

6

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

Screenshot-2019-10-16-at-4.17.39-AM

  • Choose the “Add a row to spreadsheet” action.

Screenshot-2019-10-16-at-3.51.33-AM

Step 5:
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.
Change spreadsheet name to temp_readings

Screenshot-2019-10-16-at-3.51.45-AM

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

Screenshot-2019-10-16-at-3.51.56-AM

Testing Your Applet

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

Step 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.

Screenshot-2019-10-16-at-4

Replace {event} with the trigger event name and fill up the dummy values.Then, click the “Test it” button.

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

Screenshot-2019-10-16-at-4--1-

Step 3: Go to your Google Drive. The IFTTT service should have created a folder called “IFTTT” with the “temp_readings” spreadsheet inside.

Screenshot-2019-10-16-at-4.36.05-AM

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

Screenshot-2019-10-16-at-4.37.25-AM

Circuit

thermo-1

Code

Copy the following code to a file google_temp.py and put it into the node using ampy

import machine, time, network
import onewire, ds18x20
import urequests

# To Connect to WiFi
sta_if = network.WLAN(network.STA_IF)
ap_if = network.WLAN(network.AP_IF)
sta_if.active(True)
sta_if.connect('<Your ESSID>', '<Your Password>')
ap_if.active(False)

# connecting device to GPIO05 for Temperature Sensor
dat = machine.Pin(12)

# create the onewire object
ds = ds18x20.DS18X20(onewire.OneWire(dat))

# scan for devices on the bus
devices = ds.scan()
print('found devices:', devices)
temperatures =[]
for i in range(3):
    print('temperature:', end=' ')
    ds.convert_temp()
    time.sleep_ms(750)
    for device in devices:
        a = ds.read_temp(device)
        print(a)
        temperatures.append(a)

# Your api key
your_key = '5H2vvb22322JFeQjt5exES'
# Sending Post Request to IFTTT to write in Google Sheets                
request_headers = {'Content-Type': 'application/json'}
sensor_readings = {'value1':temperatures[0], 'value2':temperatures[1], 'value3':temperatures[2] }
request = urequests.post('https://maker.ifttt.com/trigger/temp_readings/with/key/' + your_key,
    json=sensor_readings,
    headers=request_headers)
print(request.text)
request.close()

After putting the file into the node, go to REPL using

picocom /dev/tty.USB0 -b115200

Now import the file,

import google_temp 

Output

You should get the following output in the terminal

Screenshot-2019-10-16-at-4.49.39-AM

Now, check the Google sheets.

Screenshot-2019-10-16-at-4.56.56-AM