Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wesbasinger/f9ad94318ddcc9383fb447886aba7392 to your computer and use it in GitHub Desktop.
Save wesbasinger/f9ad94318ddcc9383fb447886aba7392 to your computer and use it in GitHub Desktop.
import sys
from machine import Pin, ADC
pot = ADC(Pin(0))
pot.atten(ADC.ATTN_11DB)
t = 0
# ruff: noqa: E402
sys.path.append("")
from micropython import const
import uasyncio as asyncio
import aioble
import bluetooth
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
# 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)
# Helper to encode the temperature characteristic encoding (sint16, hundredths of a degree).
def _encode_temperature(temp_deg_c):
return struct.pack("<h", int(temp_deg_c))
# This would be periodically polling a hardware sensor.
async def sensor_task():
global t
while True:
pot_value = pot.read()
if abs(pot_value - t) > 10:
t = pot_value
temp_characteristic.write(_encode_temperature(t))
await asyncio.sleep_ms(1000)
# Serially wait for connections. Don't advertise while a central is
# connected.
async def peripheral_task():
while True:
async with await aioble.advertise(
_ADV_INTERVAL_MS,
name="mpy-temp",
services=[_ENV_SENSE_UUID],
appearance=_ADV_APPEARANCE_GENERIC_THERMOMETER,
) as connection:
print("Connection from", connection.device)
await connection.disconnected(timeout_ms=None)
# Run both tasks.
async def main():
t1 = asyncio.create_task(sensor_task())
t2 = asyncio.create_task(peripheral_task())
await asyncio.gather(t1, t2)
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment