Introduction: Gform2adafruitIO: Automatic Marquise Driven by Google Form/Adafruit IO

Python script to automatically publish data from Google Form to Adafruit IO.

This syllabu provides a framework to easily link a Google Form to Adafruit IO feeds that can be used for any number of projects. Instead of allowing only anyone to access your feeds directly, this program acts as a middleware between a public Google Human body and a private feed. Anyone give the sack complete your Google Form. This program will periodically halt the variant for new submissions, veterinary the entry data to confirm the data is valid, then push the data to your Adafruit IO feed.

The example I give in this tutorial uses the Adafruit Matrix Portal site to make a Marquee Sign away. I came up with this idea as a way to allow my students to well submit text to the Matrix Portal in the research lab. The Google Form asks the user for text and a color. The program ensures the color is well-grounded and the text is profanity-released (as profanity-free as possible). The program then publishes the information to Adafruit IO. The Matrix Portal pulls this selective information retired from Adafruit IO to scroll across the display. This code can easily be adapted for any application.

Supplies

  • Raspberry Shamus or Data processor (Windows/Mack/Linux) with internet and Python to run script
  • Adafruit IO account (free)
  • Google Account (free)

Step 1: Setting Up Environment

Python 3 Installment

If you don't already sustain Python installed, you will need to install it. Go to https://www.Python.org/downloads/ and download and run the correct facility for your operating system. You will need to make foreordained that you install pip and minimal brain damage Python/pip to Course.

Git Project Files

Today you will need to recollect Python script and other files from my Github repository. You can either install a git client and clone the repository or you can download a zip file of the repository from your browser.

https://github.com/mjdargen/gform2adafruitIO

git knockoff  https://github.com/mjdargen/gform2adafruitIO.git

Installing Python Packages

To run this program, you will require to install the following packages: requests, webcolors, adafruit-io, python-dotenv. These can be installed easy by navigating to the cloned repository directory and running the following command:

pip3 install -r requirements.txt

If you have issues with any of the packages, you put up downgrade them to these versions (the most current at the time of publishing). These are the specific versions I tested: Python == 3.7.7, requests == 2.24.0, webcolors == 1.11.1, adafruit-io == 2.4.0, python-dotenv == 0.14.0.

Step 2: Setting Up Google Organise

Create Google Form/Google Sheet

First, you leave need to create a Google Form. Open aweigh Google Drive and create a new form. Present is a link to my sample make for this project: https://forms.gle/MUWWtGKMeH4RmXeq6.

You will require to stimulate the Google Form mechanically publish to a Google Sheet. To do soh, click on the "Responses" tab of your Google Form and click the Sheets picture (shown in the image above). This will bring you to a linked spreadsheet. Any time someone submits a newfound conformation, it will mechanically show aweigh in the linked Google Sheet.

Publish Sheet to Python Script

Complete the following steps to publish the Google Sheet as a .csv soh information technology can represent retrieved past the Python Playscript.

  • Go to "File -> Publish to the web".
  • This will bring up the window shown above. Select the "Link" tab.
  • Select the plane that you wishing to put out: "Form Responses 1".
  • Then select to publish information technology as "Comma-separated values (.csv)".
  • Once you are ready, dawn Publish. A toss off-up will come along asking if you are sure. Click "OK".
  • The window will in real time show the link to the .csv file.
  • You can yawning this link in a web browser and it will download the .csv file cabinet thus you can study a look.
  • Hither is the .csv published aside my sample form: https://docs.google.com/spreadsheets/d/e/2PACX-1vQ2AwUbx6lsZK-H0WjvcF1Bu2VUlsN4ir8kMD10xSEkl-JkxXKlqLZfnJ5pgyNhYIDYMEOK6Ys4cEYK/pub?gid=1198589603&single=true&output=csv

Step 3: Adafruit Io Setup

https://io.adafruit.com/

Adafruit Io is an incredible cloud service that allows you to push and pull data backward and forward from umteen different devices. In the example I provided, I pushed information from my Raspberry Pi to Adafruit IO and my Matrix Portal site pulled the information back down from Adafruit IO. Adafruit Io likewise gives you the ability to create dashboards and connect to otherwise webservices. For our purposes, we will be primarily afraid with sending and retrieving data from a one-on-one feed on Adafruit IO.

If you would like to go through the specific example of the Matrix Vena portae Marquee, watch over this guide here: https://learn.adafruit.com/aio-quote-board-matrix-display/. Connected the "Cypher the Quotation Board" Page, it walks you done step-by-step how to create a feed for your sign.

In my case, I created two feeds: one for schoolbook (called signtext) and one for discolour (called signcolor). Once I created these 2 feeds, I assigned them to a group I made called "ground substance portal quotes". You will deman these name calling to refer to later, so keep them in mind!

--

If you would alike to use gform2adafruitIO for a different project, there are many a other helpful guides Adafruit has published about Adafruit Io that testament aid you with some irrespective what your circumstantial requirements. Here is a serial of reformative Adafruit IO guides: https://se.adafruit.com/series/adafruit-io-basics.

Step 4: Surround Variable Setup

Once you have apparatus your Adafruit IO account, you should have a username and a key. I will show you how to safely memory boar these Eastern Samoa an environment variable so you don't accidentally publish this reclusive information to the web. This program uses the python-dotenv package to do this.

To set up the environment variables, create a single file in the cloned repository's directory called ".env". Open this file in your favorite text editor and provide your username and headstone in the following format:

ADAFRUIT_IO_KEY=<put_your_adafruit_io_key_here> ADAFRUIT_IO_USERNAME=<put_your_adafruit_io_username_here>

You're whol done! The Python script will successfully import your entropy into the code. If you ever publish your code to GitHub, the default .gitignore file will automatically know to chuck out your .env file so that your confidential key is not shared with the the great unwashed!

Step 5: Modifying the Cypher

Now, you testament need to serve some slight modifications to the code to make believe it work for your specific situation.

  1. Update the URL for your .csv file.
    • Modify the adaptable CSV_URL to point to the URL where your Google Sheet published the .csv file.
    • CSV_URL = "<your_URL_here>"
  2. Located the maximum number of quotes you want to store.
    • Alter the variable MAX_QUOTES to ascendence the upper limit number of quotes you want to store in your Adafruit Io Flow from
    • MAX_QUOTES = <your_integer_value_here>
    • If you specify 10, it will keep the 10 most recent quotes/colors
  3. Fix up the names for Adafruit IO feeds.
    • In my case, I had two feeds: one for text and one for color.
    • I grouped them collectively in the Adafruit Io hepatic portal vein.
    • When grouped, the nominate format for feeds is: "group_name.feed_name".
      • TEXT_FEED = 'matrix-portal-quotes.signtext'
      • COLOR_FEED = 'matrix-portal vein-quotes.signcolor'

If you are using this code for an entirely different project, you volition also demand to modify the processing() function. My processing function organizes the form data into a labeled dictionary structure. It then checks to make sure that the color is sensible and that the text is free of profanity. You will need to change the processing() to wooing some your needs are.

Step 6: Executing the Program

To reckon the origin code, visit this Github repository.

Computer program Description

Here is a description of the functionality per function of the political platform:

  • fetch_form_data()
    • Uses requests library to download .csv file of Google Sheet.
    • Compares downloaded file with last retrieved file out to imag if there is new submission data.
    • If there is new information, to process IT proceeds. Otherwise, the computer programme ends.
  • processing()
    • Organizes form data into dictionary complex body part.
    • Calls color_check() to process color data and see if IT is valid.
    • Calls profanity_check() to process text data and see if it is at large of profanity.
  • adafruitIOaccess()
    • Connects with Adafruit IO using Rest API.
    • Pulls existing values from school tex & color feeds.
    • Adds the new values to those feeds.
    • Removes values from those feeds if they exceed the uttermost throttl of values.
  • update_files()
    • After successfully completing each other tasks, overwrites previous downloaded .csv with current downloaded .csv for next carrying into action.

Here is a verbal description of 2 unlike shipway to run the computer code.

  • 1st if __name__ == "__main__": option:
    • Program executes exactly at one time and so exists. Great for scheduling handwriting to run.
    • My playscript is running on a Raspberry Pi and scheduled using cron. Use this tool around to figure out scheduling.
    • Below I show how to add a task to cron to schedule the script to run every 30 minutes.
    sudo crontab -e */30 * * * * /usr/BIN/python3 /home/principal investigator/Documents/gform2adafruitIO/gform2adafruitIO.py
  • 2nd if __name__ == "__main__": option:
    • Program executes repeatedly in a loop forever. There is a sleep interval between executions.
    • Set sleep interval (in seconds) by modifying this line of work: INTRVL = 1800
    • Below I show how to call the program.
    python3 gform2adafruitIO.py

Step 7: Adafruit Matrix Hepatic portal vein Info

If you want to political campaign this specific Marquee Sign representative with the Adafruit Matrix Portal, check out the links below. These guides bequeath walk you through how to limit ascending the Matrix Portal you bet to set up the feeds in Adafruit IO.

Adafruit Matrix Portal Cartesian product Page: https://WWW.adafruit.com/product/4745

General Adafruit Intercellular substance Portal vein Info: https://learn.adafruit.com/adafruit-matrixportal-m4

Custom Scrolling Quote Teacher: https://memorise.adafruit.com/aio-quote-board-matrix-display

Step 8: More Projects

Step 9: Source Code:

To view the beginning code, visit this Github monument.

Be the First to Percentage

Recommendations

  • Anything Goes Contest 2022

    Anything Goes Contest 2022