nebucaz / kracerble

Cycling Power BLE on Raspberry Pi using Swift
4 stars 0 forks source link

Publish Kettler Fitness Data over BLE

Suopports Kettler Racer 9 and Kettler Treadmill "Track S5"

The Kettler Racer 9 is an ergometer with USB and Bluetooth interface. Similarly, Kettler Treadmill "Track S5" also features a USB Port. Unfortunately both lack Bluetooth Smart support and therefore can not be integrated/used together with the Zwift App.

In order to be able to use mentioned fitness machnies as a source for Zwift, the project connects to the fitness machines over USB. The training data is polled and published over a BLE server. Everithing runs on Raspberry PI (tested with models 2, 3 and 4) and probably on other linux based systems:

Swift Linux on Raspberri Pi

Inspired by the great work of 360manu and his great work published in kettlerUSB2BLE. However, I wanted to rewrite the functionality using Swift language on Linux. Again, there has been great work already done by the community, namely PureSwift

Polling and Publishing

The Program tries to connect to the machines (Kettler Racer 9, Kettler Treack S5) over USB. After successfully establishing communication, the training status of the device is requestet every second and broadcasted over Bluetooth Smart Service "Cycling Power Service" or "Running Speed and Cadence" respectively.

Installation

Swift on Linux

To compile swift source code on linux, install swift on linux.

Repo: curl -s https://packagecloud.io/install/repositories/swift-arm/release/script.deb.sh | sudo bash
sudo apt-get install swift5

Using XCode

You can use Xcode to develop and compile the software on a Mac. To get a executable to be run on raspberry, the code must be compiled on the target-architecture (aarch64). Currently, the type of fitness machine type has to be choosen at compile time. Assign .racer9 or .track5 to machineType in main.swift (I'll add command line options in a future version).

let machineType : KettlerType = .track5 // .racer9

Fortunately, thomaspaulman created a handy script to rsync the code to the raspberry and run the compilation on the remote machine: Swish. Compile the source with the following command

$ swish <user> <host>

Running & Testing

Running

The compiled source can be found in the .build directory. To start for Kettler bike use

$ sudo kracerble/.build/debug/kracerble 2>&1

If you want to connect to Kettler Track S5 treadmill, you can pass the machine type parameter after specifying the serial port (better parameter handling coming in one of the next releases)

$ sudo kracerble/.build/debug/kracerble /dev/ttyUSB0 track5 2>&1

Connecting the Zwift App

Open the Zwift app and choose the KRacer9 Service after selecting the "Power" - button in the pairing dialogue

Testing Blutooth Smart Cycling Power Service with fake data

You can broadcast random test values by uncommenting/commenting the following section in main.swift

//kettler?.provideFakeData()
kettler?.startPolling(portName)