Text Editor
Adafruit recommends using the Mu editor for editing your CircuitPython code. You can get more info in this guide.
Alternatively, you can use any text editor that saves simple text files.
Secrets.py Setup
Open the secrets.py file on your CircuitPython device using Mu or your favorite text editor. You're going to edit this file to enter the Twitter Bearer Token you saved earlier.
-
Change
twitter_bearer_token
to your app's Twitter bearer token
Your secrets.py file should look like this:
# This file is where you keep secret settings, passwords, and tokens! # If you put them in the code you risk committing that info or sharing it secrets = { 'ssid' : 'home ssid', 'password' : 'my password', 'timezone' : "America/New_York", # http://worldtimeapi.org/timezones 'github_token' : 'fawfj23rakjnfawiefa', 'hackaday_token' : 'h4xx0rs3kret', 'twitter_bearer_token' : 'YOUR_SECRET_TWITTER_BEARER_TOKEN' }
Click the Download: Project Zip File link below in the code window to get a zip file with all the files needed for the project. Copy code.py from the zip file and place on the CIRCUITPY drive.
# SPDX-FileCopyrightText: 2019 Dave Astels for Adafruit Industries # # SPDX-License-Identifier: MIT """ Twitter API for PyPortal. Adafruit invests time and resources providing this open source code. Please support Adafruit and open source hardware by purchasing products from Adafruit! Written by Dave Astels for Adafruit Industries Copyright (c) 2019 Adafruit Industries Licensed under the MIT license. All text above must be included in any redistribution. """ #pylint:disable=invalid-name import time import board from adafruit_pyportal import PyPortal try: from secrets import secrets except ImportError: print("""WiFi settings are kept in secrets.py, please add them there! the secrets dictionary must contain 'ssid' and 'password' at a minimum""") raise # Set this to the username you'd like to display tweets from username = 'codewisdom' # determine the current working directory # needed so we know where to find files cwd = ("/"+__file__).rsplit('/', 1)[0] url = 'https://api.twitter.com/1.1/statuses/user_timeline.json?count=1&screen_name=' + username # Initialize the pyportal object and let us know what data to fetch and where # to display it pyportal = PyPortal(url=url, json_path=(0, 'text'), status_neopixel=board.NEOPIXEL, default_bg=cwd + '/twitter_background.bmp', text_font=cwd+'/fonts/Helvetica-Bold-16.bdf', text_position=(20, 60), text_color=0xFFFFFF, text_wrap=35, caption_text='@' + username, caption_font=cwd+'/fonts/Helvetica-Bold-16.bdf', caption_position=(5, 210), caption_color=0x808080) # Set OAuth2.0 Bearer Token bearer_token = secrets['twitter_bearer_token'] pyportal.set_headers({'Authorization': 'Bearer ' + bearer_token}) while True: pyportal.fetch() time.sleep(3600) # check every hour
Once all the files are copied from your computer to the PyPortal, you should have the following files on your CIRCUITPY drive:

To modify the Twitter account the PyPortal is fetching tweets from, change the following line in the code:
# Set this to the username you'd like to display tweets from username = 'codewisdom'
Setting up the PyPortal Object
Before anything can really happen, we need an instance of PyPortal
. This sets the URL, the json path used to extract the tweet text from the json returned from that URL, and the text area characteristics.
cwd = ("/"+__file__).rsplit('/', 1)[0] url = 'https://api.twitter.com/1.1/statuses/user_timeline.json?count=1&screen_name=' + username # Initialize the pyportal object and let us know what data to fetch and where # to display it pyportal = PyPortal(url=url, json_path=(0, 'text'), status_neopixel=board.NEOPIXEL, default_bg=cwd + '/twitter_background.bmp', text_font=cwd+'/fonts/Helvetica-Bold-16.bdf', text_position=(20, 60), text_color=0xFFFFFF, text_wrap=35, caption_text='@' + username, caption_font=cwd+'/fonts/Helvetica-Bold-16.bdf', caption_position=(5, 210), caption_color=0x808080)
Authenticating with Twitter
The OAuth2.0 bearer token authenticates requests on behalf of the application you just created. The bearer token is not tied to your Twitter account, so the code will need to pass a username
for the twitter account its retrieving tweets from.
- For more info about the OAuth 2.0 Bearer Token, visit Twitter's Developer Documentation.
bearer_token = secrets['twitter_bearer_token'] pyportal.set_headers({'Authorization': 'Bearer ' + bearer_token})
Fetching Tweets
Now that the bearer token has been set, the API can be called to fetch some tweets.
The main loop is below. Every hour it fetches the most recent tweet. Above we set up the PyPortal
instance with everything it needs to fetch data from the Twitter API, extract the text of the tweet, and display it. All that's needed is to tell it to do that, using the fetch()
method.
while True: pyportal.fetch() time.sleep(3600) # check every hour
Page last edited January 22, 2025
Text editor powered by tinymce.