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.

flow.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env python
  2. import sys
  3. import os
  4. import asyncio
  5. from poll import (
  6. ble_conn,
  7. get_current_temp,
  8. get_target_temp, set_target_temp,
  9. get_unit_is_fahrenheit,
  10. get_state, set_state
  11. )
  12. terminal_width = os.get_terminal_size().columns - 15
  13. def print_bar(value, start, end, unit):
  14. width = terminal_width
  15. s = "\r"
  16. s += "#" * int((value - start) / (end - start) * width)
  17. s += "-" * (width - int((value - start) / (end - start) * width))
  18. s += " {}{}".format(value, unit)
  19. print(s, end="", flush=True)
  20. async def sleep(t):
  21. w = terminal_width
  22. if t < w:
  23. w = int(t)
  24. print_bar(0, 0, w, "s")
  25. for i in range(0, w):
  26. await asyncio.sleep(t / w)
  27. print_bar(i + 1, 0, w, "s")
  28. print()
  29. async def ensure_target_temp(client, temp):
  30. for i in range(0, 10):
  31. await set_target_temp(client, temp)
  32. target = await get_target_temp(client)
  33. if target == temp:
  34. return
  35. #print("retry target temp")
  36. raise Exception("could not set target temp")
  37. async def ensure_state(client, state):
  38. for i in range(0, 10):
  39. await set_state(client, state)
  40. heater, pump = state
  41. t_heater, t_pump = await get_state(client)
  42. if (heater == t_heater) and (pump == t_pump):
  43. return
  44. #print("retry state")
  45. raise Exception("could not set state")
  46. async def wait_for_temp(client, temp):
  47. print("Setting temperature {}".format(temp))
  48. await ensure_target_temp(client, temp)
  49. print("Waiting for temperature to rise...")
  50. start = await get_current_temp(client)
  51. curr = start
  52. print_bar(curr, start, temp, " degC")
  53. while curr < temp:
  54. await asyncio.sleep(1.0)
  55. curr = await get_current_temp(client)
  56. print_bar(curr, start, temp, " degC")
  57. print()
  58. print("Reached temperature {}".format(temp))
  59. async def flow_step(client, temp, t_wait, t_pump):
  60. await wait_for_temp(client, temp)
  61. print("Waiting {}s for heat to settle...".format(t_wait))
  62. await sleep(t_wait)
  63. print("Pumping for {}s".format(t_pump))
  64. await ensure_state(client, (True, True)) # turn on pump
  65. await sleep(t_pump)
  66. await ensure_state(client, (True, False)) # turn off pump
  67. async def flow(client):
  68. print("Turning on heater")
  69. await ensure_state(client, (True, False))
  70. await flow_step(client, 185.0, 10.0, 7.0)
  71. await flow_step(client, 195.0, 5.0, 23.0)
  72. await flow_step(client, 205.0, 5.0, 23.0)
  73. print("Notification by pumping three times...")
  74. for i in range(0, 3):
  75. await asyncio.sleep(1.0)
  76. await ensure_state(client, (True, True)) # turn on pump
  77. await asyncio.sleep(1.0)
  78. await ensure_state(client, (True, False)) # turn off pump
  79. print("Turning heater off")
  80. await ensure_state(client, (False, False)) # turn off heater and pump
  81. print("Resetting temperature")
  82. await ensure_target_temp(client, 190.0)
  83. async def main(address):
  84. device = await ble_conn(address)
  85. print("Connecting...")
  86. async with device as client:
  87. try:
  88. if await get_unit_is_fahrenheit(client):
  89. print("Imperial American scum is currently not supported :P")
  90. sys.exit(42)
  91. print("Starting Workflow")
  92. await flow(client)
  93. except:
  94. print("\nTurning heater and pump off")
  95. await ensure_state(client, (False, False)) # turn off heater and pump
  96. raise
  97. print("Disconnecting...")
  98. if __name__ == "__main__":
  99. if len(sys.argv) <= 1:
  100. print("Please pass MAC address of device")
  101. sys.exit(1)
  102. asyncio.run(main(sys.argv[1]))