Raspberry Pi Twitter Bot

Raspberry Pi Twitter Bot
!
In this workshop you’ll build an automated Raspberry Pi Bot that
takes photos and posts them to Twitter.
!
!
Connect the Raspberry Pi Camera Module
!
Start by connecting the camera module to the CSI port, located behind the Ethernet port
Next, open the raspi-config tool from the command line:
!
sudo raspi-config
!
!
Select Enable camera and hit Enter, then go to Finish and you'll be prompted to
reboot.
!
Testing the Camera
!
You can use the command line to control the camera. The command raspistill is used
to capture a still photograph. With the camera module connected and enabled, enter the
following command in the Terminal to take a picture:
!
raspistill -o cam.jpg
!
!
If the camera is upside down the image can be rotated by 180° so it is displayed
correctly. You can do this by applying both a vertical and a horizontal flip by passing in
the -vf and -hf flags:
!
raspistill -vf -hf -o cam2.jpg
!
!
Control the Camera with Python
!
Rather than using the command line you can control the camera programmatically. We
can do this with Python, a simple but powerful programming language. To do this we
need to install the Python picamera library packages:
!
sudo apt-get install python-picamera python3-picamera
!
!
We also want to install the Twython library - this is a Python module that allows our
programs to connect to Twitter. This means we can make the Pi tweet messages, photos,
or perform searches.
sudo
sudo
sudo
sudo
!
!
apt-get update
apt-get install python-setuptools
easy_install pip
pip install twython
Registering a Twitter app
!
First create a new Twitter account for the Pi to use. It’s a good idea not to use your own
personal account.
!
To allow to Pi to post new tweets and interact with Twitter, we need to register our app
with the Twitter. API.
!
Go to https://apps.twitter.com/ and click Create New App. You don’t need to specify a
callback URL and you can use http://www.coderdojoscotland.com as the website.
!
You’ll see something resembling this once you’re done – these keys are unique to you.
!
By default, the app is set to read-only, so we won’t be able to publish tweets without
changing that to Read and Write. Go to the Permissions tab and change the Access type
to
readwrite access
!
Once saved, head back to the API keys tab and click the button at the bottom to create
an access token – this gives your application access to the Twitter account. Refresh, and
leave the page open for later – we’ll need to copy paste some of those keys.
access token
!
!
Create Your Python Project
!
The camera.py file is the scaffolding for your app, but you need to add some extra code to make
it work. Use the Python picamera documentation to work out what functions need to be called to
make it work.
!
You can run your program from the command line using:
!
sudo python camera.py
!
!
Automation
!
Now you have a python program which takes pictures and posts them to twitter, you can
schedule the script to be run at an interval, say every 30 minutes.
!
To do this we'll use cron. First open the cron table for editing:
!
crontab -e
!
!
Now you'll see the cron file, scroll to the bottom where you'll see a line with the following column
headers:
!
# m h
dom mon dow
command
!
!
The layout for a cron entry is made up of six components:
!
Minute, Hour, Day of Month, Month of Year, Day of Week and the command to be executed.
!
# * * * * * command to execute
# " " " " "
# # # # # #
# # # # # #
# # # # # $%%%%% day of week (0 - 7) (0 to 6 are Sunday to Saturday, or
use names; 7 is Sunday, the same as 0)
# # # # $%%%%%%%%%% month (1 - 12)
# # # $%%%%%%%%%%%%%%% day of month (1 - 31)
# # $%%%%%%%%%%%%%%%%%%%% hour (0 - 23)
# $%%%%%%%%%%%%%%%%%%%%%%%%% min (0 - 59)
!
To schedule for the camera.sh script to be executed every 30 minutes, add the following line:
!
30 * * * * /home/pi/Desktop/pibot/camera.sh &
!
Now save and exit using Ctrl + X, then press Y followed by Enter.
!
!
!
!
Sources: raspberrypi.org, MakeUseOf