Linux client scripts

Linux client sc...
(0 ratings)
Create by Lénárd Pásztor | January 9, 2015 | Last updated by Jorge Pereira February 18, 2015 (view change)

Versions Compared


Jorge Pereira

February 18, 2015


Jorge Pereira

February 18, 2015

« View Page History

Key  =copy,  =delete,  =insert,  =replace

Diff (diff 0.000 sec, rendering: 0.000 sec, diff len: 77 chars)

With linux operating system you can use the gatttool command from the BlueZ 5 Bluetooth stack to talk with SBrick.

Recent Ubuntu and other Debian-based distros already have Bluez 5 (as also many other distributions non-Debian). The next examples run fine with Ubuntu 14.04 and 14.10 and also with ev3dev (Debian for LEGO MIndstorms EV3). It's also possbile to use Raspberry Pi running Raspbian but not (at the moment) out-of-the-box, first you have to upgrade Raspbian to Debian «jessie» version.


You know you have installed a BlueZ version that supports Bluetooth Low Energy protocol if you type


hcitool --help
    lescan    Start LE scan


You also need a system with Bluetooth 4.0 hardware. If your system doesn't have Bluetooth or have an older version (most likely 2.x like LEGO Mindstorms EV3) you can get BT4.0 USB dongles for less than $20, linux kernel has native support for most of them. You know it works if you type

hciconfig -a
hci1:    Type: BR/EDR  Bus: USB
    HCI Version: 4.0 (0x6)  Revision: 0x1000
    LMP Version: 4.0 (0x6)  Subversion: 0x220e
    Manufacturer: Broadcom Corporation (15)

So your bluetooth dongle is 'hci1' and it understands  version 4.0

You can now find your Sbrick(s) with the following command (you might need to run it with sudo)

hcitool -i hci1 lescan

LE Scan ...
00:07:80:2E:2A:5D (unknown)
00:07:80:2E:2A:5D SBrick

So your SBrick Bluetooth ID is '00:07:80:2E:2A:5D'. It's like a fingerprint - each SBrick has it's own and unique ID.

You can now send a command to your SBrick. Plug a motor in port A and write this command if you have an SBrick running initial release firmware (4.0):

gatttool -b 00:07:80:2E:2A:5D -i hci1 --char-write --handle=0x0025 --value=01000080

 (if you have upgraded the firmware of your SBrick, some handles changed so use 0x001A instead of 0x0025)

This will make your motor spin at 50% speed for about 2,5 seconds.


Now the full explanation for the gatttool command:

'-b 00:07:80:2E:2A:5D'  broadcasts a message to the SBrick with Bluetooth ID '00:07:80:2E:2A:5D'

'-i hci' uses our Bluetooth device 'hci1'

'--char-write' specifies the type of Bluetooth command - write with response

'--handle=0x0025' specifies where to write (SBrick has several "slots" or handlers, 0x0025 is the motor controller)

'--value=01000080' is in fact the command we're sending to SBrick motor controller. In this example it has 4 bytes: 01 + 00 + 00 + 80

The meaning is: RUN (01) + Port A (00) + Clockwise (00) + 50% (80)

You can use other Ports:

A = 00

B = 01

C = 02

D = 03

You can use two directions:

Clockwise = 00

Counter-clockwise = 01

You can use 256 speed levels, in hexadecimal. Each steps equals 1/255 of full speed (100%) so:

00 = coast

01 = 1/256 = 0.4%

02 = 2/256 = 0.8%


FE = 254/255 = 99.6%

FF = 255/255 = 100%

(there's a bug with initial production firmware so FF doesn't work; until you upgrade your SBrick firmware just use the range 00...FE).


There are / there will be other commands other than RUN. At this moment there is also BREAK. It's just 2 bytes:

BREAK + Port

The hexadecimal code for BREAK is 00 and for each Port is the same as above. So

gatttool -b 00:07:80:2E:2A:5D -i hci1 --char-write --handle=0x0025 --value=0000

stops motor at Port A


The gatttool command doesn't hold the Bluetooth session. So you tell your motor to run but after about 2.5 seconds it will stop because Bluetooth session expires.You need to use some sort of code to maintain the motor running.

I'll show better ways but for now this is enough - create a file named with this:

while true
gatttool -b 00:07:80:2E:2A:5D -i hci1 --char-write --handle=0x0025 --value=010000FE
sleep 2.5

give it execution permissions and run it. It will keep you motor spinning until you press Control+C.

You may need to adjust the sleep value for lower values.


If you want to see some more complex code please take a look at my SBrick Stress Test Tool for Linux at GitHub. It's a GUI tool written in Python that allows us to controll each of the 4 ports with sliding bars:

You can see a video demo:.