#!/usr/bin/env python # -*- coding: utf-8 -*- """ ble_temp_watch.py - poll the device "devinfo" CLI command over the read/write characteristic every 2s and print chip temperature + raw ADC/trim values. usage: .venv-ble/Scripts/python ble_temp_watch.py Use it to verify the TS reading: warm the chip with a finger and watch the adc value rise / temperature change. Ctrl+C to stop. """ import asyncio import re import time from bleak import BleakClient from ble_cli_test import CLI_RW_UUID, find_device POLL_S = 2.0 TEMP_RE = re.compile(r"chip temp: (\S+) C \(adc=(\d+) trim=(\d+)\)") async def main(): dev = await find_device() print("connecting %s (%s) ..." % (dev.name, dev.address)) async with BleakClient(dev) as client: print("connected, mtu=%d - polling devinfo every %.0fs, Ctrl+C to stop" % (client.mtu_size, POLL_S)) print("time temp adc trim") while True: await client.write_gatt_char(CLI_RW_UUID, b"devinfo", response=True) await asyncio.sleep(0.1) rsp = (await client.read_gatt_char(CLI_RW_UUID)).decode("ascii", "replace") m = TEMP_RE.search(rsp) if m: print("%s %-7s %-5s %s" % (time.strftime("%H:%M:%S"), m.group(1), m.group(2), m.group(3))) else: print("%s (parse failed) %r" % (time.strftime("%H:%M:%S"), rsp[:80])) await asyncio.sleep(POLL_S) if __name__ == "__main__": try: asyncio.run(main()) except KeyboardInterrupt: pass