butchergg
Posts: 5
Joined: Sun Oct 28, 2012 6:14 pm

btbutton - C++ program to interface with cheap Bluetooth buttons

Sun Dec 02, 2018 2:37 am

I needed to interface a Bluetooth button to my Raspberry-Pi/X10 controlled Christmas lights. Seemed simple...

After messing with all sort of udev, dbus, /sysfs, select, poll, libevent, and the like, I came up with a libudev-poll solution as the easiest-to-understand, cleanest implementation, and most reliable operation. It's in a GitHub repository at:

https://github.com/butcherg/btbutton

The instructions will get you a command line program that prints the codes sent for the button events. Interfacing to a particular peripheral, e.g. GPIO, I2C, etc, is left as an exercise for the student... :D

PhatFil
Posts: 1181
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: btbutton - C++ program to interface with cheap Bluetooth buttons

Sun Dec 02, 2018 6:36 am

May i suggest you consider embellishing your work with the capacity to publish its data as a mqtt payload with a configurable topic, And provide capacity for a number of buttons to be used, 24x perhaps?

butchergg
Posts: 5
Joined: Sun Oct 28, 2012 6:14 pm

Re: btbutton - C++ program to interface with cheap Bluetooth buttons

Sun Dec 02, 2018 2:49 pm

MQTT interface would fall into the "left to the student" category... :D

What I wanted to do was capture the udev-poll integration, as that doesn't seem to be out there for others to consider. In my trials, select() proved problematic because the hidraw0 file descriptor is created and destroyed at the button connect/disconnect events and it took doing stuff in the application to handle it. Out on the internet, there's a bit of gravity toward using one of the event libraries (libevent, libev, libuv...), but the primary problem addressed with these seems to have been socket servicing for large-scale multi-threaded web applications. In the parlance of the three little bears, poll() was Just Right, not much more than select(), particularly with ways to consider the on-and-off-again presence of the /dev/hidraw* file descriptor.

I've been using it with my X10 code added for the past day, and I'm finding there's some 'blockage' somewhere, key codes buffer up and don't show for some number of seconds, all at once. It seems to be some ordering of things at startup, as once the program gets into a connect/disconnect pattern things work okay, and some startup sequences seem to set things up okay. I'd appreciate the feedback from someone with more poll() experience to see if I'm setting things up correctly.

Return to “Interfacing (DSI, CSI, I2C, etc.)”