S&B Volcano vaporizer remote control with Pi Pico W
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

poll.py 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. if cb != None:
  53. await cb(val)
  54. # -------------------------------------------------------------------------
  55. service4 = await device.service(serviceUuidVolcano4, 100)
  56. if cb != None:
  57. val += 1
  58. await cb(val)
  59. uuid1 = bluetooth.UUID("10110001-5354-4f52-5a26-4249434b454c")
  60. uuid3 = bluetooth.UUID("10110003-5354-4f52-5a26-4249434b454c")
  61. uuidf = bluetooth.UUID("1011000f-5354-4f52-5a26-4249434b454c")
  62. uuid10 = bluetooth.UUID("10110010-5354-4f52-5a26-4249434b454c")
  63. uuid13 = bluetooth.UUID("10110013-5354-4f52-5a26-4249434b454c")
  64. uuid14 = bluetooth.UUID("10110014-5354-4f52-5a26-4249434b454c")
  65. async for c in service4.characteristics(None, 100):
  66. if c.uuid == uuid1:
  67. characteristic1 = c
  68. if cb != None:
  69. val += 1
  70. #await cb(val)
  71. if c.uuid == uuid3:
  72. characteristic3 = c
  73. if cb != None:
  74. val += 1
  75. #await cb(val)
  76. if c.uuid == uuidf:
  77. characteristicf = c
  78. if cb != None:
  79. val += 1
  80. #await cb(val)
  81. if c.uuid == uuid10:
  82. characteristic10 = c
  83. if cb != None:
  84. val += 1
  85. #await cb(val)
  86. if c.uuid == uuid13:
  87. characteristic13 = c
  88. if cb != None:
  89. val += 1
  90. #await cb(val)
  91. if c.uuid == uuid14:
  92. characteristic14 = c
  93. if cb != None:
  94. val += 1
  95. #await cb(val)
  96. if cb != None:
  97. await cb(val)
  98. return (val >= 10)
  99. async def get_current_temp(device):
  100. val = await characteristic1.read()
  101. num = int.from_bytes(val, "little")
  102. return num / 10.0
  103. async def get_target_temp(device):
  104. val = await characteristic3.read()
  105. num = int.from_bytes(val, "little")
  106. return num / 10.0
  107. async def set_target_temp(device, temp):
  108. attempts = 3
  109. while attempts > 0:
  110. val = int(temp * 10.0)
  111. d = val.to_bytes(4, "little")
  112. await characteristic3.write(d)
  113. attempts -= 1
  114. target = await get_target_temp(device)
  115. if abs(target - temp) < 0.5:
  116. return
  117. raise RuntimeError("Could not set target temperature")
  118. async def get_unit_is_fahrenheit(device):
  119. val = await characteristicd.read()
  120. num = int.from_bytes(val, "little")
  121. return (num & 0x200) != 0
  122. async def get_state(device):
  123. val = await characteristicc.read()
  124. num = int.from_bytes(val, "little")
  125. heater = (num & 0x0020) != 0
  126. pump = (num & 0x2000) != 0
  127. return (heater, pump)
  128. async def set_state(device, state):
  129. attempts = 3
  130. while attempts > 0:
  131. attempts -= 1
  132. try:
  133. heater, pump = state
  134. if heater == True:
  135. await characteristicf.write(int(0).to_bytes(1, "little"))
  136. elif heater == False:
  137. await characteristic10.write(int(0).to_bytes(1, "little"))
  138. if pump == True:
  139. await characteristic13.write(int(0).to_bytes(1, "little"))
  140. elif pump == False:
  141. await characteristic14.write(int(0).to_bytes(1, "little"))
  142. except:
  143. time.sleep(0.05)
  144. continue
  145. break