Keybow RPi drives Shelly plugs / bulbs
Basically this project enables to switch on and off a device (plug or bulb) based on URL call from a mini keybow plugged on a Raspberry Pi!
With the covid, and by force of circumstance, I started to work at home by obligation then by pleasure until I dedicated a room at home with my own setup. With a house on 2 floors, and moments when I am in a meeting, recording videos or simply available, I wanted to manage the availability of access to the room ... in short a kind of "On Air" to indicate my status. Ok, I can hear you from where you are... why didn't I just buy one? Well, it was too simple, and I like to make things!
So, it is quite naturally that I turned to Raspberry with its smaller form factor, Raspberry Pi Zero W on which I could adapt a mini keyboard. To manage the lights, after some chat with friends on Twitter, it appears that Shelly solutions seamed to be the most appropriate for want I wanted to do, switching on and off thanks to HTTP requests
Here are the component I used:
- Raspberry Pi Zero W by Raspberry
- GPIO Hammer Header (Solderless) by Pimoroni
- Keybow Kit (3-key) by Pimoroni
- 16+Gb micro SD Memory card
- Shelly Plug S by Shelly
- Shelly Duo - RGBW by Shelly
For this project, I reused a spare Raspberry Pi Zero W I already had. As I'm not mastering soldering, I used a solderless GPIO header thanks to GPIO Hammer Header (Solderless) to give GPIO connectivity for the keyboard. Honestly if you can buy one natively soldered, I recommend to do so, here is the reference: Raspberry Pi Zero WH.
I used the Keybow Kit (3-key) that brings 3 RGB mechanical keys to trigger actions. Pimoroni team released some interesting material to assemble the keybow mini.
To manage the lights, I bough Shelly Plug S and a Shelly Duo - RGBW by Shelly in order to manage two lamps and a bulb.
So first step is to download the OS image, I used to download Raspberry Pi OS Lite from the official Raspberry web site.
Then you have to flash the OS to the micro SD card. Even if there are several option, I mainly used Etcher created by Balena, that makes things very simple: You just have to select the image, select your MicroSD card, and flash!
Because Etcher unmount the micro SD card after end of flashing process, you have to reinsert it, because there are 2 files to trick! You will observe that all the OS has been stored into the /boot
folder. First, in /boot
, create a file called ssh
without extension to enable SSH on the Raspberry Pi. Because you are moving forward screenless, you have to activate Wifi on the Raspberr Pi. To do so, you have to create on /boot
a configuration file called wpa_supplicant.conf
that will allow you to pre-configure the WiFi credentials. At first boot time, the Raspberry Pi will copy and use this as the default configuration file.
In this file, place the following content and adapt it to your own Wifi configuration:
country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="WIFI_SSID"
scan_ssid=1
psk="WIFI_PASSWORD"
key_mgmt=WPA-PSK
}
Note: More information can be found in the official Raspberry Pi documentation on the section "Configuring Networking" of "Setting up a Headless Raspberry Pi"!
Now, you just have to save the updated content of wpa_supplicant.conf
, then unmount you micro SD card.
First step is done!
Now that's the tada moment, just insert the micro SD Card in the slot, and power up thanks to USB cable! Wait 2 to 5 minutes for the first boot.
Use your prefered IP scanner tool, or just connect to your box router to find the IP address of the Rapsberry Pi, then ssh pi@IP_ADDRESS
, the default password is raspberry
, and you will have the prompt waiting for you!
Of course, I have to recommende to update your device's OS and change the default password.
It's quite simple, you just have to type those two commands at the prompt:
sudo apt-get update && sudo apt-get upgrade -y
and
passwd
To start using the mini keybow, you have to Enable SPI via the following command: sudo raspi-config nonint do_spi 0
Then, all you have to do is to install the library that manage mini keybow hardware: pip3 install keybow
The python code of app.py
is quite simple and is doing the following:
- Initiate a logger to manage maintenance and potential issues properly
- Load config of the actions to trigger on and off for each devices from a json config file
- Call setup function that put all RGB buttons in green
- Manage action to trigger thanks to dedicated handlers, one for each button, swapping from green to red, then to red to green
Once you have cloned the project, just cd keybowRPiDrivesMyLights
, then ./install.sh
that will install the python app as a service that will be launched each time the RaspberryPi will start.
This script shell will copy the keybowRPiDrivesMyLights.service
to /etc/systemd/system
, then enable the service via sudo systemctl enable keybowRPiDrivesMyLights
and start the service via sudo systemctl start keybowRPiDrivesMyLights
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.