#!/usr/bin/env python # Import the library functions we need import time import urllib import wiringpi2 as wiringpi wiringpi.wiringPiSetup() # Setup software PWMs on the GPIO pins PIN_RED = 0 PIN_GREEN = 2 PIN_BLUE = 3 LED_MAX = 100 wiringpi.softPwmCreate(PIN_RED, 0, LED_MAX) wiringpi.softPwmCreate(PIN_GREEN, 0, LED_MAX) wiringpi.softPwmCreate(PIN_BLUE, 0, LED_MAX) wiringpi.softPwmWrite(PIN_RED, 0) wiringpi.softPwmWrite(PIN_GREEN, 0) wiringpi.softPwmWrite(PIN_BLUE, 0) # A function to set the LedBorg colours def SetLedBorg(red, green, blue): wiringpi.softPwmWrite(PIN_RED, int(red * LED_MAX)) wiringpi.softPwmWrite(PIN_GREEN, int(green * LED_MAX)) wiringpi.softPwmWrite(PIN_BLUE, int(blue * LED_MAX)) # A function to turn the LedBorg off def LedBorgOff(): SetLedBorg(0, 0, 0) # Setup parameters cheerlightsUrl = 'http://api.thingspeak.com/channels/1417/field/1/last.txt' # Name Red Green Blue colourMap = {'red': (1.0, 0.0, 0.0), 'green': (0.0, 0.5, 0.0), 'blue': (0.0, 0.0, 1.0), 'cyan': (0.0, 1.0, 1.0), 'white': (1.0, 1.0, 1.0), 'warmwhite': (1.0, 1.0, 0.9), 'purple': (0.5, 0.0, 0.5), 'magenta': (1.0, 0.0, 1.0), 'yellow': (1.0, 1.0, 0.0), 'orange': (1.0, 0.65, 0.0), 'pink': (1.0, 0.75, 0.8), 'oldlace': (1.0, 1.0, 0.9)} # Loop indefinitely while True: try: # Attempt the following: cheerlights = urllib.urlopen(cheerlightsUrl) # Open cheerlights file via URL colourName = cheerlights.read() # Read the last cheerlights colour cheerlights.close() # Close cheerlights file if colourMap.has_key(colourName): # If we recognise this colour name then ... red, green, blue = colourMap[colourName] # Get the LedBorg colour to use from the name else: # Otherwise ... print 'Unexpected colour "' + colourName + '"' # Display the name we did not recognise red, green, blue = (0.0, 0.0, 0.0) # Use the colour of black / off SetLedBorg(red, green, blue) # Set LedBorg to the new colour except: # If we have an error pass # Ignore it (do nothing) finally: # Regardless of errors: time.sleep(1) # Wait for 1 second