The IoT Christmas Tree
- Nipun Kanade
Just Another Humble Coder
@ThoughtWorks
This
Is an internet connected tree which changes the colour of its decorative lights to what is requested to it on Twitter
Yo !! I'm all ears...
So how do we build it ?
import RPi.GPIO as GPIO
import time
def blink(pin):
GPIO.output(pin, True)
time.sleep(1)
GPIO.output(pin, False)
time.sleep(1)
return
GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.OUT)
for i in range(0,100):
blink(22)
GPIO.cleanup()
Let's do some PWM thingy and control the brightness of LEDs
import RPi.GPIO as GPIO
import time
import sys
pin = int(sys.argv[1])
freq = int(sys.argv[2])
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
p = GPIO.PWM(pin, freq)
p.start(freq)
try:
while True:
for j in range(5,100):
p.ChangeDutyCycle(j)
time.sleep(0.01)
for k in range(100,5,-1):
p.ChangeDutyCycle(k)
time.sleep(0.01)
except KeyboardInterrupt:
print "exiting"
except:
print "I was murdered"
finally:
p.stop()
GPIO.cleanup()
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. <- Ignore all these.
See this ->
import RPi.GPIO as GPIO
import time
import sys
led_pins = [int(sys.argv[1]), int(sys.argv[2]), int(sys.argv[3])]
colours = [int(sys.argv[4]), int(sys.argv[5]), int(sys.argv[6])]
led_pin = []
GPIO.setmode(GPIO.BCM)
freq = 100
def toDutyCycle(colourCode):
return colourCode*100/255
try:
for i, pin in enumerate(led_pins):
GPIO.setup(pin, GPIO.OUT)
led_pin.append(GPIO.PWM(pin, freq))
led_pin[i].start(toDutyCycle(colours[i]))
led_pin[i].ChangeDutyCycle(toDutyCycle(colours[i]))
while True:
time.sleep(1)
except KeyboardInterrupt:
print "exiting"
finally:
for i, pin in enumerate(led_pin):
pin.stop()
GPIO.cleanup()
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python-setuptools
sudo easy_install pip
sudo pip install twython
#!/usr/bin/env python
import sys
from twython import Twython
CONSUMER_KEY = '** Your Key **'
CONSUMER_SECRET = '** Your Secret **'
ACCESS_KEY = '** Your Key **'
ACCESS_SECRET = '** Your Secret **'
client_args = {'verify': False}
api = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET, client_args=client_args)
api.update_status(status=sys.argv[1])
import RPi.GPIO as GPIO
import time
import sys
import pprint
from twython import Twython, TwythonError
import requests
freq = 100
rPin = 23
gPin = 24
bPin = 18
CONSUMER_KEY = '** Your Key **'
CONSUMER_SECRET = '** Your Secret **'
ACCESS_KEY = '** Your Key **'
ACCESS_SECRET = '** Your Secret **'
client_args = {'verify': False}
twitter = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET, client_args=client_args)
def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setup(rPin, GPIO.OUT)
GPIO.setup(gPin, GPIO.OUT)
GPIO.setup(bPin, GPIO.OUT)
return
def toDutyCycle(colourCode):
return colourCode*100/255
def setLedColour(red, green, blue):
r.ChangeDutyCycle(toDutyCycle(red))
g.ChangeDutyCycle(toDutyCycle(green))
b.ChangeDutyCycle(toDutyCycle(blue))
return
def processTag(tag):
tag = tag.lower()
if tag == 'red':
setLedColour(255,0,0)
elif tag == 'lime':
setLedColour(0,255,0)
elif tag == 'blue':
setLedColour(0,0,255)
elif tag == 'aqua':
setLedColour(0,255,255)
elif tag == 'teal':
setLedColour(0,255,128)
elif tag == 'fuchsia':
setLedColour(255,0,255)
elif tag == 'maroon':
setLedColour(128,0,0)
elif tag == 'white':
setLedColour(255,255,255)
elif tag == 'silver':
setLedColour(192,192,192)
elif tag == 'gray':
setLedColour(128,128,128)
elif tag == 'yellow':
setLedColour(255,255,0)
elif tag == 'olive':
setLedColour(128,128,0)
elif tag == 'green':
setLedColour(0,128,0)
elif tag == 'navy':
setLedColour(0,0,50)
elif tag == 'purple':
setLedColour(128,0,128)
return
while True:
try:
setup()
r = GPIO.PWM(rPin, freq)
g = GPIO.PWM(gPin, freq)
b = GPIO.PWM(bPin, freq)
r.start(0)
g.start(0)
b.start(0)
while True:
time.sleep(10)
results = twitter.cursor(twitter.search, q='%40theIoTree')
for tweet in results:
if tweet['entities']['hashtags']:
data = [tag['text'].encode('ascii', 'ignore') for tag in tweet['entities']['hashtags']]
for tag in data:
processTag(tag)
print data
break
except KeyboardInterrupt:
print "exiting"
except Exception, e:
print "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
print str(e)
processTag('red')
time.sleep(4)
processTag('black')
finally:
r.stop()
g.stop()
b.stop()
GPIO.cleanup()
more pictures !!
twitter : @kanadenipun