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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/usr/bin/env python
  2. import uasyncio as asyncio
  3. import bluetooth
  4. import time
  5. from scan import ble_scan
  6. serviceUuidVolcano3 = bluetooth.UUID("10100000-5354-4f52-5a26-4249434b454c")
  7. serviceUuidVolcano4 = bluetooth.UUID("10110000-5354-4f52-5a26-4249434b454c")
  8. service3 = None
  9. service4 = None
  10. characteristic1 = None
  11. characteristic3 = None
  12. characteristicd = None
  13. characteristicc = None
  14. characteristicf = None
  15. characteristic10 = None
  16. characteristic13 = None
  17. characteristic14 = None
  18. async def cache_services_characteristics(device, cb = None):
  19. global service3, service4
  20. global characteristic1, characteristic3
  21. global characteristicd, characteristicc
  22. global characteristicf, characteristic10
  23. global characteristic13, characteristic14
  24. service3 = None
  25. service4 = None
  26. characteristic1 = None
  27. characteristic3 = None
  28. characteristicd = None
  29. characteristicc = None
  30. characteristicf = None
  31. characteristic10 = None
  32. characteristic13 = None
  33. characteristic14 = None
  34. val = 0
  35. service3 = await device.service(serviceUuidVolcano3, 100)
  36. if cb != None:
  37. val += 1
  38. await cb(val)
  39. uuidc = bluetooth.UUID("1010000c-5354-4f52-5a26-4249434b454c")
  40. uuidd = bluetooth.UUID("1010000d-5354-4f52-5a26-4249434b454c")
  41. async for c in service3.characteristics(None, 100):
  42. if c.uuid == uuidc:
  43. characteristicc = c
  44. if cb != None:
  45. val += 1
  46. await cb(val)
  47. if c.uuid == uuidd:
  48. characteristicd = c
  49. if cb != None:
  50. val += 1
  51. await cb(val)
  52. # -------------------------------------------------------------------------
  53. service4 = await device.service(serviceUuidVolcano4, 100)
  54. if cb != None:
  55. val += 1
  56. await cb(val)
  57. uuid1 = bluetooth.UUID("10110001-5354-4f52-5a26-4249434b454c")
  58. uuid3 = bluetooth.UUID("10110003-5354-4f52-5a26-4249434b454c")
  59. uuidf = bluetooth.UUID("1011000f-5354-4f52-5a26-4249434b454c")
  60. uuid10 = bluetooth.UUID("10110010-5354-4f52-5a26-4249434b454c")
  61. uuid13 = bluetooth.UUID("10110013-5354-4f52-5a26-4249434b454c")
  62. uuid14 = bluetooth.UUID("10110014-5354-4f52-5a26-4249434b454c")
  63. async for c in service4.characteristics(None, 100):
  64. if c.uuid == uuid1:
  65. characteristic1 = c
  66. if cb != None:
  67. val += 1
  68. await cb(val)
  69. if c.uuid == uuid3:
  70. characteristic3 = c
  71. if cb != None:
  72. val += 1
  73. await cb(val)
  74. if c.uuid == uuidf:
  75. characteristicf = c
  76. if cb != None:
  77. val += 1
  78. await cb(val)
  79. if c.uuid == uuid10:
  80. characteristic10 = c
  81. if cb != None:
  82. val += 1
  83. await cb(val)
  84. if c.uuid == uuid13:
  85. characteristic13 = c
  86. if cb != None:
  87. val += 1
  88. await cb(val)
  89. if c.uuid == uuid14:
  90. characteristic14 = c
  91. if cb != None:
  92. val += 1
  93. await cb(val)
  94. return (val >= 10)
  95. async def get_current_temp(device):
  96. val = await characteristic1.read()
  97. num = int.from_bytes(val, "little")
  98. return num / 10.0
  99. async def get_target_temp(device):
  100. val = await characteristic3.read()
  101. num = int.from_bytes(val, "little")
  102. return num / 10.0
  103. async def set_target_temp(device, temp):
  104. attempts = 3
  105. while attempts > 0:
  106. val = int(temp * 10.0)
  107. d = val.to_bytes(4, "little")
  108. await characteristic3.write(d)
  109. attempts -= 1
  110. target = await get_target_temp(device)
  111. if abs(target - temp) < 0.5:
  112. return
  113. raise RuntimeError("Could not set target temperature")
  114. async def get_unit_is_fahrenheit(device):
  115. val = await characteristicd.read()
  116. num = int.from_bytes(val, "little")
  117. return (num & 0x200) != 0
  118. async def get_state(device):
  119. val = await characteristicc.read()
  120. num = int.from_bytes(val, "little")
  121. heater = (num & 0x0020) != 0
  122. pump = (num & 0x2000) != 0
  123. return (heater, pump)
  124. async def set_state(device, state):
  125. attempts = 3
  126. while attempts > 0:
  127. attempts -= 1
  128. try:
  129. heater, pump = state
  130. if heater == True:
  131. await characteristicf.write(int(0).to_bytes(1, "little"))
  132. elif heater == False:
  133. await characteristic10.write(int(0).to_bytes(1, "little"))
  134. if pump == True:
  135. await characteristic13.write(int(0).to_bytes(1, "little"))
  136. elif pump == False:
  137. await characteristic14.write(int(0).to_bytes(1, "little"))
  138. except:
  139. time.sleep(0.05)
  140. continue
  141. break