Last active
September 4, 2016 01:28
-
-
Save chtzvt/f0fda3cdb32695a68776560bbf915b14 to your computer and use it in GitHub Desktop.
Python script that continually sets the color of your tikteck bulb to the current Cheerlights color.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import tikteck # See https://github.com/mjg59/python-tikteck | |
import urllib2 | |
# Bulb connection information - HW address, name and password specific to your bulb. See tikteck repo for additional info. | |
bulb_config = ("00:21:4d:00:00:01", "Smart Light", "password") | |
def updateColor(bulb): | |
try: | |
latest_color = urllib2.urlopen('http://api.thingspeak.com/channels/1417/field/2/last.txt'); | |
rgb_hex = parseHex(latest_color.read()); | |
except: | |
rgb_hex = parseHex('#ffffff'); # If latest color cannot be retrieved, default to white. | |
brightness = '0x80' # Default brightness is 50% e.g. hex(256/2) | |
bulb.set_state(rgb_hex[0], rgb_hex[1], rgb_hex[2], brightness) | |
# Converts hexadecimal string into tuple of (r, g, b) hex values | |
def parseHex(str): | |
return ('0x'+str[1:3], '0x'+str[3:5], '0x'+str[5:7]) | |
if __name__ == "__main__": | |
starttime=time.time() | |
bulb = tikteck.tikteck(*bulb_config) | |
bulb.connect() | |
while True: # Update bulb's color every 3 seconds. | |
updateColor(bulb) | |
time.sleep(3.0 - ((time.time() - starttime) % 3.0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment