In this Instructable, we are going to build something that is going to bring together social networking and data analytics(sort of).
We are going to build a TwitterBot that tweets plots of temperature (or any user defined data).
This bot will create the plots using the "gnuplot" program and uses Twython Python library to tweet the generated plots.
This bot can log data and tweet plots at configurable intervals. The data that has to be logged and subsequently plotted is user configurable and hence you can log any data that you wish to record and plot. Also it is possible to plot multiple data sources.
Step 1: Creating the Twitter Account
Follow these steps to create a Twitter application for your TwitterBot
Create a Twitter account:
- Create an email account for your twitter bot and then head onto www.twitter.com.
- In "sign up" section, enter the details.
- Enter a name for your Twitter bot
- Go ahead and complete rest of the account creation process
Once the account is created, find your actual Twitter account and follow your account from the TwitterBot's account and vice versa. That is, your actual Twitter account should follow your Twitter bot and your Twitter bot should be following your account.
Create the Twitter application:
You need to create an Twitter application to create the Twitter bot.
Follow these steps to create a Twitter bot:
- While still logged into you Twitterbot's application, click on this link.
- Enter the mandatory details for your Twitter application
- Accept the terms and click on "Create your Twitter Application"
- In the next page, click on "permissions" and select "Read, Write and Access direct messages"
- Click on "update settings" Click on "Keys and Access tokens"
- Click on "Create access token" and note down the keys that appears
We will use these keys later, hence keep this tab open
Step 2: Preparing the Edison
Configure the wifi on Edison, if you have not already done it. You can refer to this link to configure the wifi on Edison.
Follow the same above link to gain information on getting access to the console.
We need to install the 'git' program hence you'll have to update the repo list.
Open the repo list config file with vi to start ediing, enter following in the console:
press 'i' key to start editing and paste the following 3 lines.
src/gz all http://repo.opkg.net/edison/repo/all src/gz edison http://repo.opkg.net/edison/repo/all src/gz core2-32 http://repo.opkg.net/edison/repo/core2-32
If you are using putty, copy the above text and right click in the console window to paste. If you are using Linux like terminal (screen, minicom etc... within gnome terminal), use 'ctrl+shift+v' to paste the text.
Hit 'esc' key then ":wq" (colon w q) to save the changes (w-write) and (q)uit.
opkg update opkg install git
Step 3: Installing TwitterPlotBot
Type the following command onto your Edison's Linux console:
git clone https://github.com/navin-bhaskar/TwitterPlotBot.git cd TwitterPlotBot cd install sh install_packages_on_Edison.sh
The last command will download and install all the required packages this command will take few minutes to complete.
Step 4: Configuring the System
All the configuration information is stored in the "config.py" file within the project folder.
Open the config.py with the following command
and hit 'i' key to start editing.
The first thing that you need to configure is the consumer/api and access keys.
Refer to the attached mails where you need to paste the information.
Remove the user information fillers (such as 'consumer key here', excluding quotes) and copy paste the keys.
If you are using "putty" as your serial console, place the cursor between quotes and right click to paste the copied key.
If you are using "screen" then use "ctrl+shift+v" to paste the text.
You also need to specify your twitter screen name in place of "your screen name".
Other things that you might want to configure are "Tweet Interval" and "Log Interval" which control the the time interval between Tweeting the plot and logging the data respectively in terms of seconds.
The "stop plot cmd" and "resume plot cmd" string values control the strings that you can direct message to your bot to stop and start the tweets.
Step 5: Using the Application
Connect the temperature sensor to "A1" pin header on the Grove shield.
On command line, type the following command to start the application
This should start tweeting the plots.
Any time you need to stop the plot you have to direct message the TwitterBot with the "stop command" as specified in the config.py similarly, you can resume tweeting by sending the "resume command". That would be "#stoptheplot" and "#resumeplot" in default configuration.
If all you need is temperature plots being tweeted by the TwitterBot then this Instructable, for you, ends here.
If you want to configure the bot to plot other data or temperature sensor connected to different pin then head over to the next step.
Step 6: Adding Custom Data Logging (optional)
All the data collection functions can be found in the "CallBacks.py" file.
Let us take an example of adding the plot of voltage across the "A0" pin on the Galileo.
The first thing to do is define a function that will set up the analog pin and will read the voltage at that pin and returns the voltage.
def getPotVltg(): reading = mraa.Aio(0) vltg = (reading.read()/1024.0)*5 # switch is towards 5v return vltg
Then add this function to the list called "callbacks" (after the comma).
Here you need to specify a string that will be displayed to identify the plot, next comes the call back function that you have defined and the last parameter is the color of the plot in hexadecimal format starting with #
callbacks = [ # The data item name The callback func The color in the plot for this item [ 'Temperature', getTemperature, "#FF2050"], [ 'Pot vltg', getPotVltg, "#2F3055"], ]
You need to restart the application for these changes to take effect. If you need instructions on using the Python to program the Edison/Galileo, you can refer to this tutorial.