Tweepi – A Pi Powered Twitter Feed on a 16 x 2 LCD

I was on vacation and as a way to kind of jump start my brain when I got back I took on a fun little project with one of the PI Zero’s I had lying around and an old LCD screen.  It took a bit longer than I expected and really got my gears turning again, that’s for sure.  I’ll be the first to admit that it’s not perfect, I soldered to the board in a pretty messy way and the layout is probably not perfect, so there is room for improvement, but it was a quick “warm up” project to get everything in my brain working again.  It took some google searching and reading a few documents for the modules implemented but it was a great way to get learning.

A quick guide and the source code is at the bottom of this post in case you want to make one yourself.  Here’s a quick video of it in action:




You Will Need:

-Pi zero with wifi or ethernet (or pi zero w)*

-Breadboard (kind of optional if you want to jump to making this permanent right away you can just solder it all together)

-Wires

-16×2 LCD

-Potentiometer

-PI Supplies (Jessie Lite on microSD, Micro USB for power etc.)

*Note: the PI zero will need pins for this to work and that might take soldering.  You can also use a regular PI (1, 2, 3 etc.) but that might be a waste of money compared to the $5 a PI zero costs.

 

1.) Set up your Twitter Development App.

I guess the real step one assumes you have a twitter account.  If you don’t have twitter set up an account first.  Once that is done go to: https://dev.twitter.com/ and get your API keys.  These are the keys that will be used to gather the tweets from your feed.

To get the keys click on “my apps” on the top bar

 

From there Click on “Create New App”

 

 

and then from there click “Manage Keys and access” to get the keys you need

 

It’s pretty intuitive, you’ll need the Consumer key, the Consumer Secret Key, the Access Token Key and the Access Token Secret.  Basically all 4 strings of random numbers and letters need to be inserted into the indicated areas in the code at the bottom of this page.

2.) Now get your PI Zero

The Pi zero, for those that don’t know, is a small and adequately powerful computer.  For 5 bucks it can handle what we will need from it.  It’s pretty bare bones though so You’ll need a micro USB power supply, a micro USB to USB cable for the USB wifi dongle or a Micro USB to Ethernet cable to plug in the internet (Or buy a PI zero W with built in wifi and save yourself some money as those are only 10 bucks).

Full details on how to set up your PI and everything else are beyond the scope of this guide.  From here out I’m assuming you know how to set up a PI and SSH into it.  If you need help there are loads of resources and set up guides out there, here are a few: Setup and SSH.

3.) Install pip

I’m assuming from here out that you’re working from SSH or in the terminal screen.  This whole guide can be done without use of the GUI.

Python is already installed on Jessie Lite.  You will need PIP in order to get the other modules.  To install PIP (a python package manager) type (without the quotes) ” sudo apt-get install pip ”

4.) Install Python Modules

Here are the commands to install the python modules we will need.  There are other ways to do this and having a knowledge of python will help, but the below commands should do it for you.  If you have issue please let me know in the comments.  Here are the commands you will need (exclude the quotes and run one at a time):

” sudo pip install twitter “, ” sudo pip install RPLCD “, ” sudo pip install HTMLParser ”

Before going into the physical set-up

Here is a diagram of how we will be wiring up our LCD and PI.  If you follow this exactly you should be alright, make sure the PI is facing the same direction as in the diagram below.

 

5.) Set up the PI Pins

This is where you might need a little soldering.  I just soldered the pins I used for this but you could put a full header on if you wanted.  I’ll leave it up to you (if you’re not using a stock PI Zero or PI Zero W you can skip this step).

6.) Attach all pins to breadboard and LCD

Or solder them directly.  The set up is as follows (be sure everything is powered off and unplugged):

  • Pi pin 2 to positive on breadboard
  • pi pin 6 to negative (ground) on breadboard
  • Potentiometer ground to ground on breadboard
  • Potentiometer middle pin to LCD Pin 3 (this allows for adjusting the contrast on the LCD)
  • Potentiometer Positive pin to positive on breadboard
  • LCD pin 1 to ground on breadboard
  • LCD pin 2 to positive on breadboard
  • LCD pin 4 to PI GPIO 7 (pin 26)
  • LCD pin 5 to ground on breadboard
  • LCD pin 6 to GPIO 8 (pin 24)
  • LCD pin 7, 8, 9, 10 are empty
  • LCD pin 11 to GPIO 25 (pin 22)
  • LCD pin 12 to GPIO 24 (pin 18)
  • LCD pin 13 to GPIO 23 (pin 16)
  • LCD pin 14 to GPIO 18 (pin 12)
  • LCD pin 15 to positive on breadboard
  • LCD pin 16 to ground on breadboard

I’m sorry for the lack of technical terms in the above steps.  I tried to keep it simple and I’m also not an expert when it comes to the mechanical side of things with PI’s and such.  I’m also assuming you’re using a breadboard, if you’ve opted to solder this all together I assume you know enough of what you’re doing to be able to piece all these steps into something cohesive.

7.) Make the python file (named what you want) and type (or copy) the code below

The simplest way to do this is by typing ” nano tweepi.py “.  This will open a blank text file.  Copy the code below, be sure that it looks exactly as it does below (besides adding in your API keys where indicated, and the color).  Nano is the text editor and “tweepi.py” is the name of the program.  Feel free to name it whatever you want though, just make sure it ends in “.py”.  This is where having a little knowledge of python will help.  The use of white space is a major feature in python, so make sure all the spacing is the same as below.

8.) Add your api’s to the code

You’ll see where this is indicated in line 8, your keys go in line 9 to 12.

9.) Save and exit

‘CTRL x’ to exit nano and be sure to type ‘y’ to save it.

10.) Run 

To run the program we just made type ” python tweepi.py “.  If you named it something else be sure to substitute “tweepi” with the name of your program.

11.) Quit with ‘CTRL z’

After you let it run a bit and see a few tweets from your timeline, if you want to stop it you’ll have to hit ‘CTRL z’ to quit.  It’s not the most graceful exit (and an area that can be improved).

That’s it, it should be working now.  The following steps are optional, but allow you to keep running the program in the background (so you can close SSH or do other things).

12.) Install screen

‘Screen’ allows for you to operate different screens in linux.  Kind of like running things in different windows or mini instances.  To install it type ” sudo apt-get install screen ” and let that run.

13.) Start screen

Once installed it’s simple to start.  Just type ‘ screen ‘.

14.) Run in screen

Once started just run the program again, the same way we did in step 10.  Only this time, once it’s running hit ‘CTRL a’ followed by ‘d’ to detach from the screen session.  Now it’s running in the background.  To go back to that screen type ‘ screen -r ‘ to reattach it.

15.) Watch your live stream

It’s neat to leave running.  Like a little ticker of tweets.  It made me clean up who I follow on twitter so that the tweets I see have a higher chance of being quality tweets, but it keeps my feed at the top of mind.

 

The Code 

import twitter
from RPLCD import CharLCD
import time
import HTMLParser

lcd = CharLCD(cols=16, rows=2, pin_rs=26, pin_e=24, pins_data=[22, 18, 16, 12])

# Insert your API and access keys here between the quotes
api=twitter.Api(consumer_key='',
	consumer_secret='',
	access_token_key='',
	access_token_secret='')

htmlParser = HTMLParser.HTMLParser()

framebuffer = [
    '',
    '',
]

def write_to_lcd(lcd, framebuffer, num_cols):
    lcd.home()
    for row in framebuffer:
        lcd.write_string(row.ljust(num_cols)[:num_cols])
        lcd.write_string('\r\n')

from RPLCD import CharLCD

write_to_lcd(lcd, framebuffer, 16)


def loop_string(string, lcd, framebuffer, row, num_cols, delay=0.5): #delay controls the scroll speed
    padding = ' ' * num_cols
    s = padding + string + padding
    for i in range(len(s) - num_cols + 1):
        framebuffer[row] = s[i:i+num_cols]
        write_to_lcd(lcd, framebuffer, num_cols)
        time.sleep(delay)

while True:
    homeTimeline=api.GetHomeTimeline(count=1)
    tweetUser = homeTimeline[0].user.screen_name
    tweetText = homeTimeline[0].text
    tweetText = htmlParser.unescape(tweetText)
    tweetText = tweetText.replace('\n',' ')
    tweetText1 = tweetUser+ ": "+tweetText
    long_string = tweetText1
    # looping 5 times to keep the display active while adding a delay on excessive api calls
    loop_string(long_string, lcd, framebuffer, 1, 16)
    time.sleep(10)
    loop_string(long_string, lcd, framebuffer, 1, 16) 
    time.sleep(10)
    loop_string(long_string, lcd, framebuffer, 1, 16) 
    time.sleep(10)
    loop_string(long_string, lcd, framebuffer, 1, 16)
    time.sleep(10)
    loop_string(long_string, lcd, framebuffer, 1, 16)
    time.sleep(10)

A quick “To Do” here would be the looping being a little messy. There is a limit to the number of API calls that can be made to twitter before you get timed out and the looping that I have is not an ideal way to deal with that. A neat solution would be to retrieve a list of tweets and loop through those before doing another call and getting a fresh list.

Thanks for reading!  It’s my first attempt at any kind of technical guide so I apologize for any mistakes or omissions.  Please let me know in the comments if there are any areas that are wrong or need improvement. If you enjoyed this please subscribe (see the sidebar) to never miss a post. Or follow me on twitter or facebook.

Also check out the shop for some books, short stories and other goods.

Support.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 712 other subscribers

Leave a Reply