S&B Volcano vaporizer remote control with Pi Pico W
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

flow.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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, 185.0, 10.0, 5.0)
  54. await flow_step(client, 195.0, 5.0, 20.0)
  55. await flow_step(client, 205.0, 5.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. print("Resetting temperature")
  65. await set_target_temp(client, 190.0)
  66. async def main(address):
  67. device = await ble_conn(address)
  68. print("Connecting...")
  69. async with device as client:
  70. try:
  71. if await get_unit_is_fahrenheit(client):
  72. print("Imperial American scum is currently not supported :P")
  73. sys.exit(42)
  74. print("Starting Workflow")
  75. await flow(client)
  76. except:
  77. print("\nTurning heater and pump off")
  78. await set_state(client, (False, False)) # turn off heater and pump
  79. raise
  80. print("Disconnecting...")
  81. if __name__ == "__main__":
  82. if len(sys.argv) <= 1:
  83. print("Please pass MAC address of device")
  84. sys.exit(1)
  85. asyncio.run(main(sys.argv[1]))