64x64 icon dark hosting
Choose a hosting plan here and get a free year's subscription to Tuts+ (worth $180).

How to Add a Web UI to the RGB LCD Tweet Box


Start a hosting plan from $3.92/mo and get a free year on Tuts+ (normally $180)

Final product image
What You'll Be Creating

This tutorial extends the previous tutorial, How to build a Tweet controlled RGB LCD, by adding web page control. With this you can change the configuration of the Tweetbox on the fly from your laptop, tablet or phone. 

You'll learn useful techniques in this tutorial so that you can add web page control to your own Raspberry Pi projects. 


  1. First complete the previous tutorial How to build a Tweet controlled RGB LCD
  2. Install Tornado:

sudo pip install tornado

All the code for this project is available here:


Stage 1: Basic Form

To recap, by the end of the previous tutorial you had a Raspberry Pi with RGB LCD connected to Twitter receiving public tweets matching a filter. The RGB backlight would change colour depending on other matching words in the tweet. To configure the filter and colour map you had to change the code and restart the program.

In this first stage I'll add a web server to the program. Then you can go to a web page and update the configuration live. I'll be using Tornado, there are lots of different web frameworks for Python and I've used many of them over the years but this is now my go-to framework. It ticks a lot of boxes for a lot of different projects.

Take a look at the Hello world example on the Tornado page so you can see how to set up a handler and start a tornado server thread. I'll be using this exact same principle here.

I'll take the tweetbox.py code from the previous tutorial and add the web framework in. To start with I'll just have a simple form allowing us to change what I'm tracking on Twitter. First, add some imports to the top of the script:

Next I need two handlers: one to show the HTML form and a second to receive the form submission:

Note that MainHandler uses get and ConfigHandler uses post, the HTML form uses the post method to submit the form data back to the web server. To serve the form the MainHandler simply calls the render function to have a template rendered and sent to the browser. More about this in a moment.

When the form data comes back ConfigHandler extracts one argument, config_track. This is sent to a restart function to change the settings with tweepy before returning a simple string showing what is being tracked.

Create the template by adding a directory called templates to the source directory and create the form1.html file there:

This is a very simple form with a text input box and a submit button. It’s all that’s needed at this stage. Next create the restart function:

This disconnects the Twitter stream and then reconnects it with the new filter, track_text, which is whatever was submitted from the form. An important change here, from the previous tutorial, is that the Twitter stream thread runs in asynchronous mode, async=True. This runs the stream connection in a background thread so that the web server runs as the main thread.

Add a couple of lines to the end to start the stream in asynchronous mode and to then start the web server:

This starts the web server listening on port 8888. Point a web browser at http://{your-raspi-ipaddress}:8888/ and you will see the form:

Tweetbox webpage form
Tweetbox webpage form

Enter something else to track like raspberry and click submit. After five seconds it will switch to tracking these tweets instead.

Stage 2: Colour Map and Saving Configuration

In this stage I'll add the colour map to the configuration so you can set the words that trigger the RGB backlight to change. Since there are seven settings I don't really want to re-enter these every time I run the application so I'll save these to a file.

The program uses pickle to save and load the configuration file, plus there's an initial file exists check so add these imports to the top:

The DisplayLoop class is already responsible for managing the backlight_map so I'll extend this so it takes care of what I'm currently tracking, track_text. Read and Write config methods are also added here:

Change the request handlers to take care of the colours. Also below you'll see that when I render the main page I'm passing in the current configuration. This means I can fill the configuration form with the current settings when it's requested.

One technique to note here is how I invert the colour map. When processing it I want the map to be word > colour but when configuring it in the form I want colour > word. A Python dictionary comprehension can be used to invert the map in a single statement: {v:k for k, v in display_loop_instance.backlight_map.items()}

A new HTML form is required to support the colour settings. Also I need to populate the form input boxes with the current settings by making use of Tornado's template system. This is very basic, I'm just taking the values passed in to the render function and pulling them out here in the template for example {{ config_track }}.

Now that I can save and load the configuration, the former restart routine needs to be a little more sophisticated:

Since it's more than a restart it's name is now set_config. It's here where I now call write_config to save the changes to file.

All that's left is a couple of changes to read in the configuration at startup:

And to start the stream from this setting rather than  'jeremy':

Start the program, point a web browser at http://{your-raspi-ipaddress}:8888/ and you will see the form:

Web form
Web form

Stage 3: Finishing Touches

There are a few things that aren't very slick about this program:

  • Delay while updating the configuration
  • The Now tracking... response after submitting the form
  • Ugly form styling

The delay while the configuration is changing is due to the asynchronous nature of the program. There is a thread managing the Twitter stream, a thread scrolling the display and the main thread running the web server. 

When I want to change the settings on the stream I need to disconnect it and then reconnect with the new options. Unfortunately there's no event from tweepy to tell me when I have successfully disconnected and so up until now I have just delayed for five seconds between disconnect and reconnect. 

To remove this delay what I'll do is start a new connection while the old one is disconnecting so I don't have to wait. Of course this means that at one point there may be two streams receiving tweets. This would be confusing on the display since you would see the old tracking and the new tracking combined. 

Therefore, just prior to disconnecting I'll connect the old stream to a listener which does nothing with the incoming tweets. Here is the definition of the NullListener and the changes to the set_config routine:

Regarding the Now tracking… response, the current version of the form is submitted to the ConfigHandler which changes the settings and returns this ugly response. Really what I want is for the form to re-appear with the new settings in place. 

I can achieve this by redirecting the user back to the / URL. Also, there's really no need for the ConfigHandler anyway, I can define the get and post methods on the MainHandler and simply submit the form there instead:

Finally, styling. Making this look really beautiful could be a whole new tutorial in itself, but a really good start is to introduce a framework to take care of a lot of the styling for you. 

I’m  Bootstrap for styling and JQuery for scripting. Both of these are available on CDNs so you don't need to download anything, just include them in the head section of the page:

To make the form look better we'll use Bootstrap's Horizontal Form style:

Finally to put a bit of polish on the UI we'll indicate to the user that the Raspberry Pi is updating when they click the submit button. This involves a little bit of Javascript to capture the form submission event, change the button text to Updating... and disable the button:

And here's the finished web UI:

The finished web form
The finished web form


This tutorial has expanded on the previous one to add a web UI to the RGB LCD Tweet Box. You can now control what you're following on the screen and the backlight colours you want from your phone, tablet or desktop machine.