Twitter LED (Broken/Being repaired)

Note: Before you start this article take note that the LED lighting code and word recognition should still work but twitter have made it so you can’t access tweets this way anymore. You have to sign up to be a developer and insert authentication code. I am trying to currently repair this but twitter aren’t making it easy 😛

For future projects and general usefulness I have decided to try to learn a new programming language. I have decided to try python for two good reasons, because it is a good language to program projects with the Raspberry Pi and because a large chunk of my course next year is programming simulations in python.

For my first program I’m going to do a twitter feed controlled LED, I also plan to incorporate twitter into a later project so this is a good starting point.

Ingredients list: A Raspberry Pi, an LED, a 220 Ohm resistor, Internet access and a Twitter account.

First take an LED and a 220 Ohm resistor and connect the resistor to the cathode of the LED. Then connect the anode of the LED to pin 7 of the GPIO pins on the Raspberry Pi and the resistor to pin 25 using two female to female jumper wires. A GPIO pin layout diagram can be found here. Once you are sure this is done correctly, a mistake could damage the Pi, boot up your Raspberry Pi ( I am using Raspbian Wheezy). This is all the hardware setup.


Now to install the appropriate libraries type this into the command line:


Now run the installer by typing sudo: sh

Okay you know have the appropriate library the next step is to create the file. I’m using nano, to make he file type this command:


In the editor you just opened type the following code, quick tip press CTRL X to save and exit the editor. Note, I am going to put ; at the end of each line to make the program clearer do not put these into your program.

import urllib ;

import simplejson ;

import time ;

import RPi.GPIO as GPIO ;


GPIO.cleanup() ;

GPIO.setmode(GPIO.BOARD) ;

GPIO.setup(7,GPIO.OUT) ;


def latest_tweet(twitter_handle): ;

twitter_results = urllib.urlopen(‘http://’+’’+twitter_handle) ;

result_list = simplejson.loads( ;

return result_list[‘results’][0][‘text’] ;


count = 1 ;


while count > 0: ;

tweet=latest_tweet(‘@JamesLeftley’) ;


if ‘on’ in tweet: ;

print tweet,’ – LED ON’,’\n’ ;

GPIO.output(7,GPIO.HIGH) ;


if ‘off’ in tweet: ;

print tweet,’ – LED OFF’,’\n’ ;

GPIO.output(7,GPIO.LOW) ;


if ‘stop’ in tweet: ;

print tweet,’ – Stopped’,’\n’ ;

GPIO.output(7,GPIO.LOW) ;

count = 0 ;


time.sleep(2) ;

Now save and exit using CTRL X and you should be taken back to the command line. To run the program type:

sudo python

To stop the program tweet stop, to turn the LED on tweet on and to turn the LED off tweet off. The tweets can say anything as long as they include the twitter handle, in this case @JamesLeftley, and the command. All the commands are case sensitive.

To personalise the program change the twitter handle, add more outputs and change the commands.


10 thoughts on “Twitter LED (Broken/Being repaired)

  1. Hi. I have been trying your python script today but keep getting an error:
    File “”, line 1, in
    import urllib
    File “/usr/lib/python2.7/”, line32, in
    from urlparse import urljoin as basejoin
    valueError: bad marshal data (unknown type code)

    Can you help with this?

    • This usually references to a corrupt compiled python install file. First try re running the download and install of the libraries, if this doesn’t work go into the file destination stated in the error (/usr/lib/python2.7/ and deleting any .pyc files. These automatically reinstall next time you start the program. If all else fails update python with the sudo apt-get update command in the linux terminal.

      Let me know if this solves it 🙂

  2. I have started off working to code published on the OCR page, having corrected but typos in their listing using you example I am still left with the error

    Traceback (most recent call last)
    File “”, line 16 in
    File “”, line 13, latest_tweet
    return result_list[‘results’][0][‘text’]
    KeyError: ‘results’


    • I think I have found why everyone seems to be having problems. The website Twitter handle here) no longer functions and comes up with this message: {“errors”: [{“message”: “The Twitter REST API v1 is no longer active. Please migrate to API v1.1.”, “code”: 68}]}
      There website on what has changed is here:
      Basically the new twitter search requires first a different address,, which is fine but the second part is that it needs to be authenticated which is explained here: As far as I can tell without proper time to work with it is that it is going to make this process a lot more complex. If you have any success please let us know and I will update this tutorial if it can be made to work again.

      Thanks for pointing out the error.

    • A quick thing to try that might work whilst I try to figure out the new system is changing ‘’ with ‘’
      No guarantee it will work but it might be worth a shot.
      One thing I have discovered is that you have to become a twitter developer to make the search requests now.

      • Tried your suggestion, while it is not working it does seem to finding the latest Tweet – I have included the statement
        “print” following the urllib line so I can see what is being captured.

        However it still fails with the error ‘simplejson.decoder.JSONDecoderError: No JSON object could be decoded….’

      • The problem is that when you change the address to the one I mentioned it is no longer in the json format so none of the json stuff will work. Try removing the simplejson.loads bit and see what happens but leave the rest of that command line. I’m not sure whether you’ll need the first set of brackets or not after you delete it.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s