Wednesday, August 5, 2015

TwitterPlotBot on Galileo/Edison

As you might have guessed from the title of this post, we are going to build yet another Twitter bot on Galileo. So what does it do differently then? It tweets the picture of the plot of the temperature(or any other parameter that you wish to see) right to your Twitter feed.



So what can it do:
  • It tweets plots :P
  • It can plot any user defined data (explained later) 
  • You can control the tweet of the plot (pause, resume) through direct messaging
  • You can have multiple graphs in single plot
 This is how it looks on your twitter feed:




How does it work?:
For tweeting, the "twython" library is used and for plotting the graphs, gnuplot is used. As was the case with the speaking assistant, this project is also configurable as well. The configurations are stored in "config.py". You can configure things like logging interval, tweet interval, plot title, x axis/y axis label etc...

Registering the Twitter app:
We need to have a twitter account for our TwitterBot. Create a new email account for this bot and head over to  twitter.com. In the "sign up" section, enter the details.


Next, enter the name for your bot


then complete all the steps that come after this.

Once this is done, follow your actual twitter account from this account and vice versa without this, direct messaging won't work.

While you have logged into your newly created Twitter account, click on this(https://apps.twitter.com/app/new) link to create a new application. Enter the application details in this page.


After entering the mandatory fields, accept the terms and click on "Create your Twitter Application"

The next page should take you to your Twitter application's dash board click on
"Permissions" select "Read, Write and Access direct messages" and click on "update settings" to allow direct messaging to work.




Next, click on "Keys and Access tokens"


click on "Create access token" to create the required keys, note down the consumer, access keys and tokens and head over to next section.

Installation:
Boot up your Galileo to IoT Linux image you can refer to this getting started guide to do that.
Once you have logged into the Galileo, type in the following commands in the console.

git init
git clone https://github.com/navin-bhaskar/TwitterPlotBot.git  
cd TwitterPlotBot

The code base contains installation script that will download, compile and install all the required packages.
For https links (secure connection used to download code)  and Twitter authentication to work, you need to set the correct time on your Galileo. Search "UTC  time" in google and get the current UTC time


and using "date" command, set the right time.

date -s "5 AUG 3:45:00 PM"

Enter the following commands to install the required packages:

cd install
sh  install_packages_on_Galileo.sh

When the script is done with it's operation, all the required packages should be installed.
This script will install zlib, libpng, libgd and gnuplot along with Twython and PIL Python packages.

Configuring the application:
All the configuration information are stored in the "config.py" file.  One of the first things that you need to configure is the twitter screen name in place of @your_screen_name substitute your actual Twitter handle ("screen id": "@your screen name"). Then enter the keys (consumer and access token) that you have generated in one of the previous step.


 
 
You can change the log interval and also twitter interval by changing the entries "Log Interval" and "Tweet Interval" respectively. The time interval is to be specified in terms of seconds. Also you can change the commands that you can use to stop and resume the "plot tweeting" by direct messaging these configured strings from your actual Twitter account. 
 
Running the application:
Before running the application itself, you need to set proper time using "date" command as explained above.
In the command line, type the following

python  TwitterBot.py

You can start and stop the plot tweet by sending a direct message to the bot with the command that you have specified in the "config.py" only the messages coming from  the  screen name of the specified tweet handle in the config file are accepted.

Adding custom data to plot:
You can add multiple data point to plot. The file "CallBacks.py" should contain all the functions that are going to be used to get the data. Once you have defined all the functions that will return data (in int or float format), you can register these functions by specifying them in the list "callbacks" defined in the very end of the file. The first parameter within the sub list should be the name of the data item, the next parameter should be the call back function which when called should return the data such as temperature or any other sensor data that you want to log. The last parameter should be the RGB color for the plot in hexadecimal format and should start with a hash.




If you need help with writing python scripts for getting the data from your sensors, you can refer to this series of blog posts.

Coming soon:
Instructable for this project on Galileo and Edison  
Here are the links for Instructables on building TwitterBot for Galileo and Edison.

No comments:

Post a Comment