Advertisement
Guest User

Thermopro TP920 Python Communication Testing

a guest
Jun 3rd, 2025
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.02 KB | None | 0 0
  1. import asyncio
  2. from bleak import BleakClient
  3.  
  4. DEVICE_ADDRESS = "CE:51:8C:f0:BC:DB"
  5.  
  6. WRITE_CHAR_UUID = "1086fff1-3343-4817-8bb2-b32206336ce8"
  7. NOTIFY_CHAR_UUID = "1086fff2-3343-4817-8bb2-b32206336ce8"
  8. CELSIUS = "20010c2d"
  9. FAHREN = "20010f30"
  10.  
  11. HANDSHAKE_COMMANDS = [
  12.     bytes.fromhex("01098a7a13b73ed68b67c2a0"),
  13.     bytes.fromhex("410041"),
  14.     bytes.fromhex("28040a2d8c6e5d"),
  15.     bytes.fromhex("20010f30"), # Initializes device in fahrenheit, change f30 to c2d for Celsius
  16.     bytes.fromhex("23060200ffffffff27"),
  17.     bytes.fromhex("23060400ffffffff29")
  18. ]
  19.  
  20. def notification_handler(sender, data):
  21.     print("Notification from {sender}: {data.hex()}")
  22.  
  23.     hex_data = data.hex()
  24.     probe1 = hex_data[14:18]
  25.     probe2 = hex_data[22:26]
  26.  
  27.     def decimal_conversion(probe):
  28.         if probe != 'ffff':
  29.             return float(probe[:-1] + '.' + probe[-1])
  30.         else:
  31.             return probe
  32.  
  33.     print(f"Probe1 Temps: {decimal_conversion(probe1)}, Probe2 Temps: {decimal_conversion(probe2)}")
  34.  
  35. async def change_mode():
  36.     async with BleakClient(DEVICE_ADDRESS) as client:
  37.         print(f"Connecting to {DEVICE_ADDRESS}...")
  38.         await client.write_gatt_char(WRITE_CHAR_UUID, FAHREN)
  39.         print("Mode Changed.")
  40.  
  41. async def main():
  42.     async with BleakClient(DEVICE_ADDRESS) as client:
  43.         print("Connected to device")
  44.  
  45.         for i, cmd in enumerate(HANDSHAKE_COMMANDS, 1):
  46.             print(f"Sending handshake {i}: {cmd.hex()}")
  47.             await client.write_gatt_char(WRITE_CHAR_UUID, cmd)
  48.             await asyncio.sleep(0.4) # Slight delay between writes
  49.  
  50.         print("Subscribing to notifications...")
  51.         await client.start_notify(NOTIFY_CHAR_UUID, notification_handler)
  52.  
  53.         print("Listening for notifications. Press Ctrl+C to stop.")
  54.         try:
  55.             while True:
  56.                 await asyncio.sleep(1)
  57.         except KeyboardInterrupt:
  58.             print("Stopping...")
  59.  
  60.         await client.stop_notify(NOTIFY_CHAR_UUID)
  61.  
  62. asyncio.run(main())
  63. asyncio.run(change_mode())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement