Sunday, March 13, 2022

@Cheerlights Control of BLINKT via Raspberry PI Zero


On Twitter we kept seeing messages sent to @cheerlights follow by a color.  Turns out tweeting a color to @cheerlights turns a whole bunch of LEDs around the world a different color.  And now, thanks to this project, the LEDs in my garage.  [UPDATE: After The RocketMan took over Twitter the API no longer functions.  To learn more about how to control any cheerlights project, including this one, go to]


The concept seemed interesting and we had a Raspberry PI Zero and the Pimoroni BLINKT LED module laying around already.  Time to put them to use for this project.  Honestly, the @cheerlights API is so easy and well documented we won't cover that here.   Same is true with the Pimoroni BLINKT so we will just go straight to the Raspberry PI python source code.


The Python scripts "listens" for if a color change tweet has been sent to @cheerlights.   If it "hears" one the far left LED is set to that color.  All other LED colors are shifted right to show the history of the last eight colors.  


Here's the python source code for those that want to join in on the @cheerlights fun.

#!/usr/bin/env python

# Cheerlights with Pimoroni BlinkT module and RasPI Zero
# A tweet to @Cheerlights will change the LEFT most color of the BlinkT (LED#7)
# Color history maintained by shifting old colors to the RIGHT (towards LED#0)

# Valid color tweets to @Cheerlights are:
# Project details at:
# WhiskeyTangoHotel.Com
# MARCH 2022

import time  # needed for delays
import sys   # for requests

    import requests  #  needed to poll @Cheerlights
except ImportError:
    exit("Install needed to run. Use the command: sudo pip install requests")

from blinkt import set_clear_on_exit, set_pixel, show, set_brightness,
clear #

LED_delay = 1
brightness = 0.3 # 0.05 is lowest useable dim.  1.0 is full bright
(the BLINKT is *really* bright if you want!)

#Set up a matrix for r, g, b values (m stands for matrix)
rm = [0,1,2,3,4,5,6,7,8]
gm = [0,1,2,3,4,5,6,7,8]
bm = [0,1,2,3,4,5,6,7,8]

# set_pixel(pixel_no, red, green, blue, brightness)

print "Testing LEDs..."
print "---------------------------------------------"
for i in range(3):
        for j in range (0,8):
                set_pixel(j, 30, 0, 0, brightness)
        print "RED"
        for j in range (0,8):
                set_pixel(j, 0, 30, 0, brightness)
        print "GREEN"
        for j in range (0,8):
                set_pixel(j, 0, 0, 30, brightness)
        print "BLUE"

        # all LEDs off
        for j in range (0,8):
                set_pixel(j, 0, 0,0)
print "Self test complete..."

for j in range (0,8): # set values that we know are 'wrong' to get us into the test loop
        rm[j] = 73.73
        gm[j] = 73.73
        bm[j] = 73.73
print " "

while True:
                r = requests.get('')
                col = r.json()['field2']
                r, g, b = tuple(ord(c) for c in col[1:].lower().decode('hex'))
                time.sleep(5)  # delay until we look for a new color change

                if (r != rm[7]) or (g != gm[7]) or (b != bm[7]):  # new color selected
                        print "New color placed far LEFT.  Shift old colors RIGHT one place"
                        for j in range (0,7): #shift in the new values
                                rm[j] = rm[j+1]
                                gm[j] = gm[j+1]
                                bm[j] = bm[j+1]                         
                                set_pixel(j, rm[j], gm[j], bm[j], brightness)
                                time.sleep(LED_delay) # show change as a sweep

                        rm[7] = r
                        gm[7] = g
                        bm[7] = b
                        set_pixel(7, rm[7], gm[7], bm[7], brightness)

    # All LED off on control C
    except KeyboardInterrupt:
        print("stopping ...")