Code and Life

Programming, electronics and other cool tech stuff

Supported by

Supported by Picotech

$8 Bluetooth automation button for Raspberry Pi Zero W

This project was born as a sidetrack of another one (I’m planning on building a $10 DIY Bluetooth page turning pedal for my piano and iPad sheet music app, similar to PageFlip Butterfly). I was looking if AliExpress would have bluetooth pedals, which they don’t — it seems Chinese vendors are REALLY good at copying products but there is little new product innovation combining something as simple as a bluetooth keyboard sending one or two keys with a pedal (two items that they do have)! But while searching, I found this inexpensive gadget (in case the product is removed, you might just search for “bluetooth remote” at AliExpress.com):

So what is it? It’s an $8 disc with multimedia buttons that pairs with your smartphone and you can use it for example in car to control your music. But maybe it would pair with my Raspberry Pi W which has integrated bluetooth as well? Well it costs about nothing to find out!

Fast forward about two weeks and it arrived. I did not try to use it for its intended purpose, but instead went straight to pair it with my Raspberry Pi Zero W. Turns out the pairing process was quite painless, you can follow for example LifeHacker’s tutorial for pairing quite easily. And it goes a little something like this (your MAC address might vary, just look for output after “scan on”):

# bluetoothctl
power on
agent on
scan on
connect FF:FF:00:45:8D:FF
trust FF:FF:00:45:8D:FF

Now for the bummer: After a morning of googling and trying, I was not able to monitor any keypresses from the device. It is not identified in Raspbian as bluetooth keyboard, but a raw HID device, and accessing it through udev seems to be something that people asking for help on forums don’t much do. I actually also tried Adafruit EZKey which does work on Windows and iOS. It seemed to be recognized a bit better, but the Pi wouldn’t receive keypresses from that either.

I decided to call it a day after about 40 different types of attempts, including compiling some C code to access the /dev/input/eventX devices directly, catting and tailing the device files, to no avail. Well, one thing that DOES work perfectly is pairing — the controller sleeps after 30s and when a button is pressed, it wakes up fast, and the Raspberry Pi recognizes it:

$ udevadm monitor --environment
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent

KERNEL\[2065.407855\] add      /devices/platform/soc/20201000.serial/tty/ttyAMA0/hci0/hci0:64 (bluetooth)
ACTION=add
DEVPATH=/devices/platform/soc/20201000.serial/tty/ttyAMA0/hci0/hci0:64
DEVTYPE=link
SEQNUM=1169
SUBSYSTEM=bluetooth

UDEV  \[2066.221885\] add      /devices/platform/soc/20201000.serial/tty/ttyAMA0/hci0/hci0:64 (bluetooth)
ACTION=add
DEVPATH=/devices/platform/soc/20201000.serial/tty/ttyAMA0/hci0/hci0:64
DEVTYPE=link
SEQNUM=1169
SUBSYSTEM=bluetooth
SYSTEMD\_ALIAS=/sys/subsystem/bluetooth/devices/hci0:64
SYSTEMD\_WANTS=bluetooth.target
TAGS=:systemd:
USEC\_INITIALIZED=65414254

Now, with modern kernels and udevd, it’s quite easy to just wait for the first “add” action and run any script on the Pi. So just create a file in /etc/udev/rules.d, for example with sudo vim /etc/udev/rules.d/z21_persistent-local.rules, and add one line:

ACTION=="add", DEVPATH=="/devices/platform/soc/20201000.serial/tty/ttyAMA0/hci0/hci0:64", SUBSYSTEM=="bluetooth", RUN+="/bin/bash /home/pi/SmartRemote.sh"

It is run as root, so be careful what you do. You can use “sudo -u pi” to run as the “pi” user. But I am only running this in local network with fixed script that wakes up my computer, so that was enough for me.

Now I can just press the button, and the Pi Zero W runs the script, which currently turns on and off my main computer, which has its power button in an inconvenient location (Antec P180 case on the floor, with power button behind a front door that clicks magnetically shut, what a design fail :).

Not a bad result for $8 investment: A button which you can press to do anything that the RaspPi can achieve. And now that I noticed there are also $1.50 bluetooth camera remotes, maybe it’s time to order a couple of those…

1 comments

Ronald Derkis:

I am 71 and apparently missing something. I have a remote Bluetooth shutter button. It is recognized and paired on my Raspberry pi 3 b+.
I put the above script in the proper rules.d directory. And created a file called /home/pi/SmartRemote.sh. But SmartRemote.sh never seems to get called. What am I doing wrong?
Also you said somthing about 30 seconds. Does that mean that 10 seconds is as often as you can use/recognize a button press with this method?