Raspberry Pi - Python and Google Sheets

Raspberry Pi - Python and Google Sheets

I have a variety of projects in mind for the Raspberry Pi, and I have wanted to work on more Python scripting as well. Python can easily read data from the Pi's GPIO pins, but also from a URL (web address). This allows for collecting data from sensors connected directly to the Pi, and from data sources anywhere in the world.

The question is, what do you do with that data once you have it?

I do hope to use my own personal cloud to store the data in, but I think a lot of people would love to know how to store data in a Google Sheet using Python. Saving to a Google Sheet has a number of advantages. Not only will you be able to look at that data from anywhere, you can format the cells, enter calculations, and even create graphs. Getting data into a Sheet from Python is pretty easy using the gspread Python API. There are a few "setup" steps you need to do in your Google account first, but the instructions are straightforward.

I have already managed to start collecting some data using fairly common tools and mechanisms in Python and Linux. There are so many tutorials out there that it really isn't necessary to rehash them here, but I can share what I am using so far.

  • The urrlib package lets you read data from Internet resources in Python.
  • Regular expressions are incredibly powerful when you are searching through a web site for a specific data point. A regex (regular expression) can be simple, but there are some very complex ones that can really twist your brain. The re module for Python puts the power of regular expressions into your scripts.
  • If the data you just collected is particularly interesting in some way, and you want to get notified of it, use the Python smtplib module to send an email notification. Check with your cell phone provider because you may even be able to receive email as a text message!
  • cron is Linux's job scheduler. Once you have a script that you want to run at regular intervals, cron is how to set that up.

It's incredible how much you can do with a tiny computer. If you are a teacher, and want to engage your students in meaningful programming projects, the Raspberry Pi and some Python scripting will go a long way. My two scripts that read a web site for some data, put that data into a Google Sheet, check the data for significant changes, and then notify me of those changes are less than 100 lines of Python, total.

To view or add a comment, sign in

More articles by Kyle Tuck

Explore content categories