This is a python based interface for control SBrick devices. What is the benefit when you use SBrick-Framework:
- Get detailed information: UUID, service, characteristic, adc(voltage, temperature) information...
- Control LEGO power functions concurrently:
- Drive power functions
- Stop power functions
- Support multiple SBrick devices (not test yet)
A thread is created to handle a LEGO power function (a channel), and this means you can control LEGO power functions concurrently. The last drive function will overwrite the execution time of the related power function that is applied previously.
SBrick-Framework is implemented by BlueZ + SBrick protocol + libuv + MQTT protocol + mosquitto broker
, and the code is tested by python 3.4 on Raspberry Pi 3(Raspbian Jessie).
SbrickAPI is based on SBrick protocol 17 (https://social.sbrick.com/wiki/view/pageId/11/slug/the-sbrick-ble-protocol)
- Python 3.4
- BlueZ
- bluepy
- pyuv
- paho-mqtt
- mosquitto
BlueZ is a official Bluetooth protocol stack on Linux.
Useful commands for BlueZ:
$ hciconfig -a
$ sudo hciconfig hci0 up
$ sudo hcitool lescan
$ bluetoothctl
Must make sure bluetooth control is active.
$ sudo hciconfig hci0 up
bluepy is a Python module which provides an interface to Bluetooth LE on Linux.
Ref: https://github.com/IanHarvey/bluepy
$ pip3 install bluepy
pyuv is a Python module which provides an interface to libuv.
Ref: https://github.com/saghul/pyuv
$ sudo pip3 install pyuv
paho-mqtt is a Python module which provides an interface to MQTT protocol.
Ref: https://pypi.python.org/pypi/paho-mqtt/1.2.3
$ sudo pip3 install paho-mqtt
mosquitto is an open source message broker thant implements MQTT protocol versions 3.1 and 3.1.1
Ref: https://mosquitto.org/
$ apt-get install mosquitto mosquitto-clients
$ sudo systemctl status mosquitto
$ sudo systemctl start mosquitto
$ sudo systemctl enable mosquitto
- Usage of
sbrick_server.py
$ python3 sbrick_server.py -h
usage: sbrick_server.py [-h] (--connect | --scan) [--broker-ip BROKER_IP]
[--broker-port BROKER_PORT]
[--sbrick-id SBRICK_ID [SBRICK_ID ...]]
[--log-level LOG_LEVEL]
optional arguments:
-h, --help show this help message and exit
--connect Connect to SBrick
--scan Scan for getting SBrick information
--connect:
--broker-ip BROKER_IP
MQTT broker ip address. Default is 127.0.0.1
--broker-port BROKER_PORT
MQTT broker port. Default is 1883
--sbrick-id SBRICK_ID [SBRICK_ID ...]
list of SBrick MAC to connect to
--log-level LOG_LEVEL
Log verbose level. Default is INFO. [DEBUG | INFO |
WARNING | ERROR | CRITICAL]
- Scan SBrick devices. You can get SBrick MAC in this step. (must run as root)
$ sudo python3 sbrick_server.py --scan
$ sudo hcitool lescan
- Start daemon server with connecting to a SBrick device. (must run as root)
$ sudo python3 sbrick_server.py --connect --broker-ip 127.0.0.1 --broker-port 1883 --log-level debug --sbrick-id 11:22:33:44:55:66
- You can also control multiple SBrick devices. The usage is below:
$ sudo python3 sbrick_server.py --connect ..... --sbrick-id <SBrick1 MAC> <SBrick2 MAC> <SBrick3 MAC>
Example of SbrickIpcClient
class:
from lib.sbrick_m2mipc import SbrickIpcClient
# MQTT connect
client = SbrickIpcClient(broker_ip='127.0.0.1', broker_port=1883)
client.connect()
# Get voltage and temperature of a SBrick device
json_response = client.rr_get_adc(sbrick_id='11:22:33:44:55:66', timeout=5)
# Get information of UUID, sercies and characteristics of a SBrick device
json_response = client.rr_get_service(sbrick_id='11:22:33:44:55:66', timeout=5)
# Get general information of a SBrick device
json_response = client.rr_get_general(sbrick_id='11:22:33:44:55:66', timeout=5)
# Stop power functions
client.publish_stop(sbrick_id='11:22:33:44:55:66', channel_list=['00', '01'])
# Drive a power function
client.publish_drive(sbrick_id='11:22:33:44:55:66',
channnel='00',
direction='00',
power='f0',
exec_time=10)
# MQTT disconnect
client.disconnect()
SbrickIpcClient
class has below methods:
- SbrickIpcClient()
- Init class
- Parameters:
logger
: logger object. logging. Default is sys.stdoutbroker_ip
: string. IP address of MQTT. Default is 127.0.0.1broker_port
: number. Port number of MQTT. Default is 1883
- publish_dirve()
- Drive s LEGO power function
- Parameters:
sbrick_id
: string. SBrick mac address. 11:22:33:44:55:66channel
: string. hex_string. the LEGO power function port you want to drive. 00, 01, 02, 03direction
: string. clockwise or counterclockwise. hex_string. 00, 01power
: string. hex_string. 00 ~ FFexec_time
: number. the executing time of LEGO power function in seconds, 5566 means forever
- Return:
- No return
- publish_stop()
- Stop LEGO power functions
- Parameters:
sbrick_id
: string. SBrick mac address. 11:22:33:44:55:66channel_list
: list. list of channels to stop. [00, 01]
- Return:
- No return
- rr_get_service()
- Get information of UUID, services and characteristis of a SBrick device
- Parameters:
sbrick_id
: string. SBrick mac address. 11:22:33:44:55:66timeout
: number. timeout to get service in seconds.
- Return:
- Information in JSON format.
ret_code
: 100(success), 220(bad_param), 300(timeout)
- rr_get_adc()
- Get information of voltage and temperature of a SBrick device
- Parameters:
sbrick_id
: string. SBrick mac address. 11:22:33:44:55:66timeout
: number. timeout to get service in seconds.
- Return:
- Information in JSON format.
ret_code
: 100(success), 220(bad_param), 300(timeout)
- rr_get_general()
- Get general information of a SBrick device
- Parameters:
sbrick_id
: string. SBrick mac address. 11:22:33:44:55:66timeout
: number. timeout to get service in seconds.
- Return:
- Information in JSON format.
ret_code
: 100(success), 220(bad_param), 300(timeout)