SBrick Control Lego Power Functions

Samples & Hints for the ABMaterial framework, which combines a tuned Materialize CSS with the free programming tool B4J.
Site Admin
Posts: 224
Joined: 07 Oct 2017, 12:16

SBrick Control Lego Power Functions

Postby rwblinn » 12 Dec 2017, 15:22

Raspberry Pi control Lego Power Functions via SBrick Bluetooth
v1.0.0 (Build (20171211)

To remote control, via bluetooth with a web browser user interface, a Lego® Power Functions motor & lights using an SBrick.


Solution Overview
The solution runs on a Raspberry Pi with Bluetooth enabled. Bluetooth is used to communicate with the SBrick.. The SBrick has a Lego® Power Functions motor and lights connected to its ports A & C. A Lego® Power Functions Battery Box supplies power to the SBrick.
The server is developed with B4J and the ABMaterial framework enabling control & web browser access. The motor & lights are controlled via a slider to set the speed & intensity (between 0 & 100%).
Configuration parameter (communication, port descriptions) are defined on a settings page

Development Tools
  • SBrick remote control solution for Lego®.
  • B4J v6.00.
  • ABMaterial v4.03.
  • ABMaterial custom component noUiSlider plugin (v10.0.0) [The noUiSlider has been enhanced].
  • Raspberry Pi - hcitool & gatttool for Bluetooth Low Energy devices management & communication.
  • PuTTy and WinSCP to remote invoke commands & manage files.

B4J Source Code.
The archive does not contain the full ABMaterialfolder structure, but only what is required for this solution (in the www custom folders).
To test the application, copy in addition, the www folder (and only that folder) from the ABMaterial Template folder.
If interested in details, read the documented source (sbrickcontrol.b4j, sbrick.bas, sbrickcontrolpage.bas, settingspage.bas).

Parts Used
  • 1x Raspberry Pi 3 Model B v1.2 (with NOOBS 2.4.5 Release date: 2017-11-29).
  • 1x Lego® Power Functions pack 8293.
  • 1x SBrick Plus (Hardware v11, Firmware v11.20).

The Lego® Power Functions battery box is connected to the SBrick Plus. The SBrick port A is connected to the Lego® Power Functions motor, port C connected to the Lego® Power Functions lights.

Application Concept
On the Raspberry Pi Bluetooth is enabled. The Bluetooth communication between the Raspberry Pi 3 onboard Bluetooth adapter and the SBrick is handled using the gatttool.
The gatttool is running from the shell, with parameter and triggered by the server application. To communicate, the SBrick device address (device id) and adapter (hci) information are required. These are obtained on the Raspberry Pi using the hcitool.
Furthermore SBrick parameter are required, like command and value, as described in the SBrick protocol (must read document).

The SBrick has a watchdog which stops a motor in case of failure. If a motor starts, it will run for about 2.5 seconds and to then stop.
This means to keep a motor running, the motor needs a regular connect. This is handled by the server and set via the connect interval (ms). It might require some tweaking to find best value (default set to 50ms).

For this solution, the Raspberry Pi 3 onboard Bluetooth adapter is working fine. No additional BLED112 Bluetooth Smart Dongle required.
The section Supplemental Information provides details on using the hcitool, gatttool and SBrick protocol.

The settings are stored in map format in the text file sbrickcontrol.set, located in the application folder.
The settings can be changed using a text editor or via the settings page of the web browser user interface.
The keys must be in lowercase. Do not remove the first line.

Description Key=Value:
Bluetooth ID of the SBrick (like '00:07:80:2E:2F:19'). Use 'hcitool -i hci0 lescan' to scan Blutooth Low Energy devices and get the IDs.
hci adapter to be used (like 'hci0' or 'hci1'). Use 'hciconfig -a' to find the adapter.
Set the connect interval (ms) between the server application and the SBrick.
Set the port description for the ports A-D. The desctiption is shown above the slider on the SBrick Control page.
Example (using nc=Not Connected for ports b and c):

Run the application, access via web browser url http://ip-address:51050/SBrickControl.

  • In case more then one SBrick, enable SBrick selection.
  • Regular update SBrick voltage & temperature.
  • Consider to disable the SBrick watchdog (Sending a "DRIVE 0" to all channels disables the watchdog).
  • Enhancing this solution planned as SBrick are working on an adapter to connect to the Lego NXT/EV3 sensors & motor family.

Installation Raspberry Pi
See Raspberry Pi Service Setup.

Supplemental Information
More in depth information on the tools used and the development.

SBrick Device Address
On the Raspberry Pi login as user "pi".
Bluetooth is enabled on the Raspberry Pi and the SBrick is turned on (via the Battery Box).
The hcitool (Host Controller Interface) is a CLI (Command Line Interface) tool capable of detecting and connecting to peripheral devices.
Run hcitool --help for more information.

To check the version of the Bluetooth adapter, run

Code: Select all

hciconfig -a
hci0:   Type: Primary  Bus: UART
        UP RUNNING
        HCI Version: 4.1 (0x7)  Revision: 0x145
        LMP Version: 4.1 (0x7)  Subversion: 0x2209
        Manufacturer: Broadcom Corporation (15)

The result shows adapter hci0, which understands Bluetooth version 4.1. Required is min version 4.0.

To get the list of adapters, run the hcitool with lescan parameter.

Code: Select all

sudo hcitool lescan
LE Scan ...
88:6B:0F:23:7A:FE SBrick

The SBrick device MAC address is 88:6B:0F:23:7A:FE, which is required for the gatttool option -b (i.e. gatttool -b 88:6B:0F:23:7A:FE ...) or --device=.
Each SBrick has it's own and unique ID. The option -b means broadcasts a message to the SBrick.

HCI Adapter
To get the hci adapter, run the hcitool with dev parameter.
... but previous hciconfig -a gives more in depth information esp if capable talking to Bluetooth 4.0.

Code: Select all

hcitool dev
hci0    B8:27:EB:05:BB:03

The adapter hci0 is required for the gatttool option -i.

gatttool (Generic Attribute Profile) is used to discover, read, and write characteristics with a Bluetooth Low Energy (BLE) device.
The gatttool is part of the Raspberry Pi NOOBS used.
For this solution, the gatttool reads or writes SBrick characteristics, as defined by the SBrick Protocol, via Bluetooth.
Run gatttool --help for more information.

The syntax used for running the gatttool are
gatttool -b 88:6B:0F:23:7A:FE -i hci0 --char-write-req --handle=0xNNNN --value=CCPPDDSS
gatttool -b 88:6B:0F:23:7A:FE -i hci0 --char-read --handle=0xNNNN

Example running a motor connected to port A at 100% speed clockwise direction, for about 2.5 seconds.

Code: Select all

gatttool -b 88:6B:0F:23:7A:FE -i hci0 --char-write-req --handle=0x001A --value=010000FF

  • -b 88:6B:0F:23:7A:FE = broadcasts a message to the SBrick with Bluetooth ID 88:6B:0F:23:7A:FE
  • -i hci0 = Bluetooth device hci0
  • --char-write-req = the Bluetooth command, idc a write request with response
  • --handle=0x001A = where to write the command to the SBrick. The SBrick has several handlers, 0x001A is the motor controller.
  • --value=010000FF = the content of the command send to the SBrick motor controller = 4 bytes: 01 (Drive) + 00 (PortA) + 00 (Clockwise) + FF (Speed 100%)
    • Ports: A = 00, B = 01, C = 02, D = 03
    • Directions: Clockwise = 00, CounterClockwise = 01
    • Speed Levels: between 00 - FF (HEX), means 256 levels from 0 to 100%, i.e. FF = 255/255 = 100%, 80 = 128/255 = 50%.

Example break a motor connected to port A

Code: Select all

gatttool -b 88:6B:0F:23:7A:FE -i hci0 --char-write-req --handle=0x001A --value=0000

  • --value=0000 = Break command is 00, 00 = port A

The gatttool command does not hold the Bluetooth session, which means the motor stops when the Bluetooth session expires.
To keep the motor running, the gatttool needs to be repeated. In the application solution, this is handled by a connect timer with short connect interval.

The gatttool requires the device handle. To obtain the device handle, the UUID is required. The SBrick protocol states UUID 02b8cbcc-0e25-4bda-8790-a15f53e6010f.
Run the gatttool interactive to get the handle:

Code: Select all

sudo gatttool -b 88:6b:0f:23:7a:fe -I
[88:6b:0f:23:7a:fe][LE]> connect
Attempting to connect to 88:6b:0f:23:7a:fe
Connection successful
[88:6b:0f:23:7a:fe][LE]> char-desc
handle: 0x001a, uuid: 02b8cbcc-0e25-4bda-8790-a15f53e6010f

The char-desc lists all the handles with UUID. As seen, the handle is 0x001a for uuid: 02b8cbcc-0e25-4bda-8790-a15f53e6010f.

Driving a Motor
Examples driving a motor which is connected to port A (channel A). The gatttool writes character commands to the SBRick.
The parameter to send are, beside the device address and the adapter, the handle and value.
The handle for the SBrick Plus is 0x001A.
The value is composed out of command (01),port (00 for channel A), direction (00 for clockwise), speed (FF=full speed 255).
The handle and the value are described in the SBrick protocol.

Drive Motor A interactive
Test to drive the motor interactive using the gatttool.

Code: Select all

gatttool -b 88:6B:0F:23:7A:FE -I -i hci0
[88:6B:0F:23:7A:FE][LE]> connect
Attempting to connect to 88:6B:0F:23:7A:FE
Connection successful
[88:6B:0F:23:7A:FE][LE]> char-write-cmd 0x001A 010000FF

Drive Motor A from the command line
Motor A forwards speed FE. The motor will turn 3 times.

Code: Select all

gatttool --device=88:6B:0F:23:7A:FE --adapter=hci0 --char-write-req --handle=0x001A --value=010000FE
Characteristic value was written successfully

The motor will run for 2.5 seconds.

Server running gatttool Commands
The server application runs the gatttool commands, verifies the result and takes actions based on the command.
The actions have been tested first from a terminal invoking gatttool commands.
The SBrick protocol defines the commands and values. Some Examples:
Drive Motor - value: 01(command drive), 00 (portA), 00 (direction colockwise), FE (speed 254)

Code: Select all

gatttool --device=88:6B:0F:23:7A:FE --adapter=hci0 --char-write-req --handle=0x001A --value=010000FE

Break Motor - value: 00 (command break), 00 (portA)

Code: Select all

gatttool --device=88:6B:0F:23:7A:FE --adapter=hci0 --char-write-req --handle=0x001A --value=0000


Code: Select all

gatttool --device=88:6B:0F:23:7A:FE --adapter=hci0 --char-read --handle=0x000A


Code: Select all

gatttool --device=88:6B:0F:23:7A:FE --adapter=hci0 --char-read --handle=0x000C

Voltage - value 0F (ADC request), 08 (Voltage channel)

Code: Select all

gatttool --device=88:6B:0F:23:7A:FE --adapter=hci0 --char-write-req --handle=0x000A --value=0F08

Temperature - value 0F (ADC request), 09 (Temperature channel)

Code: Select all

gatttool --device=88:6B:0F:23:7A:FE --adapter=hci0 --char-write-req --handle=0x000A --value=0F09

Testing the Application with the B4J Bridge
The B4J Bridge is used to test the application. Steps taken in brief:
  • Create folder /home/pi/b4j
  • Copy b4j-bridge.jar to /home/pi/b4j.
  • Start the b4j-bridge: sudo java -jar b4j-bridge.jar.
  • Start the B4J IDE, load the application and connect to the B4J Bridge (requires the Raspberry Pi IP address)
  • Copy the application objects/www folder to /home/pi/b4j/tempjars.
  • Ensure ownership is user Pi and not root for all www folders&files.
  • Run the application (starts the server) and open the webbrowser with ipaddress:51050/SBrickControl
Recommend to clear the web browser cache after any application update. If a web page is not displayed correct, then copy the www folder from the development device to the Raspberry Pi.

References ... lug/sbrick

Return to “ABMaterial”

Who is online