TweeterBie
TWEETER BIE - (A Portable CubieBoard powered Display Board to Display the Latest Tweets)
Abstract:
In this era of any time communication having a 24 X 7 connection with the social networking sites is a necessity. This project “TWEETER BIE” is one such device that constantly updates the tweets from the ever popular social networking site www.tweeter.com .
The “TWEETER BIE” uses a basic socket protocol connection to establish a secure http connection and download the latest tweets from your account transfers it to the Arduino board on a serial communication port and displays it on the 16 X 2 LCD Display connected to the GPIO (General Purpose Input Output) pins of the Arduino.
Goal and Components
The TweeterBie is a device consisting of
1. CubieBoard A20 (CubieBoard 2)
2. Arduino Uno or any other compatible boards (I am using freeduino)
4. Internet Connection… lots of it…
The goal of the project is to display latest tweets on the 16x2 Display. Using the above components
The Hardware Components required to complete this project are
1. CubieBoard A20 (CubieBoard 2)
2. Arduino Uno or any other compatible boards (I am using freeduino)
4. Ethernet cable, USB Mini B Cable for NAND flashing, USB 2.0 Type A to B Converter Cable for Arduino
The Hardware Components optionally required are
5. Wi-Fi Dongle
6. Serial to USB Converter and related cable and drivers
7. Micro SD Card (Preferably > 4GB)
Regular Requirements in Programming:
8. A laptop or a PC with USB and LAN or Wi-Fi Support
9. A LAN Network with optional Wi-Fi Capability but compulsory Internet Connection
You need a lot of software’s to be download so it’s better to have a high speed Internet connection.
The Software Downloads required are:
1. Phoenix / Live Suite, Live suite is for MAC and LINUX and Phoenix is for Windows
2. An Operating System / Firmware to Boot on CubieBoard preferably Ubuntu Flavoured
a. Cubieez (I am Using this for this Project) around 550MB (prebuilt X11VNC)
b. Cubian
3. A Putty SSH Client to SSH into the CubieBoard.
4. Termite a serial terminal emulator or any other client
5. Advanced IP Scanner or Angry IP Scanner
How to Start
First Purchase all the required components from ebay or your local electronic stores.
Once you open the box of the CubieBoard you will be greeted with following picture:
A boxed CubieBoard A20
CubieBoard cased inside an acrylic cover with Add-on Package for SATA Hard Disk Connectivity, Power supply board for 12v Compatible SATA Disks and USB to 5v DC power Adapter cable
Ok so let’s boot-up:
First connect the USB to 5v Adapter Cable as Shown the RED LED Shows the Power Up
Followed by blinking Green and Blue LEDs in a few seconds
What happens now is that it will start booting an android OS which is pre flashed on its NAND chips as shown here on the YouTube video.
But what we need is a Linux and preferably an Ubuntu based operating system on which we can run python and use serial and socket connection to constitute a TweeterBie.
What Next....? Where Is Linux...?
There are two
thinks one can do at this point
1. We can overwrite the NAND Flash with a Linux Operating System.
2. We can use a MicroSD card burn the OS on to it and boot the CubieBoard in Linux.
The procedure I am following here is step 1
To do that we need a USB mini B cable to Standard USB cable as shown below.
Now Connect the USB mini B end to the OTG port on the CubieBoard.
Now let’s shift gears and concentrate a little on software part.
The things we need to download for completing this procedure is
1. Phoenix / Live Suite, Live suite is for MAC and LINUX and Phoenix is for Windows
2. An Operating System / Firmware to Boot on CubieBoard preferably Ubuntu Flavoured
a. Cubieez (I am Using this for this Project) around 550MB (prebuilt X11VNC)
b. Cubian
c. Lubuntu and more..
3. A Putty SSH Client to SSH into the CubieBoard.
4. Termite a serial terminal emulator or any other client
5. Advanced IP Scanner or Angry IP Scanner
· Use administrator rights to install the software’s.
First extract and install Phoenix / Live Suite and when it asks to install a driver give yes and continue.
Second extract the compressed NAND file that you have downloaded on to a folder, This will give an IMG extension file.
Once this is done we are ready to flash the firmware on to the NAND Chip.
Follow the instructions carefully and you must not have any problems.
1. Disconnect the USB cable connected to the 5v adapter of CubieBoard.
2. Remove SD card from the CubieBoard if inserted.
3. Connect the OTG port to PC using USB cable as mentioned before.
As soon as you do this the CubieBoard gets powered up and starts booting the Android or any other OS that currently resides inside the NAND.
We need to stop this
4. So remove the cable in PCs End (This will Power down the CubieBoard) Hold down the FEL button on the other side of the OTG Port on the CubieBoard.
5. Now connect the USB Cable to PC and once the RED LED Glows up release the FEL button.
(Your PC will now detect a USB Device)
6. Now Open Phoenix Suite, Select Firmware on the top menu , Browse and choose the extracted IMG file of the OS.
7. Click Upgrade. (Wait for the pop up box which says “Upgrade Firmware to this device”)
8. Don’t Press anything (neither No or Yes), Just remove the CubieBoard USB cable from PC end hold the FEL switch back as said in step 4 and reattach the cable back to the PC.
9. Release the FEL, This will bring up another POP up Window saying “Does Mandatory Format?”
10. Press Yes, and it will start flashing the NAND with the new OS Image.
11. This will take few minutes around 8 to 10 depending on the image size.
12. Press Okay at the end of Flash Process and exit the Phoenix Suite.
Ok Now the CubieBoard will automatically reboot and Boots up The OS in NAND Chip, in my case its CUBIEEZ,
Connect your Monitors or HDMI displays, Your Ethernet Cables or Wi-Fi Dongles, Mouse, Keyboard use a Hub if necessary.
Info: users: root / cubie unified password: cubieboard (change it on first boot, please) Default IP: 192.168.1.124 (edit /etc/network/interfaces or use NetworkManager)
The above credentials is for CUBIEEZ for other OS refer the related websites
>>> The above method of connecting all peripherals
readily gives you access to the Ubuntu Environment which is the easier way.
But I did it through the geeky way, It’s little hard but much more fun to do.
The Geeky Way
This is what I did, In My Configuration I had
No Spare Mouse or Keyboard, No Ethernet Connectivity,
Only Wi-Fi Dongle and a USB to RX-TX Converter with connecting wires
For this I first installed the Termite Serial terminal emulator and connected my TTL to USB converter to the PC installed all required drivers for the same and kept it ready to initialize communication.
Next in the TTL to USB converter end I took three wires one from each of the Rx, Tx and Gnd Pin and connected them to Tx, Rx and Gnd pin on the Hardware Debug Serial Interface provided on top of the CubieBoard respectively. Leave the vcc in open. Connecting 5v will damage the CubieBoard
Now open the Termite in your PC choose settings
Select the appropriate COM port (In my case its COM4) Set the following parameters:
Baud rate: 115200
Databits : 8
Stop bits : 2
Parity : None
Transmitted text: Append CR + LF as shown in above figure.
Note: You can also use with just LF or CR but that depends on the OS you are using
Now there is something very interesting one can do,
Remove and reconnect the power supply from CubieBoard either or both of the 5v DC adapter or the OTG cable connected to the PC.
Once reconnected you can see all the background process running in the CubieBoard in the Termite window as shown below
Press enter on the Keyboard by keeping cursor in the text box at the bottom of the TERMITE.
Now the screen presents login shell:
Enter “cubie” as user name and press enter (or click on send) , when asked for password type “cubieboard” and press enter(or click on send).
This will bring us to the Cubie Shell to program and execute all our future programs.
Type “sudo ifconfig” and press enter
When asked for the password from now on use “cubieboard”
Or give “SU” first then enter the password as “cubieboard” this gives you root access and need not have to use sudo from now on.
And you can run all shell commands under the root user in this terminal window.
Assuming you have a Monitor supporting the HDMI or DVI input, a USB mouse, and a USB keyboard you need not have to SSH to the board, you just have to connect them and start programming.
But if you are like me who likes hard roads then follow the below tutorial.
AND if you want the improbable road of success refer my Next Tutorial…
Assuming you have a LAN network with an Ethernet cable, you can follow the below given tutorial.
This tutorials helps you in establishing SSH connection to the CubieBoard and running X11VNC server on it to access the GUI desktop for the Cubieez.
First connect the LAN to the CubieBoard as shown below.
Next install and open Advance IP Scanner on your laptop or a desktop PC.
Set the IP range or click [IP] button to scan the subnet of the current network you are on.
And click scan.
After sometime you will get the list of all devices connected on to your Network.
Since the CUBIEEZ OS on the CubieBoard sets the default LAN0 IP to 192.168.1.124 you will get a device mac address of the CubieBoard on that particular IP (192.168.1.124) on Advance IP Scanner.
Now open up PUTTY (The other window in screenshot is Wi-Fi Guard you can neglect it)
Enter the respective IP (192.168.1.124) and port no (22) and open the SSH portal.
When it asks for Security Verification give “Yes”.
Now you have Shell access to the CubieBoard use login: “cubie” password: “cubieboard”
Now we need GUI access to the CubieBoard to do that run shell with root access.
Ø su
Provide password as “cubieboard”
Since X11VNC is preinstalled on Cubieez you just have to start the server.
Ø sudo x11vnc -xkb -noxrecord -noxfixes -noxdamage -display :0 -auth /var/run/lightdm/root/:0 –usepw (with password)
Ø sudo x11vnc -xkb -noxrecord -noxfixes -noxdamage -display :0 -auth /var/run/lightdm/root/:0 (without password)
if it asks root password provide “cubieboard”
The documentation on why to use this command line is here, basically this starts up X11 VNC server listening at port no 5900 with lightdm for display window creation, If it doesn’t work once type it again with root access.
Now install and open ULTRA VNC VIEWER, and provide the IP as 192.168.1.124:5900
Click open, this should open the window to the cubieboard.
If the Authentication window pops up give a password “password” or just give logon.
If you haven’t set any password before.
To set password for X11VNC use the command
Ø x11vnc –storepasswd YOUR_PASSWORD ~/.vnc/x11vnc.pass
After logging in you will get
Enter login: cubie and Password: cubieboard
If the VNC crashes Restart the server & adjust the VNC Viewer speed to Medium(Quick Options)
And one more thing don’t close the putty shell it will terminate the X11VNC sever.
Double click on the LX Terminal Icon on desktop and now we have a terminal to work in
Great..!. Let’s stop CubieBoard Programming here and program the Arduino and LCD.
Lets Code Arduino
ARDUINO
It’s a common knowledge to choose Arduino for most of the embedded systems due to its simplicity in programming and vast amount of resources to get the most in small duration of time.
So connect the Arduino board to your desktop/Laptop (from now on called as PC) and fire up the Arduino Software
Now open the program Examples > Liquid Crystal > Hello World as shown below.
Make all the pin connections as described in the program comments at the beginning.
I have represented it below for reference
Now the LCD module is running in a 4 bit Mode instead of 8 bit mode.
Given that the data transfer takes twice as long but it’s sufficient for the TweeterBie.
Arduino Code Re-Engineered.
The following code changes introduces the scroll effect on the text to be displayed.
#include
// already there in the sketch for LCD display
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
char inData[141]; // Allocate some space for the string max of 140 charcters in Tweets
char inChar; // Where to store the character read from serial port
byte index = 0; // Index into array to point the memory location
byte bytesrecvd = 0; // counter to check the incoming bytes
void setup() {
Serial.begin(9600); // Serial Port to send the characters to be displayed
Serial.write("Power On"); // Power up Signalling on the Debug port after establishing connection
lcd.begin(16, 2); // set up the LCD's number of columns and rows
lcd.print("hello, world!"); // Print a message to the LCD Sample from old sketch
delay(1000); // Time lag until message is read out of the LCD
lcd.setCursor(0, 0); // Set LCD cursor to Home Position.
index = 0; // Set character Indexing position to nil (zero)
lcd.setCursor(0, 0); // bring back cursor to home
lcd.print("Latest Tweet: "); // Display the Header on First Line
lcd.setCursor(16, 1); // Go to last column of the Second line and wait for Characters to appear.
}
// This ends the Setup of the Arduino , Now to create the Super Loop
void loop() {
index = 0; // always reset to zero at the beginning of data reception
while(Serial.available() > 0) // Don't enter the section unless data is available to read
{
if(index < 140) // One less than the size of the array get only 140 characters
{
inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it in “inData” array
index++; // Increment where to write next increment the index
inData[index] = '\0'; // Add terminating character (Null character) for the next position
delay(5); // give 5 milliseconds gap to avoid data corruption
bytesrecvd = index; // get the total count of bytes received
}
} // keep loping until data is available
lcd.clear(); // now clear the LCD of previous Data
lcd.setCursor(0, 0); // Go to Home Position
lcd.print("Latest Tweet: "); // write the latest tweet header on first line
lcd.setCursor(16, 1); // go to end of line on second row
lcd.autoscroll(); // start auto scroll (the problem is it will rotate both rows)
for (int thisChar = 0; thisChar < bytesrecvd; thisChar++) // get a character from stored array
{ // this character will increment simultaneously with the text scroll
lcd.noAutoscroll(); // stop scrolling
lcd.setCursor(thisChar+1, 0); // Since Latest Tweet is moved by one position reset the text.
lcd.print("Latest Tweet: "); // Set the text back up in home position
lcd.setCursor(16+thisChar, 1); // set the position for the new character to load into on the display
lcd.autoscroll();// Start Scrolling back again
lcd.print(inData[thisChar]); // Now print the character on the pre-set position
delay (300); // this is the scroll delay required to make text readable.
}
lcd.noAutoscroll(); // If no text is updated stop scrolling.
delay(5); // wait for few microseconds before looping back…
}
Note: The above Code has been attached as INO file
After dumping the code and executing with “ENGINEERING GARAGE” in Serial Port
Interface - Show the Text on LCD
Ok..! Up until now you didn’t need internet, but now on you do...
Lets get back to Cubie Board
The first thing we need to do is update so,
Ø sudo apt-get update
After few seconds..
If there is a problem with update like above figure showing inability to download updates,
Change your default repository by using the software sources on the desktop of cubieboard.
In the Debian Software tab choose Download from > Other
Give Select Best Server and wait for the Cubieez to choose the live server.
If you can’t choose the server even this way open the DNS resolver file and update as follows
Ø sudo nano /etc/resolv.conf
add or modify to the below name servers
nameserver 208.67.222.222
nameserver 208.67.220.220
Use ctrl^o to save the file and ctrl^x to exit the editor.
Try the best server selection now …!
Now try update once more it should work:
Ø sudo apt-get update
Now first we will need a way of communicate with the Arduino and send data.
I have chosen python for scripting
Cubieez has a preinstalled version of PYTHON to test, open terminal and type as follows
Ø python
Once the terminal changes to python port as >>>
It signifies that PYTHON is installed now to exit the prompt use
Ø >>> exit()
There is a tool called pySerial which can be used for the purpose of serial communication more documentation can be found here
Now, before we install pySerial we need PIP to be installed this makes it easier to install the python libraries in the repositories
To install PIP follow the below instructions
$ sudo apt-get install python-pip python-dev build-essential
When the prompt asks permission to download Give Y and press enter for it to start downloading.
Give Yes to install library
Now let’s install the downloaded PIP
$ sudo pip install --upgrade pip
And finally upgrade the virtual environment
$ sudo pip install --upgrade virtualenv
Now installing pySerial is easy
Ø pip install pyserial (use sudo if write error pops up)
It will automatically clean up leaving the following window if error pops use sudo
Now installation of pySerial is successful
Once you install pySerial you can use the miniterm which comes along with it.
To run the miniterm type the following command in the terminal
Let’s look at the ports available in the CubieBoard
Ø python -m serial.tools.list_ports
this will list the available ports
This Introductory tutorial gives a good idea on how to use the pySerial.
My Arduino is connected through USB
so the output of the previous command will now be
Ok let’s fire up mini term with port ttyUSB0 and baudrate of 9600 and make sure you run it in root.
The command for that is python -m serial.tools.miniterm
Ø sudo python -m serial.tools.miniterm /dev/ttyUSB0
or
Ø sudo python -m serial.tools.miniterm –p /dev/ttyUSB0 –b 9600
Whatever you type it will displayed on the LCD. (Its “HELLO” the O is right behind…)
Where's the Tweet
Ok since the Display... part is up and running we will configure the TWEETER part of TweeterBie.
The following can be done directly from the CubieBoard but I am doing this on my PC.
Twitter previously used the php get and post methods with one key authentication but now a days they use ‘OAuth’
This is slightly complicated but gives extra protection to the users.
In order to access the tweets we need to establish a connection to twitter servers authenticate ourselves as the user and authenticate the CubieBoard device and use API calls to get the latest tweets the below given tutorial deals with just that from basics.
Create a Twitter account (don’t worry if you have a previous account you can use that)
Creating Twitter account.
Fill up your credentials … I have used an alias, using Gmail gives an advantage of aliasing the mails that is if you already have registered an Email Id with twitter you can use the same with “+ ”
in my case gowrav.hassan@gmail.com was previously registers so I just had to register with gowrav.hassan+tweeterbie@gmail.com , No need of creating an email ID just for that, if your email Id is not associated with the twitter you can use it as usual,. The use of alias is that any mail sent to gowrav.hassan+tweeterbie@gmail.com is automatically redirected to gowrav.hassan@gmail.com by the google mail servers.
After registering I have myself a Twitter account. Now all I have to do to make the account active is go to Gmail and confirm the Twitter Registration.
After confirmation..
Now to the serious Stuffs. But first read the documentation here, here and here on Twitter app and other API Access authentication.
This is my version of the above guides, (For Write access)
Go to twitter account click on Gear Icon > Settings on the right top of your tweeter page.
Click on Mobile and provide your mobile number and activate it , This way you will have both read and write access to your account from the twitter apps we will be creating.
This step is not done in this project since I will be just reading the tweets.
Now to create access to TweeterBie go to https://dev.twitter.com/ and sign in with the Twitter credentials.
Give your account details and sign in.
Now hover over your Twitter Avatar on top right corner and choose “My Applications”
Click on Create New App
Provide the Application Details for your TweeterBie.
……..
Read the Terms of Service and guidelines,
Tick on [ ] YES I AGREE and click on Create Your Twitter Application.
Tada your application has been created now let’s generate OAuth for the TweeterBie.
Click on API Keys tab. You need those keys & Tokens, but now let’s go back to python Programming
At the bottom of this tab you have Token Actions, Now click on Create my access token.
And give a few seconds to refresh, this will give you a token as follows. If it doesn’t work retry.
Do not use the above token since it has been revoked before you have read this tutorial.
You need a copy of this Access Token and Secret Key for your TweePy in future so keep this handy.
API : TweePy
As said earlier we are using Python for coding and there is
well built API library for Twitter access in python it’s called TweePy, for more info TweePy use this documentation and this Auth tutorial.
First thing we need here is to install the TweePy on to CubieBoard.
Since we already have PIP installed it’s very easy to install TweePy.
Use the following command on the terminal.
Ø sudo pip install tweepy
TweePy Install is now completed.
-----------Optional Method Only Follow if above Procedure Didn’t Work-----------
If you have problem with accessing the TweePy library. You can clone it from git as follows
Else this is unnecessary.
Ø sudo git clone https://github.com/tweepy/tweepy.git
This clones the library.
Ø cd tweepy
This changes the directory to tweepy
Ø sudo python setup.py install
Now we install the tweepy.
--------Optional Method Only Follow if above Procedure Didn’t Work ends Here----------
TweeterBie Coding starts here,
First lets create the work directory “TweeterBie”
Ø mkdir TweeterBie
and lets change the command prompt to point inside the directory
Ø cd TweeterBie
Now we will create a file to store our python code.
Ø nano TweeterBie.py
In the NANO Editor enter the following code referred from this site http://stackoverflow.com/questions/7714282/return... ,
# File Editor : GNU nano 2.2.6 File: TweeterBie.py
import tweepy # imports TweePy Library
import serial # imports pySerial Library
import time # use of delay statement needs this library
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
# Consumer keys and access tokens, used for OAuth > Tweeter User Dependent
consumer_key = 'MfRPzXL7gsfxsB1T1Tdab42lY' # API KEY
consumer_secret = 'O9k5dVSbIxYM4GCQj0IKnRdGkFTKGwaLLEunz2v9jwZriFh2qQ'
access_token = '2526693469-raqqlVcj6jMV36AshYVAX48jvt8Q3BvSuEkrjOM'
access_token_secret = 'CbvbGGxTl50dTishhxjfDklNATHkKaiaedv5zWpVzH86p'
# Consumer keys and access tokens, used for OAuth ends here
# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
# Creation of the actual interface, using authentication
api = tweepy.API(auth)
time.sleep(3); # sleep 3 seconds to initiate arduino
public_tweets = api.home_timeline() # access all tweets from timeline
for tweet in public_tweets:
print tweet.text # prints the all Tweets on terminal
ser.write(tweet.text.encode('utf-8')) # prints text on LCD
time.sleep(8); # Sleep for 8 seconds between tweets
ser.close() # close port
Now Save and Exit . save = ctrl^o and Exit= ctrl^X
If you need regular update use the below code.
# File Editor : GNU nano 2.2.6 File: SuperLoop_TweeterBie.py
import tweepy # imports TweePy Library
import serial # imports pySerial Library
import time # use of delay statement needs this library
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
# Consumer keys and access tokens, used for OAuth > Tweeter User Dependent
consumer_key = 'MfRPzXL7gsfxsB1T1Tdab42lY' # API KEY
consumer_secret = 'O9k5dVSbIxYM4GCQj0IKnRdGkFTKGwaLLEunz2v9jwZriFh2qQ'
access_token = '2526693469-raqqlVcj6jMV36AshYVAX48jvt8Q3BvSuEkrjOM'
access_token_secret = 'CbvbGGxTl50dTishhxjfDklNATHkKaiaedv5zWpVzH86p'
# Consumer keys and access tokens, used for OAuth ends here
# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
# Creation of the actual interface, using authentication
api = tweepy.API(auth)
time.sleep(3); # sleep 3 seconds to initiate Arduino
while True:
public_tweets = api.home_timeline() # access all tweets from timeline
for tweet in public_tweets:
print tweet.text # prints the all Tweets on terminal
ser.write(tweet.text.encode('utf-8')) # prints text on LCD
time.sleep(8); # Sleep for 8 seconds between tweets
time.sleep(30); # sleep 30 Seconds before accessing the API again
ser.close() # close port
The Curtain to the Project
Now lets update the Twitter Tweets and make sure whether it’s working !!!
Warning: Only limitation please don’t give tweets larger than 22 characters…!!
This results line shifting at this point … as shown below.
New tweets:- but displayed in reverse order
1. Welcome to Twitter
2. You rock lol
3. Hi@engineersGarage
4. This is the 4th Tweet
The Outcome of the above tweets by TweeterBie is as given below.
For Infinite Super Loop.
That’s the End of TweeterBie.
Future and Conclusion
Using this one can make home automation and more, depending on Twitter status of the user,
Say. If the tweet is Going Home the AC can be turned On
Or If Tweet is leaving House the AC can be turned off as well as the home security can be put to high alert mode and so on.
To terminate and shutdown the TweeterBie at any time
Use
Ø ctrl^C
the above command terminates the process
Ø sudo halt
This shutdowns the CubieBoard..