S&B Volcano vaporizer remote control with Pi Pico W
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

poll.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env python
  2. import sys
  3. import asyncio
  4. from bleak import BleakClient, BleakScanner
  5. from bleak.exc import BleakDBusError
  6. async def ble_conn(address):
  7. attempts = 10
  8. print("Opening device..", end = "", flush=True)
  9. while attempts > 0:
  10. try:
  11. if (attempts % 10) == 0:
  12. print(".", end = "", flush=True)
  13. device = await BleakScanner.find_device_by_address(address)
  14. client = BleakClient(device)
  15. print("", flush=True)
  16. return client
  17. except BleakDBusError as e:
  18. attempts -= 1
  19. if attempts == 0:
  20. print("", flush=True)
  21. print(e)
  22. else:
  23. await asyncio.sleep(0.1)
  24. print("Could not connect to device")
  25. return None
  26. async def get_current_temp(client):
  27. val = await client.read_gatt_char("10110001-5354-4f52-5a26-4249434b454c")
  28. num = int.from_bytes(val, byteorder="little")
  29. return num / 10.0
  30. async def get_target_temp(client):
  31. val = await client.read_gatt_char("10110003-5354-4f52-5a26-4249434b454c")
  32. num = int.from_bytes(val, byteorder="little")
  33. return num / 10.0
  34. async def set_target_temp(client, temp):
  35. val = int(temp * 10.0)
  36. d = val.to_bytes(4, byteorder="little")
  37. await client.write_gatt_char("10110003-5354-4f52-5a26-4249434b454c", d)
  38. async def get_unit_is_fahrenheit(client):
  39. val = await client.read_gatt_char("1010000d-5354-4f52-5a26-4249434b454c")
  40. num = int.from_bytes(val, byteorder="little")
  41. return (num & 0x200) != 0
  42. async def get_state(client):
  43. val = await client.read_gatt_char("1010000c-5354-4f52-5a26-4249434b454c")
  44. num = int.from_bytes(val, byteorder="little")
  45. heater = (num & 0x0020) != 0
  46. pump = (num & 0x2000) != 0
  47. return (heater, pump)
  48. async def set_state(client, state):
  49. heater, pump = state
  50. if heater:
  51. await client.write_gatt_char("1011000f-5354-4f52-5a26-4249434b454c", 0)
  52. else:
  53. await client.write_gatt_char("10110010-5354-4f52-5a26-4249434b454c", 0)
  54. if pump:
  55. await client.write_gatt_char("10110013-5354-4f52-5a26-4249434b454c", 0)
  56. else:
  57. await client.write_gatt_char("10110014-5354-4f52-5a26-4249434b454c", 0)
  58. async def test_poll(client):
  59. temp = await get_current_temp(client)
  60. print("Current Temperature: {}".format(temp))
  61. target = await get_target_temp(client)
  62. print("Target Temperature: {}".format(target))
  63. fahrenheit = await get_unit_is_fahrenheit(client)
  64. if fahrenheit:
  65. print("Unit is Fahrenheit")
  66. else:
  67. print("Unit is Celsius")
  68. heater, pump = await get_state(client)
  69. if heater:
  70. print("Heater is On")
  71. else:
  72. print("Heater is Off")
  73. if pump:
  74. print("Pump is On")
  75. else:
  76. print("Pump is Off")
  77. async def test(address):
  78. device = await ble_conn(address)
  79. print("Connecting...")
  80. async with device as client:
  81. print("Writing...")
  82. await set_target_temp(client, 190.0)
  83. print("Reading...")
  84. for i in range(0, 5):
  85. await test_poll(client)
  86. print()
  87. await asyncio.sleep(2.0)
  88. if __name__ == "__main__":
  89. if len(sys.argv) <= 1:
  90. print("Please pass MAC address of device")
  91. sys.exit(1)
  92. asyncio.run(test(sys.argv[1]))