Cheerlights with Blinkt!
Our makerspace makes heavy use of the Blinkt! device from Pimoroni. One of the great applications on top is Cheerlights.
CheerLights is an Internet of Things project created by Hans Scharler that allows people's lights all across the world to synchronize to one color set by Twitter. This is a way to connect physical things with social networking experiences and spread cheer at the same time. When one light turns red, they all turn red. [ThingSpeak Community]
See it in action on our makerspace
Why did we do this? Because we can :) However, there is a learning experience here: connecting the world to a some hardware, with Twitter in between. Can go both ways. We have learned how to mash-up solutions to achieve something new. After 1 hour of setting up and 1 hour of figuring out glitches - awesome!
How to use
- Fire up Twitter
- Send a tweet to @cheerlights or include “cheerlights” somewhere in your message with the name of a color
- Done!
Example Tweet
We joined Cheerlights: #cheerlights green for our makerspace!
Some color inspiration comes from the homepage and is listed here:
- red (#FF0000)
- green (#008000)
- blue (#0000FF)
- cyan (#00FFFF)
- white (#FFFFFF)
- oldlace / warmwhite (#FDF5E6)
- purple (#800080)
- magenta (#FF00FF)
- yellow (#FFFF00)
- orange (#FFA500)
- pink (#FFC0CB)
More on the API here
How to setup your own
This guide is rather straightforward. We want to have Cheerlights running on our device, headless. The sample code which comes with the Blinkt! device is great, but can fail for several reasons. Therefore we modified a little to achieve this goal. Building on the great work done by others.
- Assemble the hardware: Take your Pi and attach the Blinkt!
- Setup A Pi with a base system
For this step, use one of our guides to setup a device. You can use our Raspberry Pi Headless Setup guide for instance
- Debug the script. The issue we face with the excellent demo script - cheerlights.py found here is long-time periods of running. Whenever a glitch occurs, the script stops. So we modified just so slightly to run all the time, regardless of any error.
This is rather brute-force, but works:
#!/usr/bin/env python
# Cheerlights example from Pimoroni, modified for Raspberry Valley use. The issue: Autostarting the script on
# a headless Pi caused errors in minutes or hours. We simply wrapped the code in the loop with a 'try except'
# to ignore all errors. The 'KeyboardInterrupt' exception is added to help when debugging (stop the code via
# CTRL-C)
#
# Original Source: https://github.com/pimoroni/blinkt/tree/master/examples
# Blinkt! Getting Started: https://learn.pimoroni.com/tutorial/sandyj/getting-started-with-blinkt
import time
import sys
try:
import requests
except ImportError:
exit("This script requires the requests module\nInstall with: sudo pip install requests")
from blinkt import set_clear_on_exit, set_pixel, show
set_clear_on_exit()
while True:
try:
r = requests.get('http://api.thingspeak.com/channels/1417/field/2/last.json')
col = r.json()['field2']
r, g, b = tuple(ord(c) for c in col[1:].lower().decode('hex'))
for i in range(8):
set_pixel(i, r, g, b)
show()
time.sleep(1)
except KeyboardInterrupt:
print("stopping ...")
sys.exit(0)
except:
time.sleep(1)
- Automate the startup of the script using our guide Autorun Python Script
- Done.