As a first small project with the Pi, I decided to do something really really simple, tweeting some system information with a time interval.
- Programming Language
- How to get system info
- How to have an authorization key
- How to schedule the script
- To reach my goal I decided to use ruby because it’s well supported by the pi and also because right now, it’s my main language. I installed a gem called twitter simply opening a terminal and using this command:
sudo gem install twitter
You can find the documentation here
- To get Pi hardware such as system temperature and system voltage info I used the command vcgencmd; I also used the uname command to get Pi system info
- I also needed an authorization key to access Twitter remotely; this means you need an account to access the developer section and request a secret key. So, if you don’t have any, register and then move to the developer site, sign in with your username and password and go to the apps section of your developer profile, create a new application and follow the provided instructions on how to get your secret key. Once you have it, go to the next step.
- Open your favourite text editor(since I use ssh access to my Raspberry Pi, I use nano) and create a ruby class where you will save your secret key as constants. I prefer, as a programming strategy, to write all my constants – or at least all related constants – in a separate file, so that it can be included everywhere you need it, and here is mine:
CONSUMER_KEY = 'your_consumer_key'
CONSUMER_SECRET = 'your_secret_consumer_key_secret'
ACCESS_TOKEN = 'your_token'
ACCESS_TOKEN_SECRET = 'your_token_secret'
- The basic script is really simple and here it is:
# import twitter gem
# import your constants class, I used require_relative since my class is in a different path
# initialize the twitter client with your secret keys
client = Twitter::REST::Client.new do |config|
config.consumer_key = Constants::CONSUMER_KEY
config.consumer_secret = Constants::CONSUMER_SECRET
config.access_token = Constants::ACCESS_TOKEN
config.access_token_secret = Constants::ACCESS_TOKEN_SECRET
# here is how to get system info. In a linux system if you use backticks to enclose system commands, they
# are executed and the result is saved in the relative variable
# uname for basic system info, kernel version, name etc.
# Raspberry Pi system commands to get basic hardware info
system_temp = `vcgencmd measure_temp`
system_clock = `vcgencmd measure_clock arm`
system_volts = `vcgencmd measure_volts core`
# composing the tweet text
main_text = "Here I am! Wildeng's " + system_info + " running at: "
info_text = main_text + " " + system_temp + " " + system_volts
info_text += ' #raspberrypy #ruby '
# the update function actually tweets the message
I added some comments to the code to clarify some things,: there isn’t a need for a full description since it’s quite simple and self explanatory.
- Scheduling the script was the easiest thing to do, since each nix system has this nice feature called CRON a time scheduler which is easily configurable using the crontab command:
sudo crontab -e
I decided to schedule the execution of the script every hour:
*/60 * * * * sudo ruby path_to_your_script
That’s all! Simple isn’t it? Keep in mind that Twitter could filter similar messages that arrive at short intervals, so choose longer intervals ( I have chosen a time interval of one hour).
If everything went well, now you have a simple twitter bot that sends tweets at predefined intervals!