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 firstname.lastname@example.org:Howchoo/Pi-Tweeter.git
Then cd into the repo.
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.
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'
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'
To send the tweet, simply call the tweet.py file:
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.