-
Notifications
You must be signed in to change notification settings - Fork 0
/
christmastree.py
232 lines (207 loc) · 7.12 KB
/
christmastree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
####################################################
#import some Python libraries we'll need
####################################################
#import the time library
import time
#import the Raspberry Pi GPIO library (General Purpose In Out)
import RPi.GPIO as GPIO
#import libraries that let us use Twython
from twython import TwythonStreamer
from twython import Twython
####################################################
# set up Twitter info
####################################################
# What we want to look for on Twitter
TERMS = 'jinglebells, caroling, tinsel, mistletoe, sleigh, christmastree, whitechristmas'
#add your own Twitter access keys here
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_KEY = ''
ACCESS_SECRET = ''
########################################################
#initialise all pins and set up for each colour of light
########################################################
print "Setting the mode of the Pi..."
# Set the mode of the board
GPIO.setmode(GPIO.BCM)
print "Setting pin 18 to output for Pink..."
# Setup pin number 18 as output and initialise to "off"
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, False)
print "Setting pin 24 to output for Blue"
# Setup pin number 24 as output and initialise to "off"
GPIO.setup(24, GPIO.OUT)
GPIO.output(24, False)
print "Setting pin 22 to output for Yellow..."
# Setup pin number 22 as output and initialise to "off"
GPIO.setup(22, GPIO.OUT)
GPIO.output(22, False)
print "Setting pin 25 to output for Red..."
# Setup pin number 25 as output and initialise to "off"
GPIO.setup(25, GPIO.OUT)
GPIO.output(25, False)
print "Setting pin 21 to output for Purple..."
# Setup pin number 21 as output and initialise to "off"
GPIO.setup(21, GPIO.OUT)
GPIO.output(21, False)
print "Setting pin 17 to output for Green..."
# Setup pin number 17 as output and initialise to "off"
GPIO.setup(17, GPIO.OUT)
GPIO.output(17, False)
print "Setting pin 5 to output for Orange..."
# Setup pin number 5 as output and initialise to "off"
GPIO.setup(5, GPIO.OUT)
GPIO.output(5, False)
######################################################################
# Setup what to do when the Twython Streamer finds a matching tweet
#####################################################################
class BlinkStreamer(TwythonStreamer):
def on_success(self, data):
#if data contains something labelled 'text'
if 'text' in data:
if 'tinsel' in data['text'] or 'Tinsel' in data['text']:
###########################
# print it out prettily
###########################
print data['text'].encode('utf-8')
print
###########################
#blink LED when tweet seen
###########################
#let the user know what we're doing
print "Pink on..."
#set the output of pin 18 to be True
GPIO.output(18, True)
#sleep for 1 second
time.sleep(2)
#let the user know what we're doing
print "Pink off..."
#set the output of pin 18 to be False
GPIO.output(18, False)
#sleep for 1 second
time.sleep(0.5)
elif 'mistletoe' in data['text'] or 'Mistletoe' in data['text']:
###########################
# print it out prettily
###########################
print data['text'].encode('utf-8')
print
#let the user know what we're doing
print "Blue on..."
#set the output of pin 22 to be True
GPIO.output(22, True)
#sleep for 1 second
time.sleep(2)
#let the user know what we're doing
print "Blue off..."
#set the output of pin 22 to be False
GPIO.output(22, False)
#sleep for 1 second
time.sleep(0.5)
elif 'sleigh' in data['text'] or 'Sleigh' in data['text']:
###########################
# print it out prettily
###########################
print data['text'].encode('utf-8')
print
#let the user know what we're doing
print "Yellow on..."
#set the output of pin 24 to be True
GPIO.output(24, True)
#sleep for 1 second
time.sleep(2)
#let the user know what we're doing
print "Yellow off..."
#set the output of pin 24 to be False
GPIO.output(24, False)
#sleep for 1 second
time.sleep(0.5)
elif 'christmastree' in data['text'] or 'Christmastree' in data['text'] or 'ChristmasTree' in data['text']:
###########################
# print it out prettily
###########################
print data['text'].encode('utf-8')
print
#let the user know what we're doing
print "Red on..."
#set the output of pin 25 to be True
GPIO.output(25, True)
#sleep for 1 second
time.sleep(2)
#let the user know what we're doing
print "Red off..."
#set the output of pin 25 to be False
GPIO.output(25, False)
#sleep for 1 second
time.sleep(0.5)
elif 'whitechristmas' in data['text'] or 'WhiteChristmas' in data['text']:
###########################
# print it out prettily
###########################
print data['text'].encode('utf-8')
print
#let the user know what we're doing
print "Purple on..."
#set the output of pin 21 to be True
GPIO.output(21, True)
#sleep for 1 second
time.sleep(2)
#let the user know what we're doing
print "Purple off..."
#set the output of pin 21 to be False
GPIO.output(21, False)
#sleep for 1 second
time.sleep(0.5)
elif 'jinglebells' in data['text'] or 'JingleBells' in data['text']:
###########################
# print it out prettily
###########################
print data['text'].encode('utf-8')
print
#let the user know what we're doing
print "Green on..."
#set the output of pin 17 to be True
GPIO.output(17, True)
#sleep for 1 second
time.sleep(2)
#let the user know what we're doing
print "Green off..."
#set the output of pin 17 to be False
GPIO.output(17, False)
#sleep for 1 second
time.sleep(0.5)
elif 'caroling' in data['text'] or 'Caroling' in data['text']:
###########################
# print it out prettily
###########################
print data['text'].encode('utf-8')
print
#let the user know what we're doing
print "Orange on..."
#set the output of pin 5 to be True
GPIO.output(5, True)
#sleep for 1 second
time.sleep(2)
#let the user know what we're doing
print "Orange off..."
#set the output of pin 5 to be False
GPIO.output(5, False)
#sleep for 1 second
time.sleep(0.5)
else:
print "nothing"
##################################################################################################
# Create a streamer to watch the twitter feed for tweets that contain the string in variable TERMS
##################################################################################################
try:
stream = BlinkStreamer(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET)
# we're only interested in the ones that contain TERMS so filter them out and
# discard any others
stream.statuses.filter(track=TERMS)
# the following commands make sure the program cleans up
# before it stops running to avoid errors when it runs next
except KeyboardInterrupt:
print("stopping")
GPIO.cleanup()
finally:
GPIO.cleanup()