I recently found a way to tweet from the Raspberry Pi using an app called Tweepy. I've written a tutorial below on how to do this as I've wanted to automate Twitter feeds for a while now and this looks like just the thing to enable me to do it. It took me about an hour to setup and now I've got it up and running, I'm going to look at reading data from the environment, the web and local databases and tweeting them out directly.

This setup takes a number of steps, namely:

  1. Install Tweepy - the python twitter client.
  2. Set up and authenticate your twitter account
  3. Configure Tweepy.
  4. Test it out

Install Tweepy

Tweepy is a Twittter client written in Python which you can use to both send and retrieve tweets. To install Tweepy, you'll need to make sure you have the up-to-date version of the Python setup tools, pip (which allows you to easily manage Python applications) and Tweepy. You can do all this by typing the following at the Raspberry Pi terminal:

sudo apt-get install python-setuptools
sudo easy_install pip
sudo pip install tweepy

Tweepy should all be set up now. Our next job is to register ourselves with Twitter so that we can use their API. This is pretty straightforward.

Set up and authenticate your Twitter account

You'll need a twitter account for this, so if you don't have one already, go to Twitter to set one up. Next you'll need to go to the Twitter Dev area to register an app against your Twitter account. The reason we're doing this is so that we don't have to enter our username and password when we're making requests to Twitter. Simply enter the information in the boxes provided:

  1. Select create a new application
  2. Enter the name, description and website (leave the callback URL blank)
  3. Accept the terms and conditions

Now you've created your app, you'll need to change the permissions. Click on the Permissions tab at the top and check the Read and Write access box. I found that this didn't update immediately and I had to wait a while until it finally showed the correct permissions. If you look at the top of the page, you'll see a button that says 'Test OAuth'. Click on this and leave it open as you'll need the settings to set up Tweepy.

Configure Tweepy

Now your Twitter app is setup, your next step is to create a python script that will store your authentication details and take care of the tweeting for you.

First, create a file called Tweet.py in the /home/pi directory. (You can create it where you like, but for the purposes of this tutorial, I'll keep it here.)

sudo nano /home/pi/Tweet.py

and copy/paste the following code into it:

#!/usr/bin/env python
import sys
import tweepy
CONSUMER_KEY = '***************YOUR DATA*****************'
CONSUMER_SECRET = '***************YOUR DATA*****************'
ACCESS_KEY = '***************YOUR DATA*****************'
ACCESS_SECRET = '***************YOUR DATA*****************'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)

Now, using the correct information which should be on your screen from the previous step, update the information in this file with your Twitter OAuth information. (Make sure you keep the '' but replace the **YOUR DATA** part with your own information.)

When you've done this, CTRL-X and Y to save your file. Lastly, we'll need to change our permissions:

sudo chmod 755 Tweet.py

and we're ready to go!

Test it out

To send out your first tweet from your Raspberry Pi, enter the following at the command line:

python /home/pi/Tweet.py 'Hello out there!'

You're done, you've sent out your first Raspberry Tweet. Now you have the basics in place you can look at gathering information either from your local environment, a database or the Internet and tweeting this out. I'll try and cover this in another article, meanwhile if you have any questions, please leave a comment below. Thanks.


comments powered by Disqus