How to Tweet from Your Raspberry Pi

John John (304)
15 minutes

Suppose you want to incorporate tweeting into an app you're building on your Raspberry Pi. This guide will walk you through the process of creating a twitter app, generating access tokens, and sending a tweet.

To learn the process, we're going to create a simple app on our Raspberry Pi that tweets the output of the uptime command. This might be a contrived example, but it will easily demonstrate what is required to tweet from the pi.

Posted in these interests:
h/twitter2 guides
h/pi253 guides
h/python67 guides

We've created a repository to be used for the duration of this guide. We will refer to code within so it will be helpful to clone the repository.

For your own application, you can use this code as a starting point or just copy pieces you want to use.

Git should be installed on your Pi, if not you can install it with:

sudo apt-get install git-all

Now, we'll clone the repo into our home directory:

cd
git clone git@github.com:Howchoo/Pi-Tweeter.git

Then cd into the repo.

cd Pi-Tweeter

We're going to use Tweepy, which is a Python library for using the Twitter API. This library is listed in our requirements.txt file, so all you need to do is:

sudo pip install -r requirements.txt

Optional You may want to install the packages in a virtual environment.

Create a Twitter application

In order to Tweet from your Raspberry Pi, you'll need to create a new Twitter app. First log into your Twitter account and go to https://apps.twitter.com, then click on "Create New App".

Follow the instructions, then submit.

This app will allow us to Tweet on behalf of our Twitter user.

Set the app permissions

Click on "Permissions" and make sure that "Read, Write, and Access direct messages" is checked.

After creating the app, you're taken to a screen where you can manage the app you just created. Click on "Keys and Access Tokens". Then under Application Settings you should see your consumer key and consumer secret. Copy and paste these two values into the settings.py file in the appropriate places. It should look something like this:

# Paste your consumer key and consumer secret here
consumer_key = 'ZTe3yHjf2Ji8hIblKRkPyvoet'
consumer_secret = 'luXSddKDXnYyfexmgVVVlkBElU3X4N9PHb81xT1sgJvdU8kJcm'
Generate access tokens

Scroll to the bottom of this page, and click on "Create my access token". This will generate an access_token and an access_token_secret. Paste these into settings.py as well:

# Paste access_token and access_token_secret here
access_token = '3237241200-20hXADKs34jxqGcXeDGA9KGqg4JLlwqbsBhmZpg'
access_token_secret = '5IcPQIbYa2n3jZOJbgHt196uQ5CmvTGE1gIK63xxIkTHn'
Now we're ready to tweet

To send the tweet, simply call the tweet.py file:

python tweet.py

Of course, this script is only an example. It's set up to tweet the output of the uptime command, which looks something like this:

03:17:22 up 3 days, 10:43,  3 users,  load average: 0.10, 0.03, 0.01

Maybe we can set up a motion sensor and camera and tweet pictures of our cat. Or maybe we can set up a thermometer and tweet some predefined statuses that are appropriate for the weather. Or if we just want everyone to know the load average of our pi, we can leave the repo as is and tweet our uptime command.

It's no longer bundled with the Pi, but you can still get it for free!
Zach Zach (248)
5 minutes

Raspberry Pi OS (formerly Raspbian) removed Mathematica from the official downloads list, as is evident in their release notes: * Removed Mathematica As confirmed by The Raspberry Pi Foundation, it was removed because the Mathematica contract expired after 5 years and was not renewed. If you still want to use Mathematica on the Pi, you're in luck! Mathematica maker Wolfram Tweeted that Mathematica+WolfLang will continue to be available on Raspberry Pi: Link to Tweet The Wolfram rep went on to say: Wolfram Research is committed to support Raspberry Pi's mission to put the power of computing and digital making into the hands of people all over the world. This short guide will show you how to download and install Mathematica for free onto your Raspberry Pi, getting around its removal from the downloads list.