S&B Volcano vaporizer remote control with Pi Pico W
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

poll.py 3.8KB

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