Raspberry Pi/Linux Client?


#21

@samuelclay Can you give me a hand on interpreting the battery status characteristic? At the moment I’m assuming it’s an integer byte (bigendian), representing the battery level out of 255, however it has gone up recently for me, so I’m guessing that’s wrong?


#22

Thanks for putting up that repo @fredley! I just added it to the API page.

The battery level indicator is a rough conversion of voltage, which can change over time and go up as well. It’s a rough indicator of battery life but you should be able to turn it into a percentage and display it to the user.


#23

Also, Home Assistant support would be amazing.


#24

Has anyone noticed the battery status service and characteristic changing from the defaults?

I should be seeing a 180f service with a 2a19 characteristic, but instead I’m seeing the following.(Reading button presses still works.)

[f9:1d:ttmacaddr] Resolved services
[f9:1d:ttmacaddr]  Service [00001530-1212-efde-1523-785feabcd123]
[f9:1d:ttmacaddr]    Characteristic [00001534-1212-efde-1523-785feabcd123]
[f9:1d:ttmacaddr]    Characteristic [00001531-1212-efde-1523-785feabcd123]
[f9:1d:ttmacaddr]    Characteristic [00001532-1212-efde-1523-785feabcd123]
[f9:1d:ttmacaddr]  Service [0000180a-0000-1000-8000-00805f9b34fb]
[f9:1d:ttmacaddr]    Characteristic [00002a29-0000-1000-8000-00805f9b34fb]
[f9:1d:ttmacaddr]  Service [99c31523-dc4f-41b1-bb04-4e4deb81fadd]
[f9:1d:ttmacaddr]    Characteristic [99c31526-dc4f-41b1-bb04-4e4deb81fadd]
[f9:1d:ttmacaddr]    Characteristic [99c31525-dc4f-41b1-bb04-4e4deb81fadd]
[f9:1d:ttmacaddr]  Service [00001801-0000-1000-8000-00805f9b34fb]
[f9:1d:ttmacaddr]    Characteristic [00002a05-0000-1000-8000-00805f9b34fb]

#25

That’s the DFU (device firmware update) service, used for over-the-air firmware upgrades.

#define BLE_DFU_SERVICE_UUID                 0x1530                       /**< The UUID of the DFU Service. */

This looks like the device information service, which is a default Bluetooth service.

#define BLE_UUID_DEVICE_INFORMATION_SERVICE                      0x180A     /**< Device Information service UUID. */

That’s the button status service, with it’s two characteristics: buttons pressed and remote nickname.

#define BUTTONSERVICE_UUID_BASE {0xdd, 0xfa, 0x81, 0xeb, 0x4d, 0x4e, 0x04, 0xbb, 0xb1, 0x41, 0x4f, 0xdc, 0x7a, 0x90, 0xc3, 0x99}
#define BUTTONSERVICE_UUID_SERVICE                  0x1523
#define BUTTONSERVICE_UUID_BUTTONSTATUS_CHAR        0x1525
#define BUTTONSERVICE_UUID_FIRMWARE_NICKNAME_CHAR   0x1526

Another generic GATT service, found in the code here:

#define BLE_UUID_GATT                                 0x1801 /**< Generic Attribute Profile. */

#26

Thanks for that. That was what I gathered from checking the turntouch project out on github. But I’m just not seeing the battery info for some reason.

I’m just using a simple python script to dump the characteristics, and I’m not seeing the battery information come out for some reason. Does anybody want to try out the script in linux and see if they get the battery info showing properly? (Note the mac address of the turntouch which goes in near the end.)

#!/usr/bin/python3
import gatt

manager = gatt.DeviceManager(adapter_name='hci0')

class AnyDevice(gatt.Device):
    def connect_succeeded(self):
        super().connect_succeeded()
        print("[%s] Connected" % (self.mac_address))

    def connect_failed(self, error):
        super().connect_failed(error)
        print("[%s] Connection failed: %s" % (self.mac_address, str(error)))

    def disconnect_succeeded(self):
        super().disconnect_succeeded()
        print("[%s] Disconnected" % (self.mac_address))

    def services_resolved(self):
        super().services_resolved()

        print("[%s] Resolved services" % (self.mac_address))
        for service in self.services:
            print("[%s]  Service [%s]" % (self.mac_address, service.uuid))
            for characteristic in service.characteristics:
                print("[%s]    Characteristic [%s]" % (self.mac_address, characteristic.uuid))


device = AnyDevice(mac_address='f9:1d:******', manager=manager)
device.connect()

manager.run()

#27

As a followup to this thread, I did manage to get the python script running as a linux service @fredley wrote at https://github.com/fredley/raspi-turntouch working, but it turns out that the service kept disconnecting from the TurnTouch.

So I hacked up this second dirty script which I run from cron on my raspberry pi every ten minutes which restarts the turntouch service if it isn’t actually connected to the TurnTouch device. It works so it will do until someone rewrites the primary script to detect when the TurnTouch gets disconnected and reconnect automatically.

#!/bin/sh
# Turntouch Bluetooth Mac address goes in the next line as AA_BB_CC_DD_EE_FF
TTbtMac=AA_BB_CC_DD_EE_FF

until /usr/bin/dbus-send --system --print-reply --type=method_call --dest=org.bluez /org/bluez/hci0/dev_$TTbtMac org.freedesktop.DBus.Properties.Get string:"org.bluez.Device1" string:"Connected" | /bin/grep -q true;
do

echo Turntouch Not Connected >> /var/log/turntouch.log
dbus-send --system --print-reply --type=method_call --dest=org.bluez /org/bluez/hci0/dev_$TTbtMac org.freedesktop.DBus.Properties.Get string:"org.bluez.Device1" string:"Connected"  >> /var/log/turntouch.log

systemctl stop turntouch
sleep 10
systemctl start turntouch
sleep 20
dbus-send --system --print-reply --type=method_call --dest=org.bluez /org/bluez/hci0/dev_$TTbtMac org.freedesktop.DBus.Properties.Get string:"org.bluez.Device1" string:"Connected"  >> /var/log/turntouch.log
done
SERVICE=turntouch
if [ "`systemctl is-active $SERVICE`" != "active" ]
then
        echo "$SERVICE wasnt running so attempting restart" >> /var/log/turntouch.log
        systemctl restart $SERVICE
        systemctl status $SERVICE | echo "$SERVICE was restarted" >> /var/log/turntouch.log
        exit 0
fi
echo Turntouch Connected. >> /var/log/turntouch.log