Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From \$16.50/m

# How to Build a Tweet Controlled RGB LCD

Difficulty:IntermediateLength:MediumLanguages:

In this tutorial I’ll show you how to use a Raspberry Pi with an RGB LCD to monitor tweets. Tweets containing specific keywords are displayed in defined colours. You’ll learn how to create a Twitter application to use the stream API to push data to your program, giving you an opportunity to learn about multi-threaded Python programs.

## Setup

2. Install Tweepy like so:

sudo pip install tweepy

Create a Twitter Application, on the Twitter web site, to allow the Raspberry Pi program to connect to the stream. To do this navigate to https://apps.twitter.com and sign in.

Create an application by completing the form, as shown below. Note: you can put a placeholder for the website–it's a required field but not required for this Raspberry Pi project.

When you submit the form an application will be created. On the API Keys tab you'll see an API Key and API secret–these are unique to you and will be required later.

Refresh the page if you need to. You'll now have the Access token and Access token secret. Again, these are unique to you and will be required later.

That's the application created.

## The Code

In the code for this project, below, you'll need to edit to edit to enter your API Key, API Secret, Access Token Key and Access Token Secret. You may also need to change the path to the Adafruit_CharLCDPlate so it matches where you've installed it on your Pi.

When you run the program, the Pi will connect to the Twitter Stream. In this example, any tweet containing the word Jeremy will be pushed to the Pi and displayed on the RGB LCD. This is any public tweet from anyone in the world–nothing to do with who you're following–it's anyone!

Remember to use sudo when running the Python script–root privileges are required for the i2c access to the LCD. For example:

sudo python /path/to/script/script.py

Note: The program will not close cleanly by pressing Control-C. Instead, open another shell and use ps ax and kill to terminate the program.

The program looks for other words in the tweet to set the colour of the RGB backlight. If it finds the work Clarkson it will set the colour to red, pearl is green, love is blue.

Finally, if there's time between Tweets, the program will scroll the word-wrapped text so that you can see it all.

## The Code Explained

There are a few key things happening in the code. You can adapt the code for your own purposes, and I encourage you to experiment. There are two threads:

2. the display thread that manages the LCD screen–it displays and scrolls the text.

There's a simple example on the Tweepy github page–this connects to the stream and prints out any tweet containing the word basketball. You can see in that example, more clearly, how a StreamListener class is defined and then passed to the Stream constructor.

I've done the same in my script here: stream = Stream(auth, display_loop_instance) with display_loop_instance

The instance of StreamListener can define a few event methods for Tweepy to call. Just like the example, I'm only using on_data and on_error. Once the connection to Twitter is established on_data is called whenever a new tweet comes in. The tweet data that's received is a UTF-8 encoded JSON document like this:

I'm only interested in the text attribute from this document so I'll use Python's json library to parse the json into Python objects. I can then grab the text and encode it as ascii so that it's compatible with the LCD screen.

In a list comprehension I've word wrapped the tweet into a space padded 16 character width list, ready to display on the screen: [x.ljust(16) for x in textwrap.wrap(str(self.tweet),16)]

Next, I've calledset_backlight–this looks for a secondary word within the tweet text and sets the RGB backlight colour. If it doesn't find a word in the map then the display defaults to white.

Finally I've called scroll_message to force an immediate update on the display.

There are two independent things happening in the program: receiving tweets from the stream and scrolling through them on the display. The display thread takes care of the scrolling and it also monitors the keypad for a secondary function.

The two threads need to work over the same data, the tweets, so to keep things simple the display thread also calls methods on the display_loop_instance

Python has a neat way to start a thread that calls a method on an object. You just have to make your class callable by defining a __call__ method.

In the code this method has a never-ending while loop with a 1 second sleep. This sleep is vital, without it the text will scroll through too fast. Also the thread has to yield so it doesn't needlessly use a lot of CPU time.

An extra useful thing here, and possibly a point where this program could be expanded, is the keypad monitoring. I like to run this program from boot-up so you can take it to work or somewhere and plug it into a LAN on DHCP and it just works.

Sometimes though, you need to find out the IP address you've been assigned so you can ssh into it. In the display thread never-ending loop I've added a simple statement to check if the left keypad button is pressed. If it is, I display the wired and wireless IP addresses on the screen. You can test this by pressing left key.

To run the program automatically at boot simply edit the /etc/rc.local file and add a line to run your program:

python /home/pi/path/to/program/program.py &

Reboot and it should start automatically.

## Conclusion

In this tutorial, I have shown you how to construct a device, using a Raspberry Pi and an Adafruit RGB LCD, to display tweets and to display them in certain colours dependent upon any keywords defined in the tweet itself.

Let me know if you have any particularly interesting ideas for the use of this project or ways in which you have adapted it for your own purposes.