How to Upload Data to Google Sheets using Pi Pico W

 

 

Discover how to effortlessly transmit data from your Raspberry Pi Pico W to Google Sheets as CSV files, streamlining the process of logging extensive sensor data. Given the Pico W's constrained storage capacity, offloading data externally becomes crucial for efficient storage management.

In this tutorial, we'll leverage an intermediary service known as IFTTT. This free automation service facilitates the establishment of a straightforward HTTP endpoint, seamlessly connecting your Raspberry Pi Pico W to Google. Notably, the setup incurs no initial costs, requiring only an active internet connection to get started.

Before reading the remainder, be sure to subscribe and support the channel if you have not!

Subscribe:

Youtube

Support:

https://www.buymeacoffee.com/mmshilleh

1-) Create an IFTTT account and setup service

Go to ifttt.com and create a free account

Create an app on the dashboard

Select If This and search and select webhook:

Select receive a web request, this will create the endpoint that the Pico W will access over the internet.

Name the event however you like, I name it BME280 in this example because I am using the ShillehTek BME280 Pressure, Temperature, and Humidity sensor to send three columns of data. Really you can use any sensor or any data you want!

You can buy the sensor at shillehtek.com if interested.

Now you have setup the first step, next select Then That and search google sheets. We want to add a row to a spread sheet.

Substitute the information for setup accordingly:

Important, you should setup the formatted row depending on how many columns of data you are pushing at a time.

Now that it is all setup, you can do a quick test by going to ifttt.com/my_services

Select Webhooks

Substitute your values in the JSON body and click Test It, if it is successful you should see a new spreadsheet in your Google Drive!

This page also contains your IFTTT secret key, which you can see in the image above is a long string at the end of the URL. Save this somewhere safe because you will need it in the MicroPython Code in the next step! 

Step 2-) MicroPython Raspberry Pi Pico W

import urequests
import time
import machine
import network
from machine import Pin, I2C
import bme280

import config


SSID = config.SSID
WIFI_PASSWORD = config.WIFI_PASSWORD

EVENT_NAME = 'BME280_YT'
IFTTT_KEY = 'czLk7ytVFFpRQZuAeEOeiXi7h18XaGANp3oi1oy_MNm'
IFTTT_URL = f'/trigger/{EVENT_NAME}/with/key/{IFTTT_KEY}'
server = 'maker.ifttt.com'

i2c = I2C(0, sda=Pin(20), scl=Pin(21), freq=400000)
bme = bme280.BME280(i2c=i2c)


def connect_wifi():
try:
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect(SSID, WIFI_PASSWORD)

for i in range(0, 10):
if not sta_if.isconnected():
time.sleep(1)
print("Connected to Wi-Fi")
except Exception as e:
print('There was an issue connecting to WIFI')
print(e)


def make_ifttt_request():
json_data = '{"value1":"' + bme.values[0] + '","value2":"' + bme.values[1] + \
'","value3":"' + bme.values[2] + '"}'
headers = {'Content-Type': 'application/json'}
response = urequests.post('https://' + server + IFTTT_URL, data=json_data, headers=headers)
print('Response:', response.content.decode())
response.close()
print('Closing Connection')


connect_wifi()
counter = 0
while True:
counter += 1
time.sleep(1)
print(f'Uploading Value: {str(counter)}')
make_ifttt_request()

Here is the code you will need to run on your device to start uploading values. You can see I use the BME280 library because I attached a BME280 to my device, really you can tailor this to fit your needs.

Just make sure you substitute the EVENT_NAME, IFTTT_KEY, and the SSID and WIFI_PASSWORD. I added those to a config file but you can just hardcode them as a string in the code. If everything is setup you should start seeing continuous values populate your Google Sheet every second or so, you can change the time.sleep() interval appropriately.

Conclusion

Hope you learned something new, if you did please consider subbing to the channel at https://www.youtube.com/@mmshilleh

Even better consider donating at https://www.buymeacoffee.com/app/dashboard

You can also buy ShillehTek sensors other than the BME280 at our Amazon store!

https://www.amazon.com/stores/ShillehTek/page/F0566360-4583-41FF-8528-6C4A15190CD6?ref_=ast_bln

We offer a wide array of pre-soldered sensors!

Create a free account to access full content.

All access to code and resources on ShillehTek.

Signup Now

Already a member? Sign In

Explore More on Our Blog

Controlling a 5V Laser Diode with Raspberry Pi Pico W

Controlling a 5V Laser Diode with Raspberry Pi Pico W

Learn how to safely control high-power components like a 5V laser diode using the Raspberry Pi Pico W and a...

How to Connect and Use the HCSR501 PIR Sensor with a Raspberry Pi Pico/Pico W

How to Connect and Use the HCSR501 PIR Sensor with a Raspberry Pi Pico/Pico W

Learn how to set up the HCSR501 PIR sensor with a Raspberry Pi Pico to detect motion and trigger...

Powering the Raspberry Pi Pico W with the MB102 Power Supply

Powering the Raspberry Pi Pico W with the MB102 Power Supply

Learn how to power your Raspberry Pi Pico W projects easily and flexibly with the MB102 Power Supply Module...

How to Use L298N Motor Driver with Pico W

How to Use L298N Motor Driver with Pico W

Learn how to use the L298N motor driver to control DC motors with the Raspberry Pi Pico W in MicroPython.

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Discover how to set up, code, and activate the LED based on detected sound with the Raspberry Pi...

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

In this tutorial, I’ll guide you through setting up the KY-037 sound sensor with a Raspberry Pi using...

How to Post to Reddit Using Python

How to Post to Reddit Using Python

Post to reddit automatically using a Python script.

How to Create a Time-Lapse Video with a Raspberry Pi Camera

How to Create a Time-Lapse Video with a Raspberry Pi Camera

Learn how to make a timelapse with your Raspberry Pi in Python.

How to Integrate the MPU6050 with the STM32 Blue Pill

How to Integrate the MPU6050 with the STM32 Blue Pill

Learn how to measure acceleration with the STM32 and the MPU6050 in the Arduino IDE.

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

This comprehensive tutorial will guide you through the process of setting up and programming the STM32 Blue Pill...

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.