Today I’m going to show you how to tweet some system information from your Raspberry Pi. In part 2, we did a basic tweet entered at the command line, with a standard, fixed, default message if no tweet text was entered.
But That Default Text was Pretty Boring
So let’s do something more fun with it. Let’s make it tweet the time, date and processor temperature if you don’t enter any tweet text.
I got the ‘tweet the cpu temperature’ idea from Chris Mobberly’s blog. Here’s a condensed code snippet we’re going to borrow from Chris….
import os cmd = '/opt/vc/bin/vcgencmd measure_temp' line = os.popen(cmd).readline().strip() temp = line.split('=').split("'") tweet_text = 'My Current Processor Temperature: '+ temp + ' C' api.update_status(status=tweet_text)
We’ll mix it up with some of our own code from the previous script and add a bit of new.
You Can’t Tweet the Same Thing Twice in a Row
When I was messing around with Chris’ script, an issue arose when two successive tweets were of the same temperature value. Tweepy threw an error. It won’t let you tweet the exact same thing twice in a row. So if you run the program twice without any tweet text, and the temperature is the same, it won’t send the second tweet.
The way round this was to make each tweet unique by ‘time-stamping’ the tweets. Having got that working, I decided they needed the degree symbol too. So I retrieved the correct unicode character from a script I’d written in the past.
Here’s the Code
In lines 5-6 we’re importing some new libraries to handle the temperature query and time-stamping.
In line 8 we take a snapshot of the time and date
In line 9 we assign a variable with unicode character for the degree symbol
Lines 11-25 are the same as part 2
Lines 27-33 deal with reading the CPU temperature and tweeting it, if no tweet text is entered.
Lines 35-38 are the same as part 2
#!/usr/bin/env python2.7 # tweet2.py by Alex Eames https://raspi.tv/?p=5941 import tweepy import sys import os from datetime import datetime i = datetime.now() degree = unichr(176) # code for degree symbol # Consumer keys and access tokens, used for OAuth consumer_key = 'type in your consumer key here' consumer_secret = 'type in your consumer secret here' access_token = 'type in your access token here' access_token_secret = 'type in your access token secret here' # OAuth process, using the keys and tokens auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) # Creation of the actual interface, using authentication api = tweepy.API(auth) if len(sys.argv) >= 2: # use entered text as tweet tweet_text = sys.argv else: # if no entered text, tweet the temp now = i.strftime('%Y/%m/%d %H:%M:%S') cmd = '/opt/vc/bin/vcgencmd measure_temp' line = os.popen(cmd).readline().strip() temp = line.split('=').split("'") print now + ' Pi Processor Temperature is '+ temp + ' ' + degree +'C' tweet_text = now + ' Pi Processor Temperature is '+ temp + ' ' + degree +'C' if len(tweet_text) <= 140: api.update_status(status=tweet_text) else: print "tweet not sent. Too long. 140 chars Max."
The first time I ran it, it just tweeted the text I entered. (Remember to use quotes.)
The next time, with no entered text, it displayed and tweeted the full Date, Time and CPU temperature information.
What Can You Do With It?
We're now well on our way to integrating twitter into a hardware application, like a weather station. You could use this method to tweet any kind of input you can read with a Pi. The possibilities are almost limitless. It's really quite exciting.
But before we start on that side of things, there's one or two more useful tweepy technique I want to cover. Next in the series will cover how to add a photo to a tweet. This is where it starts to get really interesting.