Glowing Twitter beacon

Make a light that flashes each time a certain phrase is tweeted.

Make it

Firstly if you don’t have a twitter account register for one for free at twitter.com. In order to create apps that interact with twitter you need to register your apps with Twitter. To do this, go to dev.twitter.com and sign into your Twitter account.

Once you have signed in, at the bottom of the page, under the “Tools” column click on “Manage Your Apps”.
Then click on the button “Create New App”.

The next page involves giving twitter some information about your app. For this project you don’t need to fill in the Callback URL.
Name - is the name of the app
Description - what the app does
Website - include a generic webpage as a placeholder. I used my home screen of my facebook. It’s important to use the http:// version of this webpage’s address.
Then read and agree to the terms and conditions.
 
You will now need to adjust permissions so that you can use your Twitter account for the project.
 
Go to the “Permissions” tab. Under “Access” change from “Read” only to “Read and Write” and then click “Update Settings”.
 
Now go to the “Keys and Access Token” tab and at the bottom of the page click “Create my access token”. You will then have a page with all of the information from Twitter that you need to make the program for this project work.
 
There are four unique codes on this page.
Consumer Key (API Key)
Consumer Secret (API Secret)
Access Token
Access Token Secret
 
Make a note of these unique codes.

You will need

A Raspberry Pi
A Pibrella
Usual Raspberry Pi peripherals - mouse, screen, keyboard, power cable, ethernet cable
An additional power supply for Pibrella
Set of battery powered fairy lights - pick ones with low voltage so only running off two AA batteries.
Wire cutters
Soldering iron (optional)
Some wires

Entering your code

  1. Attach your Pibrella board to your Raspberry Pi. There are fewer pins on the Pibrella than on the Raspberry Pi. Just attach the Pibrella starting from the left hand side of pins as you look at Raspberry Pi with the Pi logo the correct way up.
    For this project the Pibrella and the Raspberry Pi both need power so attach a separate power cable to each.
  2. Connect your Raspberry Pi to an HDMI screen. Then attach your mouse, keyboard, power supply and ethernet cable.
  3. Turn on your Raspberry Pi and get to the usual desktop.
  4. Open up the terminal and do the usual updates for the Pi:
    sudo apt-get update
    sudo apt-get upgrade
  5. Because we are using the Pibrella we will also need to import the Pibrella libraries so do
    sudo apt-get install python-pip
    sudo pip install pibrella
  6. Pibrella turns on and off peripherals which require additional power and so you have to run this programme from the sudo level.
    It can be easy to open your text editor with this permission already. To do this type sudo idle & in the terminal and press return.
    This will open a python environment with the correct permissions.
  7. Copy the code at the bottom of this page BUT change the Consumer Key, Consumer Secret, Access Token and Access Secret as indicated in the comments. You will have generated these already when you set up your Twitter App. If you didn’t take a note of them simply log in to the Twitter development environment and go to “Manage Apps” and go to Keys and Access Tokens. You will find these codes there.
  8. Without plugging in the LEDs we can now do a quick test of the program.
    You can use this program to search Twitter for certain words or phrases.
    Pick a phrase but it’s a good idea to start with something that people will be saying a lot - I use “love” and replace where the program says ‘robots’
    i.e. change stream.filter(track=[‘robots’])
    to stream.filter(track=[‘love’])
  9. Run your program in the terminal. Assuming you have entered your unique codes correctly and have copied the program exactly you will see a stream of the phrase “They are talking about this”. Every time this phrase prints it is because someone has tweeted something which contains the word.

If you aren’t sure if it is really working. Change the phrase love or robots to a nonsense word e.g. ‘hellloooooowwwweee’ and then get a friend to tweet the non-sense word/phrase and you should see your program respond by printing ‘They are talking about this’

Attach the LEDs

  1. Power down your Pibrella and unplug the power supply from both the Pibrella and the Raspberry Pi.
  2. Remove any batteries from the battery pack of the fairy lights. If you have a soldering iron you can desolder the wires of the fairy lights from the battery pack. If not simply use the wire cutters or scissors to snip the fairy lights from the battery pack. Gently strip away the plastic covering from the ends of the wire.
  3. Attach one end of the newly exposed fairy light wire to a jumper wire or use an alligator clip to connect it to a thin piece of jumper wire. Repeat for the other loose wire from the fairy lights.
  4. Attach these wires to the e output pins on the Pibrella.
  5. Turn on your Raspberry Pi and in the terminal type sudo idle & to open the text page. If you saved the code from earlier copy it into the the python environment.
  6. Change the phrase robots to your desired search phrase or words, for us it was #xmaslectures, and run your code. You should now see the fairy lights flash whenever anyone tweets your phrase.
  7. Remember don’t leave the fairy lights running while you’re not there because
    a) It wastes electricity if you’re not around to see your flashing lights
     b) It is not a good idea to leave electrical appliances plugged if you are not there - particularly if    you have done some DIY tinkering on them.

Only attempt this project with battery powered fairy lights that run off a low voltage. If you don’t want to cut up or desolder your fairy lights you can also do this project by connecting one or more LEDs (multiple in parallel) with a resistor and connecting these to output e of the Pibrella Board.

Finally, have fun thinking about what other things you could connect to pin e on the Pibrella. It will give approximately a 3V output so can run small motors and solenoids. What other twitter bots could you build with this basic Twitter searching program?

The code

import tweepy

from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Streamimport sys, subprocess, urllib, time, tweepy
#import pibrella

#enter the corresponding information from your Twitter application:
CONSUMER_KEY = ‘___________________’#keep the quotes, replace this with your consumer key
CONSUMER_SECRET =‘____________________’#keep the quotes, replace this with your consumer secret key
ACCESS_TOKEN = ‘_____________________’#keep the quotes, replace this with your access token
ACCESS_SECRET = ‘_________________ ’#keep the quotes, replace this with your access token secret
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET
)api = tweepy.API(auth)

class StdOutListener(StreamListener):
        def on_data(self, data):
        print 'They are talking about this’
 #       pibrella.output.e.on()
  #      time.sleep(1)
   #     pibrella.output.e.off()
        return True

    def on_error(self, status):
        print 'error'

if __name__ == '__main__':
    l = StdOutListener()   
auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)

    stream = Stream(auth, l)
    stream.filter(track=['robots'])
    #this searches for robots and when that is found it flashes the LEDs which are attached to pin e on the pibrella and it returns the phrase They are talking about this

Other things to do with stuff