How to Tweet from your Raspberry Pi

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.

1

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
2

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.

3

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.

4

Set the app permissions

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

5

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'
6

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'
7

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
8

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.