A few weeks ago I realised I had to get my lazy ass out of my day job Helpdesk routine and needed to do something develop-y again.
A RaspberryPi3 was lying around already and buying more stuff for these little buggers is fun anyway. Tinkering with it even more.
So what did I do, I bought a(nother) Pi Zero W, a display, Pi Camera and cases. None of them used for the Twitterbot ;-(. I’ll use it for other stuff & projects soon (even here!).
I wanted to have the Pi’ tweet something to start getting into Python again and also to play with Twitter or so. And, working for a network monitoring company, I wanted a way to see how the Pi’ were doing health-wise (aka free disk space, free memory, CPU load and temperature and finally uptime – 140 characters can only show so much).
Okay, let’s get cracking.
Ground work – Register a Twitter app
Obviously you have to have a Twitter account. With that head to https://apps.twitter.com and Create a new App.
After creating the App, which will give you Consumer Key (API Key) & Consumer Secret (API Secret), you’ll need to generate Access Token and Access Token Secret.
You’ll define all the keys, secrets & tokens like so:
consumer_key = 'Your_Twitter_App_Developer_Consumer_Key' consumer_secret = 'Your_Twitter_App_Developer_Consumer_Secret' access_token = 'Your_Twitter_App_Developer_Access_Token' access_token_secret = 'Your_Twitter_App_Developer_Access_Secret'
And Scene! (Python)
Okay, what do we need in terms of modules:
from __future__ import print_function from subprocess import PIPE, Popen import time import psutil import tweepy
We need time to calculate the uptime in seconds later on. psutil is to gather all the metrics. tweepy is for the tweeting itself (Who would have thought?!). PIPE and Popen is because we can only get the cpu temperature from the shell. We’ll do that with the following function:
def get_cpu_temp(): process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE) response, _error = process.communicate() return float(response[response.index('=') + 1:response.rindex("'")])
psutil & tweeps via Pip (in virtualenv)
You’ll likely need to install these two modules. You simply:
$ sudo pip install tweepy $ sudo pip install psutil
in a shell. You may need to ‘$ sudo apt-get install gcc python-dev’ to be able into install ‘plutil’ (do so if bash throws an error about python.h into your face when trying to install psutil).
I absolutely recommend to install these modules in a virtualenv(ironment), so that they are not installed globally (psutil may be helpful globally, tweepy may be not so much). In case virtualenv doesn’t ring a bell for you, here is great guide: [A non-magical introduction to Pip and Virtualenv for Python beginners] (trust me it’s not hard at all, and that coming from me, who no zilch about this a couple of days ago, and I’m no rocket scientist!).
While most of the metrics are easy gathering via psutil (again, this from me!), uptime was a bit tricky:
boot_time = psutil.boot_time() time_now = time.time() uptime_seconds = time_now - boot_time uptime_m, uptime_s = divmod(uptime_seconds, 60) uptime_h, uptime_m = divmod(uptime_m, 60) uptime_d, uptime_h = divmod(uptime_h, 24) uptime_string = 'uptime: ' + str(int(uptime_d)) + 'd' + str(int(uptime_h)) + 'h' + str(int(uptime_m)) + 'min' + str(int(uptime_s)) + 's'
I’m sure there are other more efficient ways of doing this, still I’m a bit proud. It calculates the difference of the time now versus the time the Pi booted (in seconds since *epoch*, so 1970-1-1 unix-date-stuff, and then calculates this with **divmod** up into days, and giving me the rest as smaller time unit of each call. This culminates in a surely glorious way of building a string of the uptime [/irony].
Yes, this isn’t the full code. You’ll find it here in my [GitHub-Repository for the Tweetbot].
Further ideas for improvement:
- Put network metrics into this.
- If the tweet string is too long, don’t just simply cut it, but divide it into several tweets.
- Get the installed memory instead of just using 1024 (MB) of a Pi3 to calculate free memory %.
Resources and Footnotes:
- This blog post which gave me the idea to start this project: [Turn Your Raspberry Pi into a Twitterbot]
- From this project I learned how to get the CPU Temperature: [pi-health-dashboard]