# 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 = '' APP_SECRET = '' OAUTH_TOKEN = '' OAUTH_TOKEN_SECRET = '' # 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()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question