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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/env python
  2. import uasyncio as asyncio
  3. import sys
  4. import time
  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 = 100 #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="")
  20. def sleep(t):
  21. print_bar(0, 0, t, "s")
  22. for i in range(0, t):
  23. time.sleep(1.0)
  24. print_bar(i + 1, 0, t, "s")
  25. print()
  26. async def wait_for_temp(client, temp):
  27. print("Setting temperature {}".format(temp))
  28. await set_target_temp(client, temp)
  29. print("Waiting for temperature to rise...")
  30. start = await get_current_temp(client)
  31. curr = start
  32. print_bar(curr, start, temp, " degC")
  33. while curr < temp:
  34. time.sleep(1.0)
  35. curr = await get_current_temp(client)
  36. print_bar(curr, start, temp, " degC")
  37. print()
  38. print("Reached temperature {}".format(temp))
  39. async def flow_step(client, temp, t_wait, t_pump):
  40. await wait_for_temp(client, temp)
  41. print("Waiting {}s for heat to settle...".format(t_wait))
  42. sleep(t_wait)
  43. print("Pumping for {}s".format(t_pump))
  44. await set_state(client, (True, True)) # turn on pump
  45. sleep(t_pump)
  46. await set_state(client, (True, False)) # turn off pump
  47. async def flow(client):
  48. print("Turning on heater")
  49. await set_state(client, (True, False))
  50. await flow_step(client, 190.0, 15.0, 5.0 - 4)
  51. await flow_step(client, 205.0, 10.0, 20.0 - 4)
  52. await flow_step(client, 220.0, 10.0, 20.0 - 4)
  53. print("Notification by pumping three times...")
  54. for i in range(0, 3):
  55. #time.sleep(1.0 / 3)
  56. await set_state(client, (True, True)) # turn on pump
  57. #time.sleep(1.0 / 3)
  58. await set_state(client, (True, False)) # turn off pump
  59. print("Turning heater off")
  60. await set_state(client, (False, False)) # turn off heater and pump
  61. print("Setting temperature back to 190")
  62. await set_target_temp(client, 190.0)
  63. if __name__ == "__main__":
  64. async def main(address):
  65. client = await ble_conn(address)
  66. try:
  67. if await get_unit_is_fahrenheit(client):
  68. raise RuntimeError("Imperial American scum is currently not supported :P")
  69. print("Starting Workflow")
  70. await flow(client)
  71. except:
  72. print("\nTurning heater off")
  73. await set_state(client, (False, False)) # turn off heater and pump
  74. raise
  75. import machine
  76. led_onboard = machine.Pin("LED", machine.Pin.OUT)
  77. for i in range(0, 3):
  78. led_onboard.on()
  79. time.sleep(0.2)
  80. led_onboard.off()
  81. time.sleep(0.2)
  82. print("ready")
  83. while True:
  84. if rp2.bootsel_button() == 1:
  85. led_onboard.on()
  86. print("run")
  87. try:
  88. asyncio.run(main(None))
  89. print("done")
  90. except Exception as e:
  91. print(e)
  92. led_onboard.off()
  93. machine.reset()
  94. time.sleep(0.2)