Last active
March 18, 2023 08:49
-
-
Save nebelgrau77/56ece15c19cef97b14110ea725d2dea6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Reading and decoding temperature values over BLE from Arduino BLE Sense | |
''' | |
import asyncio | |
from bleak import BleakClient | |
from bleak.backends.characteristic import BleakGATTCharacteristic | |
ADDRESS = "E6:A3:44:CD:9C:4F" # BLE sense | |
TEMP_UUID = "00002a6e-0000-1000-8000-00805f9b34fb" # temperature characteristic, data from HTS221 | |
HUM_UUID = "00002a6f-0000-1000-8000-00805f9b34fb" # humidity characteristic, data from HTS221 | |
PRESS_UUID = "00002a6d-0000-1000-8000-00805f9b34fb" # pressure characteristic, data from LPS22 | |
LIGHT_UUID = "00002a77-0000-1000-8000-00805f9b34fb" # irradiance characteristic (clear light data from APDS9660) | |
BATT_UUID = "00002a19-0000-1000-8000-00805f9b34fb" # battery level characteristic (dummy ADC reading on pin A0) | |
def notification_handler(characteristic: BleakGATTCharacteristic, data: bytearray): | |
'''prints received data''' | |
# for test purposes just the first byte of each characteristic, without any decoding and conversions | |
value = data[0] | |
print(f'{characteristic.description}: {value}') | |
async def main(address, char_uuids): | |
async with BleakClient(address) as client: | |
print(f'Connected: {client.is_connected}') | |
for uuid in char_uuids: | |
await client.start_notify(uuid, notification_handler) | |
while True: | |
await asyncio.sleep(5.0) | |
for uuid in char_uuids: | |
await client.stop_notify(uuid) | |
asyncio.run(main(ADDRESS, [BATT_UUID, TEMP_UUID, HUM_UUID, PRESS_UUID, LIGHT_UUID])) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment