-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathra_ble.py
30 lines (23 loc) · 956 Bytes
/
ra_ble.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#
# Copyright reelyActive 2023-2024
# We believe in an open Internet of Things
#
import bluetooth
ble = bluetooth.BLE()
ble.active(True)
def send_value(value):
# value is a percentage
# we want a hex value btw 0 and 65535 (16-bits),
# then split into TWO bytes (MSB and LSB)
bitvalue = int(value * .01 * 65535)
high = (bitvalue >> 8)
low = (bitvalue & 0x00FF)
# Advertising payload bytes:
# 0: 0x05 = Length (not including the length byte itself)
# 1: 0x16 = 16-bit service data (from GAP)
# 2-3: 0x2af9 = 16-bit UUID of generic level characteristic (little endian)
# 4-5: 0x1234 = 16-bit generic level value (little endian) 4660 / 65535 = 7.11%
#_ADV_PAYLOAD = [ 0x05, 0x16, 0xf9, 0x2a, 0x34, 0x12 ]
_ADV_PAYLOAD = [ 0x05, 0x16, 0xf9, 0x2a, low, high ]
_ADV_INTERVAL_US = 500000 # 500000 microseconds = .5 seconds
ble.gap_advertise(_ADV_INTERVAL_US, adv_data=bytearray(_ADV_PAYLOAD))