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)

You can use the gatttool command from the BlueZ 5 linux Bluetooth stack to talk with SBrick.

Recent Ubuntu and other Debian-based distros already have Bluez 5. 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 need a system with Bluetooth 4.0. You can get BT4.0 USB dongles for less than $20, linux support most of them. You know it works if you type

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

So your blutooth dongle is 'hci1'.

You can 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 ID is '00:07:80:2E:2A:5D'

You can now send a command to your SBrick. Plug a motor in port A and write this command:

 

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

 

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:

00 = coast

FF = 100%

(there's a bug with production firmware, FF doesn't work, until you upgrade your SBrick firmware use 00 to FE).

 

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

BREAK + Port

So

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

stops motor at Port A

 

Now gatttool doesn't hold the Bluetooth session. So you tell your motor to run, 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 demo.sh with this:

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

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.

« View Page History