Skip to content

Commit 5eb2b5b

Browse files
committed
Add Json Characteristic examples
1 parent 65f350b commit 5eb2b5b

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

examples/ble_json_central.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# SPDX-FileCopyrightText: 2020 Mark Raleson
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
# Read sensor readings from peripheral BLE device using JSON characteristic.
6+
7+
from adafruit_ble import BLERadio
8+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
9+
from ble_json_service import SensorService
10+
11+
12+
ble = BLERadio()
13+
connection = None
14+
15+
while True:
16+
17+
if not connection:
18+
print("Scanning for BLE device advertising our sensor service...")
19+
for adv in ble.start_scan(ProvideServicesAdvertisement):
20+
if SensorService in adv.services:
21+
connection = ble.connect(adv)
22+
print("Connected")
23+
break
24+
ble.stop_scan()
25+
26+
if connection and connection.connected:
27+
service = connection[SensorService]
28+
service.settings = {
29+
'unit': 'celsius' # 'fahrenheit'
30+
}
31+
while connection.connected:
32+
print('Sensors: ', service.sensors)

examples/ble_json_peripheral.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# SPDX-FileCopyrightText: 2020 Mark Raleson
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
# Provide readable sensor values and writable settings to connected devices via Json characteristic.
6+
7+
import time
8+
import random
9+
from adafruit_ble import BLERadio
10+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
11+
from ble_json_service import SensorService
12+
13+
14+
# Create BLE radio, custom service, and advertisement.
15+
ble = BLERadio()
16+
service = SensorService()
17+
advertisement = ProvideServicesAdvertisement(service)
18+
19+
# Function to get some fake weather sensor readings for this example in the desired unit.
20+
def measure(unit):
21+
temperature = random.uniform(0.0, 10.0)
22+
humidity = random.uniform(0.0, 100.0)
23+
if unit == 'fahrenheit':
24+
temperature = (temperature * 9.0 / 5.0) + 32.0
25+
return {
26+
'temperature': temperature,
27+
'humidity': humidity
28+
}
29+
30+
# Advertise until another device connects, when a device connects, provide sensor data.
31+
while True:
32+
print('Advertise services')
33+
ble.stop_advertising() # you need to do this to stop any persistent old advertisement
34+
ble.start_advertising(advertisement)
35+
36+
print("Waiting for connection...")
37+
while not ble.connected:
38+
pass
39+
40+
print("Connected")
41+
while ble.connected:
42+
settings = service.settings
43+
measurement = measure(settings.get('unit', 'celsius'))
44+
service.sensors = measurement
45+
print('Settings: ', settings)
46+
print('Sensors: ', measurement)
47+
time.sleep(.25)
48+
49+
print('Disconnected')

examples/ble_json_service.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from adafruit_ble.uuid import VendorUUID
2+
from adafruit_ble.services import Service
3+
from adafruit_ble.characteristics import Characteristic
4+
from adafruit_ble.characteristics.json import JsonCharacteristic
5+
6+
7+
# A custom service with two Json characteristics for this device. The "sensors" characteristic
8+
# provides updated sensor values for any connected device to read. The "settings" characteristic
9+
# can be changed by any connected device to update the peripheral's settings. The UUID of your
10+
# service can be any valid random uuid (some BLE UUID's are reserved).
11+
# NOTE: Json data is limited by characteristic max_length of 512 byes.
12+
class SensorService(Service):
13+
uuid = VendorUUID("51ad213f-e568-4e35-84e4-67af89c79ef0")
14+
15+
settings = JsonCharacteristic(
16+
uuid=VendorUUID("e077bdec-f18b-4944-9e9e-8b3a815162b4"),
17+
properties=Characteristic.READ | Characteristic.WRITE,
18+
initial_value={
19+
'unit': 'celsius'
20+
}
21+
)
22+
23+
sensors = JsonCharacteristic(
24+
uuid=VendorUUID("528ff74b-fdb8-444c-9c64-3dd5da4135ae"),
25+
properties=Characteristic.READ,
26+
)
27+
28+
def __init__(self, service=None):
29+
super().__init__(service=service)
30+
self.connectable = True

0 commit comments

Comments
 (0)