Programming
Send automated tweets with Ruby and Raspberry Pi
January 12, 2014
0

Hello folks,

As a first small project with the Pi, I decided to do something really really simple, tweeting some system information with a time interval.

Problem:

  1. Programming Language
  2. How to get system info
  3. How to have an authorization key
  4. How to schedule the script

Solution:

  1. 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

  2. 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
  3. 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.
  4. 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:
    class Constants
            CONSUMER_KEY = 'your_consumer_key'
            CONSUMER_SECRET = 'your_secret_consumer_key_secret'
            ACCESS_TOKEN = 'your_token'
            ACCESS_TOKEN_SECRET = 'your_token_secret'
    end
  5. The basic script is really simple and here it is:
    class TwitterAccessTest
            # import twitter gem
            require 'twitter'
            # import your constants class, I used require_relative since my class is in a different path
            require_relative 'path_to_your_constants_file'
            # 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
    
            end
            # 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.
            system_info=`uname -nmo`
            # 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
            client.update(info_text)
    
    end

    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.

  6. 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!

Enjoy!

 

Leave a Reply

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close