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.

prLED

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

wget https://raw.github.com/Rob-Bishop/RaspberryPiRecipes/master/InstallTweetLEDLibraries.sh

Now run the installer by typing sudo: sh InstallTweetLEDLibraries.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:

nano TweetLED.py

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://’+’search.twitter.com/search.json?q=’+twitter_handle) ;

result_list = simplejson.loads(twitter_results.read()) ;

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 TweetLED.py

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.

Advertisements

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

  1. Hi. I have been trying your python script today but keep getting an error:
    File “TweetLED.py”, line 1, in
    import urllib
    File “/usr/lib/python2.7/urllib.py”, 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/urllib.py) 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 “TweetLED.py”, line 16 in
    tweet=latest_tweet(‘@Davidrpi’)
    File “TweetLED.py”, 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 http://search.twitter.com/search.json?q=(insert 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. https://dev.twitter.com/docs/api/1.1/overview.”, “code”: 68}]}
      There website on what has changed is here: https://dev.twitter.com/docs/api/1.1/overview
      Basically the new twitter search requires first a different address, https://api.twitter.com/1.1/search/tweets.json?q=JamesLeftley, which is fine but the second part is that it needs to be authenticated which is explained here: https://dev.twitter.com/docs/auth/oauth. 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 ‘search.twitter.com/search.json?q=’ with ‘twitter.com/search?q=’
      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 twitter_results.read()” 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:

WordPress.com Logo

You are commenting using your WordPress.com 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