Created
July 30, 2024 05:39
-
-
Save skittleson/28135bab60c0643c507a006c7ae143af to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# scan for temp sensors in area. | |
import aioble | |
import uasyncio as asyncio | |
from ubluetooth import UUID | |
import bluetooth | |
import struct | |
__TIMEOUT_MS = 30000 | |
__SCAN_TIM_MS = 20000 | |
async def scan_for_device(): | |
matching = {} | |
async with aioble.scan(duration_ms=__SCAN_TIM_MS, interval_us=30000, window_us=30000, active=True) as scanner: | |
async for result in scanner: | |
adv_name = str(result.name()) | |
if 'ATC_' in adv_name: | |
print(result, adv_name, result.rssi) | |
matching[adv_name] = result.device | |
return matching | |
async def list_services(device): | |
try: | |
connection = await device.connect(timeout_ms=__TIMEOUT_MS) | |
print("Connected to device") | |
SERVICE_UUID = bluetooth.UUID(0x181A) | |
CHAR_UUID = bluetooth.UUID(0x2A1F) | |
service = await connection.service(SERVICE_UUID,timeout_ms=__TIMEOUT_MS) | |
if service is not None: | |
temp_char = await service.characteristic(CHAR_UUID,timeout_ms=__TIMEOUT_MS) | |
data = await temp_char.read(timeout_ms=__TIMEOUT_MS) | |
#data = await temp_char.notified(timeout_ms=20000) | |
# little endian | |
temperature = struct.unpack('<h', data)[0] | |
print(temperature) | |
except Exception as e: | |
print(e) | |
if connection is not None: | |
await connection.disconnect() | |
print("Disconnected from device") | |
async def main(): | |
print("Scanning for devices...") | |
devices = await scan_for_device() | |
if len(devices) > 0: | |
for device in devices: | |
await list_services(devices[device]) | |
else: | |
print(f'Device {device} not found') | |
asyncio.run(main()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment