Ansible tools for operating and managing fleets of Blinksticks in harmony using the Blinkstick Python library.

Overview

Blinkstick Ansible

deploy workflow

Much of the inspiration is from https://github.com/arvydas/blinkstick-python/wiki where some of the python snippets worked as-is and some did not. Some of the snippets are from Python 2.7 and wouldn't work on Python 3.X. I am using At least Python 3.8 for everything python related.

The reason for creating this repository was for me to have an easy way to operate 4 Blinkstick Nanos that I had bought for my raspberry pi kubernetes cluster. Having multiple nodes with separate Blinksticks, I wanted an abstraction layer on all four nodes that makes managing the configurations less monotonous. I have supplied several roles that can be executed against the blinksticks.

Install

Instructions

Get the following packages and materials on the machine executing Ansible. I am using 4 Blinkstick Nanos for this project, but any blinkstick should work.

Modify inventory/all.yaml with your own IP addresses and blinkstick serial numbers. Ensure that you have passwordless ssh setup to all nodes before proceeding with any Ansible configuration.

Example entry looks like...

node1: # name of the host. Arbitruary, it can be anything.
  ansible_host: 192.168.0.1 # IP address of the host
  serial: BS000001-3.0 # Run ansible-playbook main.yaml -t get-info to get this value for each node.

If you want to find the blinkstick serial numbers after mofifying the IP addresses, run the get-info tag. This saves you from logging into each node to find the serials.

Available Tags

tag description
get-info Collects all blinkstick information across all nodes. This includes serial numbers.
install Install all python packages defined in the python_packages list variable in main.yaml.
cpu-usage Monitors CPU usage using psutil and returns the appropriate color based on the percentage. This script runs indefinitely and checks every second.
internet-status When executed, a python script checks for internet access. If internet is up, color is green, if internet is down, color switches to purple.
aliases Used to create aliases that can be used directly in the command-line to execute the defined roles without knowing the full ansible-playbook command.
rave Executes a sequenece to perform a lightshow on all blinksticks.
off Turn all blinksticks off.
# Deploy everything !!
ansible-playbook main.yaml -t deploy
  # executes the following...
  # -t get-info
  # -t install
  # -t cpu-usage
  # -t internet-status
  # -t aliases

# Retrieve all Blinkstick information and serial number. Ensure the blinkstick is plugged into a USB slot before executing.
ansible-playbook main.yaml -t get-info

# Install all python library dependancies. Add or removes in the the python_packages list in main.yaml and re run this command to make the change on all nodes. 
ansible-playbook main.yaml -t install

# This roles uses the blinkstick python library to substitute the serial number from the ansible inventory and parse it as python. The script is then executed on the remote machines.
ansible-playbook main.yaml -t cpu-usage

# This roles uses the blinkstick python library to substitute the serial number from the ansible inventory and parse it as python. 
# A python script will continuously monitor the CPU percentage of the nodes and update the color accordingly.
ansible-playbook main.yaml -t internet-status

# Throw a party to celebrate getting everything installed.
ansible-playbook main.yaml -t rave

# Set the base color and brightness
ansible-playbook main.yaml -t base-config

# Or... pass the brightness and color inline using extra vars
ansible-playbook main.yaml -t base-config -e color=cyan -e brightness=50

# Turn all the Blinksticks off. (Useful for nighttime when you want to sleep. Use a cron to turn off automatically.)
ansible-playbook main.yaml -t off

# Creates aliases in either ~/.zprofile or ~/.bashrc, dynamically decided by the role.
ansible-playbook main.yaml -t aliases
  # The following aliases are added to either ~/.zprofile or ~/.bashrc....
  # blink-base
  # blink-day
  # blink-night
  # blink-rave
  # blink-off

Cron Scheduler

Add lines to the vars.crons section of cron.yaml to create cron jobs that execute locally on the machine running Ansible.

vars:
  crons:
    day:   { minute: '0', hour: '8',  weekday: '*',   disabled: 'no', tag: 'base-config', brightness: '70', color: 'green' }
    night: { minute: '0', hour: '21', weekday: '*',   disabled: 'no', tag: 'base-config', brightness: '20', color: 'cyan' }
    rave:  { minute: '0', hour: '17', weekday: 'FRI', disabled: 'no', tag: 'rave',        brightness: '80', color: 'cyan' }

To apply cron schedules from the root of the repository...

ansible-playbook cron.yaml

Troubleshooting

Blinkstick is having problems with python 3.9.2. I installed the latest version of Raspian (Debian Bullseye) and it ships with 3.9.2. On the other nodes, I was using <=3.8.2 so installed python 3.8.2 on Debian and got it to work using the steps below.

the issue appears when executing blinkstick using sudo blinkstick. This is essentially a workaround using a workaround because even with Python 3.8 there are problems which are outlined in this issue. Shown below is the output on each version followed by steps to get it working on your system using Python 3.8. I think even using Python 3.7 would just work out of the box without the extra steps...

Python 3.9 Output

sys.exit(main()) File "/usr/local/bin/blinkstick", line 220, in main sticks = blinkstick.find_all() File "/usr/local/lib/python3.9/dist-packages/blinkstick/blinkstick.py", line 1566, in find_all result.extend([BlinkStick(device=d)]) File "/usr/local/lib/python3.9/dist-packages/blinkstick/blinkstick.py", line 217, in __init__ self.bs_serial = self.get_serial() File "/usr/local/lib/python3.9/dist-packages/blinkstick/blinkstick.py", line 283, in get_serial return self._usb_get_string(self.device, 3) File "/usr/local/lib/python3.9/dist-packages/blinkstick/blinkstick.py", line 221, in _usb_get_string return usb.util.get_string(device, index, 1033) File "/usr/local/lib/python3.9/dist-packages/usb/util.py", line 260, in get_string return buf[2:buf[0]].tostring().decode('utf-16-le') IndexError: array index out of range">
[email protected]:~/python38-env $ python3 --version
Python 3.9.2

[email protected]:~/python38-env $ sudo blinkstick --blink green
Traceback (most recent call last):
  File "/usr/local/bin/blinkstick", line 331, in <module>
    sys.exit(main())
  File "/usr/local/bin/blinkstick", line 220, in main
    sticks = blinkstick.find_all()
  File "/usr/local/lib/python3.9/dist-packages/blinkstick/blinkstick.py", line 1566, in find_all
    result.extend([BlinkStick(device=d)])
  File "/usr/local/lib/python3.9/dist-packages/blinkstick/blinkstick.py", line 217, in __init__
    self.bs_serial = self.get_serial()
  File "/usr/local/lib/python3.9/dist-packages/blinkstick/blinkstick.py", line 283, in get_serial
    return self._usb_get_string(self.device, 3)
  File "/usr/local/lib/python3.9/dist-packages/blinkstick/blinkstick.py", line 221, in _usb_get_string
    return usb.util.get_string(device, index, 1033)
  File "/usr/local/lib/python3.9/dist-packages/usb/util.py", line 260, in get_string
    return buf[2:buf[0]].tostring().decode('utf-16-le')
IndexError: array index out of range

Python 3.8 Output

[email protected]:~/python38-env $ source ./bin/activate
(python38-env) [email protected]:~/python38-env $ python --version
Python 3.8.2

Change file /usr/local/bin/blinkstick interpretor from #!/usr/bin/env python to #!/home/pi/python38-env/bin/python3

(python38-env) [email protected]:~/python38-env $ head -5 /usr/local/bin/blinkstick
#!/home/pi/python38-env/bin/python3

from optparse import OptionParser, IndentedHelpFormatter, OptionGroup
from blinkstick import blinkstick

Run sudo blinkstick --blink green

(python38-env) [email protected]:~/python38-env $ sudo blinkstick --blink green
(python38-env) [email protected]:~/python38-env $

Install Blinkstick inside Python 3.8 virtual environment

Create Python 3.8 virtual environment

# Install python 3.8 on debian: https://linuxize.com/post/how-to-install-python-3-8-on-debian-10/

mkdir ~/python38-env && cd ~/python38-env
python3.8 -m venv .
source ./bin/activate

Perform Python 3.8 workaround steps

sudo apt-get install dos2unix
sudo pip install pyusb
sudo pip install blinkstick
sudo chmod +x /usr/local/bin/blinkstick
sudo dos2unix /usr/local/bin/blinkstick

Change python interpretor for blinktick module

Note: In my case I used python38-env as the target folder when creating the virtual env in the early steps.

By changing the interpreter in the module directly will allow Blinkstick to function when the virtual env is deactivated.

#!/usr/bin/env python to #!/home/pi/python38-env/bin/python3

Run sudo blinkstick and you should see the help menu.

Running sudo blinkstick --blink green works on the host.

Reference

Install Python 3.8 on Debian 10: https://linuxize.com/post/how-to-install-python-3-8-on-debian-10/ Install Python 3.8 on Debian 11: https://www.linuxcapable.com/how-to-install-python-3-8-on-debian-11-bullseye/ I used the Debian 10 tutorial for installing on bullseye but then found this version. Looks like it installs 3.8.12 instead of 3.8.2 Python 3.8 workaround reference: https://github.com/arvydas/blinkstick-python/issues/34

Owner
Greg Robinson
Delivery Consultant
Greg Robinson
Universal Xiaomi MIoT integration for Home Assistant

Xiaomi MIoT Raw 简体中文 | English MIoT 协议是小米智能家居从 2018 年起推行的智能设备通信协议规范,此后凡是可接入米家的设备均通过此协议进行通信。此插件按照 MIoT 协议规范与设备通信,实现对设备的状态读取及控制。

1.9k Jan 02, 2023
Segger Embedded Studio project for building & debugging Flipper Zero firmware.

Segger Embedded Studio project for Flipper Zero firmware Установка Добавить данный репозиторий в качестве сабмодуля в корень локальной копии репозитор

25 Dec 28, 2022
Examples to accompany the

Examples to accompany the "Raspberry Pi Pico Python SDK" book published by Raspberry Pi Trading, which forms part of the technical documentation in support of Raspberry Pi Pico and the MicroPython po

Raspberry Pi 589 Jan 08, 2023
A python script for Homeassistant that counts down the days to birthdays, anniversaries etc

Date Countdown A python script for Homeassistant that counts down the days to birthdays, anniversaries etc Important note I no longer use homeassistan

Marc Forth 21 Mar 12, 2022
Minimal and clean dashboard to visualize some stats of Pi-Hole with an E-Ink display attached to your Raspberry Pi

Clean Dashboard for Pi-Hole Minimal and clean dashboard to visualize some stats of Pi-Hole with an E-Ink display attached to your Raspberry Pi.

Alessio Santoru 104 Dec 14, 2022
Hourglass on the pi pico using circuitpython

hourglass-on-pico "Hourglass" on the raspberry pi pico using circuitpython circuitpython version 7.0.0 Components used: Raspberry Pi Pico ADXL345 acce

4 Jul 18, 2022
Testing additional addon devices, and their working scripts

ESP32-addon-devices-potpurri Testing additional addon devices, and their micropython working scripts 📑 List of device addons tested so far Ethernet P

f-caro 0 Nov 26, 2022
An embedded application for toy-car controlling based on Raspberry Pi 3 Model B and AlphaBot2-Pi.

An embedded application for toy-car controlling based on Raspberry Pi 3 Model B and AlphaBot2-Pi. This is the source codes of my programming assignmen

StardustDL 4 Oct 19, 2022
Scapy: the Python-based interactive packet manipulation program & library. Supports Python 2 & Python 3.

Scapy Scapy is a powerful Python-based interactive packet manipulation program and library. It is able to forge or decode packets of a wide number of

SecDev 8.3k Jan 08, 2023
Skykettle ha - Redmond SkyKettle integration for Home Assistant

Redmond SkyKettle integration for Home Assistant This integration allows to cont

Alexey 'Cluster' Avdyukhin 48 Jan 06, 2023
CircuitPython Driver for Adafruit 24LC32 I2C EEPROM Breakout 32Kbit / 4 KB

Introduction CircuitPython driver for Adafruit 24LC32 I2C EEPROM Breakout Dependencies This driver depends on: Adafruit CircuitPython Bus Device Regis

foamyguy 0 Dec 20, 2021
a library for using WS2812b leds (aka neopixels) with Raspberry Pi Pico

pico_ws2812b a library for using WS2812b leds (aka neopixels) with Raspberry Pi Pico You'll first need to save the ws2812b.py file to your device (for

76 Nov 25, 2022
For use with an 8-bit parallel TFT touchscreen using micropython

ILI9341-parallel-TFT-driver-for-micropython For use with an 8-bit parallel TFT touchscreen using micropython. Many thanks to prenticedavid and his MCU

3 Aug 02, 2022
Small Robot, with LIDAR and DepthCamera. Using ROS for Maping and Navigation

🤖 RoboCop 🤖 Small Robot, with LIDAR and DepthCamera. Using ROS for Maping and Navigation Made by Clemente Donoso, 📍 Chile 🇨🇱 RoboCop Lateral Fron

Clemente Donoso Krauss 2 Jan 04, 2022
Used python functional programming to make this Ai assistant

Python-based-AI-Assistant I have used python functional programming to make this Ai assistant. Inspiration of project : we have seen in our daily life

Durgesh Kumar 2 Dec 26, 2021
Python Keylogger for Linux

A keylogger is a program that records your keystrokes, this program saves them in a .txt file on your local computer and, after 30 seconds (or as long as you want), it will close the .txt file and se

Darío Mazzitelli 4 Jul 31, 2021
gdsfactory is an EDA (electronics design automation) tool to Layout Integrated Circuits.

gdsfactory 3.5.5 gdsfactory is an EDA (electronics design automation) tool to Layout Integrated Circuits. It is build on top of phidl gdspy and klayou

147 Jan 04, 2023
An open source two key macro-pad modeled to look like a cartoony melting popsicle

macropopsicle An open source two key macro-pad modeled to look like a cartoony melting popsicle. Build instructions Parts List -1x Top case half (3D p

17 Aug 18, 2022
Component for deep integration LedFx from Home Assistant.

LedFX for Home Assistant Component for deep integration LedFx from Home Assistant. Table of Contents FAQ Install Config Performance FAQ Q. What versio

Dmitry Mamontov 28 Dec 13, 2022
ESP32 recording button presses, and serving webpage that graphs the numbers over time.

ESP32-IoT-button-graph-test ESP32 recording button presses, and serving webpage via webSockets in order to graph the responses. The objective was to t

f-caro 1 Nov 30, 2021