Debugged version of the Python Bluetooth Internet Gateways Study Guide from Bluetooth SIG V2_0_1
A web BLE gateway based on an embedded HTTP server (Apache2) and WS (Websocketd) with my added MQTT adapter
Tested with ROCK-3C and Silicon Labs Thunderboard Sense (SLTB001A) & Thunderboard EFR32BG22 (SLTB010A)
Get latest Debian XFCE image - CLI not working - tested with rock-3c_debian_bullseye_xfce_b42.img.xz
Set systemd default target to CLI: sudo systemctrl set-default multi-user.target
Update system:
sudo apt upgrade
Reboot sudo reboot
Install Paho-mqtt in venv:
sudo apt install libdbus-1-dev libglib2.0-dev libcairo2-dev libgirepository1.0-dev
Install Mosquitto:
Setup listeners:
# Enable settings by listener
per_listener_settings true
# Allow anonymous access on port 1883
listener 1883
allow_anonymous true
Restart:
Copy gateway code:
cd bluetooth-gateway-study-guide/implementation/solutions/peripherals/gateway_mqtt_no_security
Create virtual env:
source venv/bin/activate
Install packages into venv:
pip install dbus-python
pip install PyGObject
pip install paho-mqtt
Open a separate Terminal and start Bluettotctl:
[bluetooth]# scan on
Discovery started
[CHG] Controller 50:5A:65:27:45:D8 Discovering: yes
...
[NEW] Device 9A:61:DA:87:D2:C4 Nano33BLESENSE
[bluetooth]# connect 9A:61:DA:87:D2:C4
Attempting to connect to 9A:61:DA:87:D2:C4
[CHG] Device 9A:61:DA:87:D2:C4 Connected: yes
Connection successful
[NEW] Primary Service (Handle 0xa504)
/org/bluez/hci0/dev_9A_61_DA_87_D2_C4/service0006
00001801-0000-1000-8000-00805f9b34fb
Generic Attribute Profile
...
[Nano33BLESENSE]# menu gatt
Menu gatt:
Available commands:
...
[Nano33BLESENSE]# list-attributes
Primary Service (Handle 0x17f0)
/org/bluez/hci0/dev_9A_61_DA_87_D2_C4/service0006
00001801-0000-1000-8000-00805f9b34fb
Generic Attribute Profile
Characteristic (Handle 0x0001)
/org/bluez/hci0/dev_9A_61_DA_87_D2_C4/service0006/char0007
00002a05-0000-1000-8000-00805f9b34fb
Service Changed
Descriptor (Handle 0x0000)
/org/bluez/hci0/dev_9A_61_DA_87_D2_C4/service0006/char0007/desc0009
00002902-0000-1000-8000-00805f9b34fb
Client Characteristic Configuration
Primary Service (Handle 0x819c)
/org/bluez/hci0/dev_9A_61_DA_87_D2_C4/service000a
0000181a-0000-1000-8000-00805f9b34fb
Environmental Sensing
Characteristic (Handle 0x7c60)
/org/bluez/hci0/dev_9A_61_DA_87_D2_C4/service000a/char000b
00002a6e-0000-1000-8000-00805f9b34fb
Temperature
Descriptor (Handle 0x0000)
/org/bluez/hci0/dev_9A_61_DA_87_D2_C4/service000a/char000b/desc000d
00002902-0000-1000-8000-00805f9b34fb
Client Characteristic Configuration
Characteristic (Handle 0x9070)
/org/bluez/hci0/dev_9A_61_DA_87_D2_C4/service000a/char000e
00002a6f-0000-1000-8000-00805f9b34fb
Humidity
Descriptor (Handle 0x0000)
/org/bluez/hci0/dev_9A_61_DA_87_D2_C4/service000a/char000e/desc0010
00002902-0000-1000-8000-00805f9b34fb
Client Characteristic Configuration
Start mqtt_client in venv:
/home/rock/gateway_mqtt_no_security/venv/bin/python /home/rock/gateway_mqtt_no_security/gateway/mqtt_client.py 'localhost' 'test/gateway'
Open another Terminal: Make sure you have scanned and connected your peripheral using Bluetoothctl Issue mqtt command to get temperature according to characteristic handle for your device - the output shows in the other terminal:
mosquitto_pub -h localhost -t "test/gateway/in/read_characteristic" -m '{"bdaddr":"9A:61:DA:87:D2:C4", "handle":"/org/bluez/hci0/dev_9A_61_DA_87_D2_C4/service000a/char000b"}'
Result:
2024-03-10 12:37:53,163 - {"bdaddr": "9A:61:DA:87:D2:C4", "handle": "/org/bluez/hci0/dev_9A_61_DA_87_D2_C4/service000a/char000b", "cmd": "read_characteristic", "value": 2120, "result": 0}
Convert result: 2120 = 21.20 C