BlinkStick and Raspberry Pi
BlinkStick Examples
The great thing about BlinkStick is that it supports a wide range of programming languages, including Python. Controlling BlinkStick boards via Python is not very complicated. The simple script in Listing 1 can give you a general idea of how this is done. This script is written for BlinkStick Strip, but you can easily adapt it for other BlinkStick boards.
Listing 1
Random Pulse Script
01 #!/usr/bin/env python 02 import random 03 from blinkstick import blinkstick 04 bstick = blinkstick.find_first() 05 06 while True: 07 LED=random.randrange(0, 7) 08 R=random.randrange(0, 255) 09 G=random.randrange(0, 255) 10 B=random.randrange(0, 255) 11 bstick.pulse(red=R, green=G, blue=B, index=LED, repeats=1)
Even if you are new to Python, you shouldn't have any problem understanding what the script does: It makes LEDs pulse with random colors in random order. To do this, the script first imports two libraries: random
and blinkstick
. It then uses the blinkstick.find_first()
function to detect the first connected BlinkStick board. The random.randrange
function is used to generate random red, green, and blue values as well as generate an LED index number. Finally, the bstick.pulse
function is used to pulse the specified LED with the color consisting of the randomly generated RGB values. This block is wrapped into an infinite loop that runs until the script is interrupted. You can use this simple example as a starting point for your own scripts, and the Python API documentation [2] provides further information on programming BlinkStick via Python.
Watching randomly pulsating LEDs on the BlinkStick boards can be mesmerizing but hardly practical. The next example does something more practical. Riding a bicycle in strong wind is rarely a pleasure (unless, of course, you have a tail wind). So, before you venture out for a bike ride, you might want to check the wind speed; a simple Bash shell script can help you with that (Listing 2).
Listing 2
Check Wind Speed Bash Script
01 #!/bin/bash 02 city="Berlin" 03 country="DE" 04 appid="Your OpenWeatherMap API Key" 05 interval="600" 06 i="0" 07 led_number="8" 08 while true 09 do 10 windspeed=$(curl "http://api.openweathermap.org/data/2.5/ weather?q=$city,$country&appid=$appid" | jq '.wind.speed') 11 windspeed_int=${windspeed%.*} 12 if [ $windspeed_int -le 5 ]; then 13 while [ $i -lt $windspeed_int ] 14 do 15 blinkstick green --index $i --brightness 15 16 i=$[$i+1] 17 done 18 elif [ $windspeed_int -gt 5 ] && [ $windspeed_int -le 7 ] ; then 19 while [ $i -lt $windspeed_int ] 20 do 21 blinkstick yellow --index $i --brightness 15 22 i=$[$i+1] 23 done 24 else 25 while [ $i -lt $led_number ] 26 do 27 blinkstick red --index $i --brightness 15 28 i=$[$i+1] 29 done 30 fi 31 sleep $interval 32 i="0" 33 while [ $i -lt $led_number ] 34 do 35 blinkstick off --index $i 36 i=$[$i+1] 37 done 38 i="0" 39 done
The script fetches and parses the weather data from the OpenWeatherMap service to obtain the current wind speed in meters per second (mps). It then uses the if
… elif
… else
conditional statement to represent the wind speed on the BlinkStick Strip board. This condition is based on the following logic: (1) if the wind speed is less than or equal to 5mps (~16 ft/sec), the LEDs are set to green and the number of LEDs indicates the speed; (2) when the speed is greater than 5mps but less than or equal to 7mps, the LEDs are set to yellow, with the number of LEDs indicating the speed; (3) if the wind speed is greater than 7mps, all LEDs are set to red.
For this script to work you need to obtain a unique API key by signing up for the OpenWeatherMap service [3]. Then, replace the placeholder value of the appid
variable in the script with the actual API key. To fetch and parse the weather data, the script relies on the curl
and jq
tools, so you must install them on your system.
The script performs several simple tasks. It first fetches weather data in the JSON format and pipes the output to the jq
tool, which extracts the wind speed value. This float value is then converted to an integer and used as a part of the conditional statement. To turn on the appropriate number of LEDs, the script uses the while
… do
loop with a simple counter. A similar loop at the end of the script is used to reset all LEDs by turning them off. By default, the script fetches and parses weather data every 10 minutes, but you can change that by specifying the value of the interval
variable in milliseconds.
Final Word
Although they're slightly more expensive than a handful of regular LEDs, BlinkStick boards have a lot going for them. They feature multiple bright RGB LEDs that can be controlled individually, and you have several BlinkStick models from which to choose. They feature a simple API and support many popular programming languages; moreover, both BlinkStick hardware and software are open source. The commands and scripts described in this article give you an idea of BlinkStick's capabilities, and you'll find more examples of how to use BlinkStick in the project's GitHub wiki [4].
Infos
- BlinkStick: http://www.blinkstick.com
- BlinkStick Python API documentation: http://arvydas.github.io/blinkstick-python
- OpenWeatherMap: http://openweathermap.org
- BlinkStick code examples: http://github.com/arvydas/blinkstick-python/wiki
« Previous 1 2 Next »
Buy this article as PDF
Pages: 4
(incl. VAT)