For the purpose of this guide, we will use a Mi humidity and temperature sensor. We will use BLE connector to connect to the device and collect data.
Our goals for this device:
At this moment the only info about the device are it’s MAC address and characteristic identifiers:
MAC Address - 4C:65:A8:DF:85:C0
Characteristics IDs:
00002A00-0000-1000-8000-00805F9B34FB - identifier of standard device name characteristic (GATT Specification)
226CAA55-6476-4566-7562-66734470666D - identifier of a custom temperature and humidity characteristic (How to get characteristics list from BLE device)
Before we start configure our BLE connector within the gateway. These libraries depend on type of your OS:
You should install some system libraries for using BLE Connector and bluepy library for python. This command will install required libraries:
This command will install bluepy library:
|
You should install some system libraries for using BLE Connector and bluepy library for python. This command will install required libraries:
This command will install bluepy library:
|
We need to create ble setup file and put configuration there. For example:
{
"name": "BLE Connector",
"rescanIntervalSeconds": 100,
"checkIntervalSeconds": 100,
"scanTimeSeconds": 5,
"passiveScanMode": true,
"devices": [
{
"name": "Temperature and humidity sensor",
"MACAddress": "4C:65:A8:DF:85:C0",
"telemetry": [
{
"key": "temperature",
"method": "notify",
"characteristicUUID": "226CAA55-6476-4566-7562-66734470666D",
"byteFrom": 2,
"byteTo": 6
},
{
"key": "humidity",
"method": "notify",
"characteristicUUID": "226CAA55-6476-4566-7562-66734470666D",
"byteFrom": 9,
"byteTo": 13
}
],
"attributes": [
{
"key": "name",
"characteristicUUID": "00002A00-0000-1000-8000-00805F9B34FB",
"method": "read",
"byteFrom": 0,
"byteTo": -1
}
],
"attributeUpdates": [
{
"attributeOnThingsBoard": "sharedName",
"characteristicUUID": "00002A00-0000-1000-8000-00805F9B34FB"
}
],
"serverSideRpc": [
{
"methodRPC": "sharedName",
"withResponse": true,
"characteristicUUID": "00002A00-0000-1000-8000-00805F9B34FB",
"methodProcessing": "write"
}
]
}
]
}
About sections of BLE configuration file you can read more here.
In this guide we will use configuration above.
Let’s analyze our settings:
If you have a different device, you should provide your device characteristic identifiers in the configuration json.
We have saved the configuration file as ble.json in the config folder (the directory, that contains the general configuration file - tb_gateway.yaml).
To use the connector, we must turn it on in the main configuration file (tb_gateway.yaml)
In “connectors” section we should uncomment following strings:
-
name: BLE Connector
type: ble
configuration: ble.json
To run the gateway with BLE Connector we will use root permissions.
Command for run depends on type of installation.
If you have installed the gateway as daemon, run the following command:
sudo systemctl restart thingsboard-gateway
If you have installed the gateway as a python module (using pip package manager or from sources), use following command or script to run the gateway.
Notice: You must place correct path to the main configuration file (tb_gateway.yaml) in the command/script.
sudo python3 -c 'from thingsboard_gateway.gateway.tb_gateway_service import TBGatewayService; TBGatewayService("YOUR_PATH_HERE")'
or script:
from thingsboard_gateway.gateway.tb_gateway_service import TBGatewayService
config_file_path = "YOUR_PATH_HERE"
TBGatewayService(config_file_path)
Check data in your ThingsBoard instance, that you have been configured in the general configuration guide.
- Go to the your ThingsBoard instance and login.
- Go to the “Devices” tab. “Temperature and Humidity sensor” should be there.
Go to the device details, ATTRIBUTES tab, which contains all client-side attributes, including requested from our configuration file attributes.
Notify: Attributes from the GATT specification updates only on first connect to device after the gateway start.
Let’s try to change the device name. We should follow several steps:
Notify: Some devices can reset their name to default.
To get all available device characteristics, you can use the following Python script:
from bluepy.btle import Peripheral
MAC = "PUT_DEVICE_MAC_ADDRESS_HERE"
peripheral = Peripheral(MAC)
for service in peripheral.getServices():
for characteristic in service.getCharacteristics():
print("Characteristic - id: %s\tname (if exists): %s\tavailable methods: %s" % (str(characteristic.uuid), str(characteristic), characteristic.propertiesToString()))
Notate: To run the script - you should have installed Bluepy library and permissions to access to Bluetooth module.
After run the script, you will receive output in console like following:
Characteristic - id: 00002a00-0000-1000-8000-00805f9b34fb name (if exists): Characteristic <Device Name> available methods: READ WRITE
Characteristic - id: 00002a01-0000-1000-8000-00805f9b34fb name (if exists): Characteristic <Appearance> available methods: READ
Characteristic - id: 00002a04-0000-1000-8000-00805f9b34fb name (if exists): Characteristic <Peripheral Preferred Connection Parameters> available methods: READ
Characteristic - id: 00002a05-0000-1000-8000-00805f9b34fb name (if exists): Characteristic <Service Changed> available methods: INDICATE
Characteristic - id: 226caa55-6476-4566-7562-66734470666d name (if exists): Characteristic <226caa55-6476-4566-7562-66734470666d> available methods: NOTIFY
Characteristic - id: 226cbb55-6476-4566-7562-66734470666d name (if exists): Characteristic <226cbb55-6476-4566-7562-66734470666d> available methods: WRITE NOTIFY
Characteristic - id: 00002a19-0000-1000-8000-00805f9b34fb name (if exists): Characteristic <Battery Level> available methods: READ NOTIFY
Characteristic - id: 00002a29-0000-1000-8000-00805f9b34fb name (if exists): Characteristic <Manufacturer Name String> available methods: READ
Characteristic - id: 00002a24-0000-1000-8000-00805f9b34fb name (if exists): Characteristic <Model Number String> available methods: READ
Characteristic - id: 00002a25-0000-1000-8000-00805f9b34fb name (if exists): Characteristic <Serial Number String> available methods: READ
Characteristic - id: 00002a27-0000-1000-8000-00805f9b34fb name (if exists): Characteristic <Hardware Revision String> available methods: READ
Characteristic - id: 00002a26-0000-1000-8000-00805f9b34fb name (if exists): Characteristic <Firmware Revision String> available methods: READ
Characteristic - id: 00001532-1212-efde-1523-785feabcd123 name (if exists): Characteristic <00001532-1212-efde-1523-785feabcd123> available methods: WRITE NO RESPONSE
Characteristic - id: 00001531-1212-efde-1523-785feabcd123 name (if exists): Characteristic <00001531-1212-efde-1523-785feabcd123> available methods: WRITE NOTIFY
Characteristic - id: 00001534-1212-efde-1523-785feabcd123 name (if exists): Characteristic <00001534-1212-efde-1523-785feabcd123> available methods: READ
Characteristic - id: 00000001-0000-1000-8000-00805f9b34fb name (if exists): Characteristic <0001> available methods: WRITE NOTIFY
Characteristic - id: 00000002-0000-1000-8000-00805f9b34fb name (if exists): Characteristic <0002> available methods: READ
Characteristic - id: 00000004-0000-1000-8000-00805f9b34fb name (if exists): Characteristic <0004> available methods: READ
Characteristic - id: 00000010-0000-1000-8000-00805f9b34fb name (if exists): Characteristic <0010> available methods: WRITE
Characteristic - id: 00000013-0000-1000-8000-00805f9b34fb name (if exists): Characteristic <0013> available methods: READ WRITE
Characteristic - id: 00000014-0000-1000-8000-00805f9b34fb name (if exists): Characteristic <0014> available methods: READ WRITE
Where:
id - characteristic identifier.
name (if exists) - Characteristic name, if it has description in a GATT specification.
available methods - Characteristic supports methods.
Characteristic methods:
Usually characteristics where name is equals to id is custom.