Over the past few days I’ve worked on connecting Gauges to my computer over Bluetooth. I have the physical gauges attached to an Arduino and a breadboard, and I am using a Sparkfun BlueSmirf Silver to receive streaming data from my laptop.
I wrote a Python script using the PyBlueZ stack to connect to the Bluesmirf. I had tried using PySerial, a great package, to write directly to the symbolic /dev/rfcomm0 representing serial bluetooth devices in Linux, but the connection had problems. The problems I thought I could avoid by switching to PyBlueZ popped up again, and I am still hunting for a solution.
When I start the Python script, which I call “gauges-daemon” since eventually it will run as a Linux daemon, or background task, the connection opens fine and the gauges respond. However, after a certain period of activity the script suddenly encounters BluetoothError #11, “Resource Temporaily Unavailable.” To counter this error I thought I could have the script re-open the connection, but then I get error #16, “Device or Resource Busy”.
I did some permuting and found that I can start the script, wait for it to fail, and then re-start the BlueSmirf. This allows the script to reconnect. The evidence seems to suggest that the problem is with the BlueSmirf, though I have pored over the datasheet (PDF) and found no mention about necessary steps to keep connections alive.
EDIT: The problem was a buffer overflow on the PC. I was sending Bluetooth messages to the Gauges, which would respond with status text. Because the Python script never read the port for incoming messages, these incoming packets collected in the Bluetooth serial buffer and everything halted until the buffer was cleared.
I fixed the Python script to represent the necessary changes: