S&B Volcano vaporizer remote control with Pi Pico W
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

flow.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 wait_for_temp(client, temp):
  30. print("Setting temperature {}".format(temp))
  31. await set_target_temp(client, temp)
  32. print("Waiting for temperature to rise...")
  33. start = await get_current_temp(client)
  34. curr = start
  35. print_bar(curr, start, temp, " degC")
  36. while curr < temp:
  37. await asyncio.sleep(1.0)
  38. curr = await get_current_temp(client)
  39. print_bar(curr, start, temp, " degC")
  40. print()
  41. print("Reached temperature {}".format(temp))
  42. async def flow_step(client, temp, t_wait, t_pump):
  43. await wait_for_temp(client, temp)
  44. print("Waiting {}s for heat to settle...".format(t_wait))
  45. await sleep(t_wait)
  46. print("Pumping for {}s".format(t_pump))
  47. await set_state(client, (True, True)) # turn on pump
  48. await sleep(t_pump)
  49. await set_state(client, (True, False)) # turn off pump
  50. async def flow(client):
  51. print("Turning on heater")
  52. await set_state(client, (True, False))
  53. await flow_step(client, 190.0, 20.0, 5.0)
  54. await flow_step(client, 205.0, 10.0, 20.0)
  55. await flow_step(client, 220.0, 10.0, 20.0)
  56. print("Notification by pumping three times...")
  57. for i in range(0, 3):
  58. await asyncio.sleep(1.0)
  59. await set_state(client, (True, True)) # turn on pump
  60. await asyncio.sleep(1.0)
  61. await set_state(client, (True, False)) # turn off pump
  62. print("Turning heater off")
  63. await set_state(client, (False, False)) # turn off heater and pump
  64. async def main(address):
  65. device = await ble_conn(address)
  66. print("Connecting...")
  67. async with device as client:
  68. try:
  69. if await get_unit_is_fahrenheit(client):
  70. print("Imperial American scum is currently not supported :P")
  71. sys.exit(42)
  72. print("Starting Workflow")
  73. await flow(client)
  74. except:
  75. print("\nTurning heater off")
  76. await set_state(client, (False, False)) # turn off heater and pump
  77. raise
  78. print("Disconnecting...")
  79. if __name__ == "__main__":
  80. if len(sys.argv) <= 1:
  81. print("Please pass MAC address of device")
  82. sys.exit(1)
  83. asyncio.run(main(sys.argv[1]))