Post Tweets With Your Raspberry Pi
Raspberry Pi is famous for its great computing strength and ability to run the Linux operating system. In today’s tutorial, you’ll learn how to make your Raspberry Pi tweet. You can add extra features to this project to post tweets autonomously if any specific event occurs. Let’s get started.
You’ll need following parts to make this project.
- Raspberry Pi running Raspbian OS (Any model)
- USB Mouse
- USB Keyboard
- HDMI Compatible Monitor
- HDMI Cable
- USB power supply for Raspberry (5V 2A)
- Working Internet Connection
Create App In Twitter
You need to create an app in Twitter so that Raspberry Pi can use to make tweets on your behalf. Go here to make a twitter app. The Raspberry Pi will require following parameters to make tweets:
- Consumer Key (API Key)
- Consumer Secret (API Secret)
- Access Token
- Access Token Secret
You can find all these details from your app.
Store Keys In Raspberry Pi
In this step, you need to create a python file in your Raspberry Pi and store all the Keys and Tokens there. Create a file named keys.py in your working folder with all information in it.
consumer_key = "Place your consumer key here" consumer_secret = "Place your consumer secret key here" access_token = "Place the access token here" access_token_secret = "place the access token secret here"
Now, save the file and go the nex step of this tutorial.
Well, what is Twython actually? Twython is the premier Python library providing an easy way to access the Twitter data. It’s been tested by companies, educational institutions and individuals alike. This amazing library will make our job a lot easier and the code much shorter. To install the Twython library, follow the given steps:
sudo apt-get update sudo apt-get upgrade sudo apt-get install python-setuptools sudo easy_install pip sudo pip install twython sudo pip install requests sudo pip install requests-oauthlib
pip is required to install Twython, so it’s installed in 3rd step. But if you already have pip installed, just ignore that step.
Write The Python Script & Run It
Open a file in your working directory in your Raspberry Pi and rename it to twitter.py. Make sure that it is in the same directory wit previously created keys.py file. Now, copy-paste the following code using any editor or IDE:
import sys from twython import Twython from keys import ( consumer_key, consumer_secret, access_token, access_token_secret ) twitter = Twython( consumer_key, consumer_secret, access_token, access_token_secret ) message = "My first tweet using Rapberry Pi! Yeh!" twitter.update_status(status=message) print("Raspberry Pi successfully tweeted: %s " % message)
Pretty simple, isn’t it? Actually, the Twython library performs lots of tasks behind the screen keeping the code surprisingly small.
Now, save the file and open terminal in your raspberry pi. Write the following command and hit the Enter key to run this Python script:
That’s all. Now you can see that is your Raspberry Pi is tweeting successfully.