diff --git a/main.py b/main.py index d0eb59f..6608db2 100644 --- a/main.py +++ b/main.py @@ -1,118 +1,68 @@ +import sys + +sys.path.append("") + +from micropython import const + +import uasyncio as asyncio import aioble import bluetooth -import machine -import uasyncio as asyncio + +import random +import struct + +# org.bluetooth.service.environmental_sensing +_ENV_SENSE_UUID = bluetooth.UUID(0x181A) +# org.bluetooth.characteristic.temperature +_ENV_SENSE_TEMP_UUID = bluetooth.UUID(0x2A6E) +# org.bluetooth.characteristic.gap.appearance.xml +_ADV_APPEARANCE_GENERIC_THERMOMETER = const(768) + +# How frequently to send advertising beacons. +_ADV_INTERVAL_MS = 250_000 -_OBD_UUID_SERVICE = bluetooth.UUID(0xfff0) -_OBD_UUID_CHARACTERISTIC = bluetooth.UUID(0xfff1) - -led = machine.Pin("LED", machine.Pin.OUT) -connected = False -alive = False +# Register GATT server. +temp_service = aioble.Service(_ENV_SENSE_UUID) +temp_characteristic = aioble.Characteristic( + temp_service, _ENV_SENSE_TEMP_UUID, read=True, notify=True +) +aioble.register_services(temp_service) -async def find_remote(): - async with aioble.scan(duration_ms=5000, interval_us=30000, window_us=30000, active=True) as scanner: - async for result in scanner: - if (result.name() == "OBDBLE"): - print("Found OBDBLE") - for item in result.services(): - print(item) - if _OBD_UUID_SERVICE in result.services(): - print("Found OBDBLE service") - return result.device - return None +# Helper to encode the temperature characteristic encoding (sint16, hundredths of a degree). +def _encode_temperature(temp_deg_c): + return struct.pack("