Twitter Weather Station

Introduction

In this example we build a simple Raspberry Pi weather and time station that Tweets the temperature and the time, using Python. The purpose of this project is to demonstrate how easy it is to push information out to the internet using the BrickPi, Python, and a few sensors.


Hardware Needed

Building the weather station is very simple. You’ll need:

The BrickPi draws very little power reading the temperature sensor, so it can be powered from the Raspberry Pi.


Setup the Raspberry Pi

Turn on your BrickPi and Raspberry Pi, and connect to it. (Here are some pointers!) Before we begin make sure your BrickPi is connected to the internet. In our example, we used a WiFi Dongle.

The example is located on Github. To get it, you’ll want to clone our Github repository for BrickPi in Python, BrickPi_Python.

In the command line, change directory to where you want to clone the repository. For example, change directory to the Desktop:

cd Desktop

Clone the repository:

After successfully cloning the BrickPi_Python directory we need to import a few more things to the BrickPi.  First, we will make sure we have setuptools installed.  Again in the command line, type:

Install a Python module for interfacing with Twitter.  Again in the command line type:

git clone git://github.com/ryanmcgrath/twython.git
cd twython
sudo python setup.py install


Get Twitter Credentials

We’ll need a way to prove to Twitter that we’re actually who we say we are. Twitter has an authentication process that uses a system called OAuth. This is handled directly by the Python module we imported earlier, twython. We will need to define a set of authorization keys (sort of like a password). To do this, we will setup an Application in Twitter, and then copy down the authorization keys.

First, we’ll sign in to Twitter and then head over to http://dev.twitter.com/apps

Click on “Create New Application”

Fill out the form as you like, adding your website and naming your application. If you were making a Twitter client for Android, this would be more important. Since we’re accessing through Python, the website isn’t important. Use yours, or use www.dexterindustries.com.

Click on “Settings” near the top, and change access to “Read and Write”. This will allow you to send new Tweets (and even read your Twitter stream if you want to).

Now create two new keys: the Access Token and the Access Token Secret. These will be regenerated by a algorithm in the next step.

Under “Details”, click “Create My Access Token”

Now copy down your “Consumer Key”, “Consumer Secret”, “Access Token” and “Secret”.

Caution: Be Careful Who You Share This Data With. Do not post this information online, or share with a friend. As long as the Application you created in Twitter is active, if someone else has these keys, they can access your account. These are best treated as you would treat a password.



Modifying your Program

Now that we have some credentials, let’s put them into the example program. If you open up “BrickPi_Twitter_Example.py” in a text editor, you’ll see that lines 22 – 25 are where the keys we just generated are. If you setup those keys on your laptop or desktop, you can copy them into a text file on a USB drive, and move them over to your BrickPi.

APP_KEY, APP_SECRET, OAUTH_TOKEN, and OAUTH_TOKEN_SECRET are, by default, all 1-0 numbers when you download it. Replace “1234567890” with your keys.

Save the Python file.


Setup the BrickPi

You should notice in the example code that the thermometer should be plugged into sensor Port 1.


Run the Program

Make sure the Raspberry Pi is still connected to the internet. If it is, run the following command:

sudo python BrickPi_Twitter_Example.py

And we should see something like this:



Note the last line, "Tweeted: @dexterind Office Conditions at..." everything after the colon is what we've put up on Twitter.  If we check our Twitter feed, we should see something like this:








Last modified: Monday, 30 November 2020, 7:01 PM