Tuesday, April 1, 2014

RasPI + RGB LED = Color of Twitter

This project uses a Raspberry PI to scan all posted Tweets in real time for the mention of a color.  When a color is Tweeted  the Red, Green, and/or Blue segments of a RGB LED are turned on to display the Tweeted color.  Video demo below:

-----
The project is pretty cool and simple to duplicate.  You should be able to just copy/past my Python script below into your favorite RasPI editor and go from there.

Note that the code expects the RGB LED to be connected to I/O Pins 11 (Red), 15 (Green) and 13 (Blue).  Also, be sure to add a current limiting resistor to each of the three I/O pins; not the RGB LED ground pin.  My RGB LED was spec'd for 330 Ohm resistors.  This picture should help identify Pins 11, 15, and 13:
-----
You will also need to establish your own Twitter API Token.  Don't worry; it's easy if you already have a Twitter account.  To get them go to https://dev.twitter.com/.  Enter these API Token values where the X's are in the source code below.
-----
#  Program to search Twitter to control a RGB LED
#  by WhiskeyTangoHotel.Com with special thanks to Sparkfun and twython
#  Tracks a tally count after each find
#  APRIL 2014

import time
import datetime # to allow timestamp math
import RPi.GPIO as GPIO
from twython import TwythonStreamer

#  PI I/O 11 = Red
#  PI I/O 13 = Blue
#  PI I/O 15 = Green
#  Red + Blue = Orange
#  Red + Green = Pink
#  None on = Black

# GPIO pin number of LED
Red = 11
Blue = 13
Green = 15

# Setup GPIO as output
GPIO.setmode(GPIO.BOARD)
GPIO.setup(Red, GPIO.OUT)
GPIO.output(Red, GPIO.LOW)

GPIO.setup(Green, GPIO.OUT)
GPIO.output(Green, GPIO.LOW)

GPIO.setup(Blue, GPIO.OUT)
GPIO.output(Blue, GPIO.LOW)

# Twitter application authentication

APP_KEY = 'xxxxxxxxxxxxxxxxxxxx'
APP_SECRET = 'xxxxxxxxxxxxxxxxxxxx'
OAUTH_TOKEN = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
OAUTH_TOKEN_SECRET = 'xxxxxxxxxxxxxxxxxxxx'

# Search terms placed in array TERM[]   # will find any term within the array ['one', 'two', 'three']   CASE SENSITIVE
TERM = []
TERM.append('red') #TERM[0]
TERM.append('blue') #TERM[1]
TERM.append('green') #TERM[2]
TERM.append('orange') #TERM[3]
TERM.append('pink') #TERM[4]

LED_secs_on = 30  # when found; how long to burn the LED

localtime = time.asctime( time.localtime(time.time()) )
print localtime
t0 = datetime.datetime.now()  # for timestamp math
print 'Self testing RGB LED...'
#Test RGB LED
i = 0
for i in range(0,5):
print 'RED...  GREEN...  BLUE...'
GPIO.output(Red, GPIO.HIGH)
time.sleep(0.3)
GPIO.output(Red, GPIO.LOW)

GPIO.output(Green, GPIO.HIGH)
time.sleep(0.3)
GPIO.output(Green, GPIO.LOW)

GPIO.output(Blue, GPIO.HIGH)
time.sleep(0.3)
GPIO.output(Blue, GPIO.LOW)
i = i + 1

print ' '
print "START searching for TERMS: "
print TERM[0]
print TERM[1]
print TERM[2]
print TERM[3]
print TERM[4]
print '................................'
print ' '

Tally_0 = 0
Tally_1 = 0
Tally_2 = 0
Tally_3 = 0
Tally_4 = 0

# Setup callbacks from Twython Streamer
class BlinkyLED(TwythonStreamer):
        def on_success(self, data):
global Tally_0
global Tally_1
global Tally_2
global Tally_3
global Tally_4

if 'text' in data:
check_string = data['text'].encode('utf-8')

if TERM[0] in check_string:
print TERM[0] + ' found on ' + time.asctime( time.localtime(time.time()) )
print ' '
Tally_0 = Tally_0 + 1
print data['text'].encode('utf-8')
GPIO.output(Red, GPIO.HIGH)


if TERM[1] in check_string:
print TERM[1] + ' found on ' + time.asctime( time.localtime(time.time()) )
print ' '
Tally_1 = Tally_1 +1
print data['text'].encode('utf-8')
GPIO.output(Blue, GPIO.HIGH)

if TERM[2] in check_string:
print TERM[2] + ' found on ' + time.asctime( time.localtime(time.time()) )
print ' '
Tally_2 = Tally_2 +1
print data['text'].encode('utf-8')
GPIO.output(Green, GPIO.HIGH)

if TERM[3] in check_string:
print TERM[3] + ' found on ' + time.asctime( time.localtime(time.time()) )
print ' '
Tally_3 = Tally_3 + 1
print data['text'].encode('utf-8')
GPIO.output(Red, GPIO.HIGH)
GPIO.output(Green, GPIO.HIGH)

if TERM[4] in check_string:
print TERM[4] + ' found on ' + time.asctime( time.localtime(time.time()) )
print ' '
Tally_4 = Tally_4 + 1
print data['text'].encode('utf-8')
GPIO.output(Red, GPIO.HIGH)
GPIO.output(Blue, GPIO.HIGH)

if TERM[0] in check_string or TERM[1] in check_string or TERM[2] in check_string or TERM[3] in check_string or TERM[4] in check_string:
print ' '
print 'SCORE:'
print TERM[0] + ' = ' + str(Tally_0)
print TERM[1] + ' = ' + str(Tally_1)
print TERM[2] + ' = ' + str(Tally_2)
print TERM[3] + ' = ' + str(Tally_3)
print TERM[4] + ' = ' + str(Tally_4)
print ' '
print str(Tally_0 + Tally_1 + Tally_2 + Tally_3 + Tally_4) + ' total finds after ' + str(datetime.datetime.now() - t0)
print '--------------------------------'
print ' '
time.sleep(LED_secs_on)    # keep LED on for xx secs
GPIO.output(Red, GPIO.LOW)  # turn off the LED
GPIO.output(Green, GPIO.LOW)
GPIO.output(Blue, GPIO.LOW)

# Create streamer
try:
        stream = BlinkyLED(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
        stream.statuses.filter(track=TERM)
except KeyboardInterrupt:
        GPIO.cleanup()

-----
That's it.  Hope you give it a try!