In this example, you'll use the Twitter API to create a query URL to request tweets that fit your search criteria. To get started, the query in the example looks for tweets from Adafruit that have the text "NEW GUIDE". With this query, you'll always know when a new Learn Guide has been tweeted about.
Code the Twitter Feed Test
Once you've finished setting up your Pico W with CircuitPython, you can access the code and necessary libraries by downloading the Project Bundle.
To do this, click on the Download Project Bundle button in the window below. It will download as a zipped folder.
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries # # SPDX-License-Identifier: MIT import time import os import ssl import wifi import socketpool import microcontroller import adafruit_requests # query URL for tweets. looking for tweets from Adafruit that have the text "NEW GUIDE" # disabling line-too-long because queries for tweet_query & TIME_URL cannot have line breaks # pylint: disable=line-too-long tweet_query = 'https://api.twitter.com/2/tweets/search/recent?query=NEW GUIDE from:adafruit&tweet.fields=created_at' headers = {'Authorization': 'Bearer ' + os.getenv('bearer_token')} wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD')) pool = socketpool.SocketPool(wifi.radio) requests = adafruit_requests.Session(pool, ssl.create_default_context()) last_id = 0 # checks last tweet's ID check = 0 # time.monotonic() holder while True: try: if (check + 30) < time.monotonic(): # updates current time # get tweets from rpilocator containing in stock at adafruit twitter_response = requests.get(url=tweet_query, headers=headers) # gets data portion of json twitter_json = twitter_response.json() twitter_json = twitter_json['data'] # to see the entire json feed, uncomment 'print(twitter_json)' # print(twitter_json) # tweet ID number tweet_id = twitter_json[0]['id'] # tweet text tweet = twitter_json[0]['text'] # timestamp timestamp = twitter_json[0]['created_at'] if last_id != tweet_id: print("New Learn Guide!") print(tweet) print(timestamp) last_id = tweet_id check = time.monotonic() # pylint: disable=broad-except # any errors, reset Pico W except Exception as e: print("Error:\n", str(e)) print("Resetting microcontroller in 10 seconds") time.sleep(10) microcontroller.reset()
Upload the Code and Libraries to the Pico W
After downloading the Project Bundle, plug your Pico W into the computer's USB port with a known good USB data+power cable. You should see a new flash drive appear in the computer's File Explorer or Finder (depending on your operating system) called CIRCUITPY. Unzip the folder and copy the following items to the Pico W's CIRCUITPY drive.
- lib folder
- code.py
Your Pico W CIRCUITPY drive should look like this after copying the lib folder and the code.py file.
Add Your settings.toml File
Remember to add your settings.toml file as described in the Create Your settings.toml File page earlier in the guide. You'll need to include your WIFI_SSID
, WIFI_PASSWORD
and Twitter bearer_token
in the file.
CIRCUITPY_WIFI_SSID = "your-ssid-here" CIRCUITPY_WIFI_PASSWORD = "your-ssid-password-here" bearer_token = "your-twitter-bearer-token-here"
For the Twitter API, you'll need to request a developer account and create a project on the API. Follow along with this guide page to see how to do this and retrieve your bearer authentication token.
Run code.py
Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!
Every 30 seconds, a request is made to the Twitter API using your query URL. If a new tweet is available that matches the query, its text and timestamp are printed to the REPL. The tweet's tweet ID is used to determine if a new tweet is available.
Text editor powered by tinymce.