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.

poll.py 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/usr/bin/env python3
  2. # ----------------------------------------------------------------------------
  3. # Copyright (c) 2023 Thomas Buck (thomas@xythobuz.de)
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # See <http://www.gnu.org/licenses/>.
  16. # ----------------------------------------------------------------------------
  17. import uasyncio as asyncio
  18. import bluetooth
  19. import time
  20. from scan import ble_scan
  21. serviceUuidVolcano3 = bluetooth.UUID("10100000-5354-4f52-5a26-4249434b454c")
  22. serviceUuidVolcano4 = bluetooth.UUID("10110000-5354-4f52-5a26-4249434b454c")
  23. service3 = None
  24. service4 = None
  25. characteristic1 = None
  26. characteristic3 = None
  27. characteristicd = None
  28. characteristicc = None
  29. characteristicf = None
  30. characteristic10 = None
  31. characteristic13 = None
  32. characteristic14 = None
  33. async def cache_services_characteristics(device, cb = None):
  34. global service3, service4
  35. global characteristic1, characteristic3
  36. global characteristicd, characteristicc
  37. global characteristicf, characteristic10
  38. global characteristic13, characteristic14
  39. service3 = None
  40. service4 = None
  41. characteristic1 = None
  42. characteristic3 = None
  43. characteristicd = None
  44. characteristicc = None
  45. characteristicf = None
  46. characteristic10 = None
  47. characteristic13 = None
  48. characteristic14 = None
  49. val = 0
  50. service3 = await device.service(serviceUuidVolcano3, 100)
  51. if cb != None:
  52. val += 1
  53. await cb(val)
  54. uuidc = bluetooth.UUID("1010000c-5354-4f52-5a26-4249434b454c")
  55. uuidd = bluetooth.UUID("1010000d-5354-4f52-5a26-4249434b454c")
  56. async for c in service3.characteristics(None, 100):
  57. if c.uuid == uuidc:
  58. characteristicc = c
  59. if cb != None:
  60. val += 1
  61. await cb(val)
  62. if c.uuid == uuidd:
  63. characteristicd = c
  64. if cb != None:
  65. val += 1
  66. await cb(val)
  67. # -------------------------------------------------------------------------
  68. service4 = await device.service(serviceUuidVolcano4, 100)
  69. if cb != None:
  70. val += 1
  71. await cb(val)
  72. uuid1 = bluetooth.UUID("10110001-5354-4f52-5a26-4249434b454c")
  73. uuid3 = bluetooth.UUID("10110003-5354-4f52-5a26-4249434b454c")
  74. uuidf = bluetooth.UUID("1011000f-5354-4f52-5a26-4249434b454c")
  75. uuid10 = bluetooth.UUID("10110010-5354-4f52-5a26-4249434b454c")
  76. uuid13 = bluetooth.UUID("10110013-5354-4f52-5a26-4249434b454c")
  77. uuid14 = bluetooth.UUID("10110014-5354-4f52-5a26-4249434b454c")
  78. async for c in service4.characteristics(None, 100):
  79. if c.uuid == uuid1:
  80. characteristic1 = c
  81. if cb != None:
  82. val += 1
  83. await cb(val)
  84. if c.uuid == uuid3:
  85. characteristic3 = c
  86. if cb != None:
  87. val += 1
  88. await cb(val)
  89. if c.uuid == uuidf:
  90. characteristicf = c
  91. if cb != None:
  92. val += 1
  93. await cb(val)
  94. if c.uuid == uuid10:
  95. characteristic10 = c
  96. if cb != None:
  97. val += 1
  98. await cb(val)
  99. if c.uuid == uuid13:
  100. characteristic13 = c
  101. if cb != None:
  102. val += 1
  103. await cb(val)
  104. if c.uuid == uuid14:
  105. characteristic14 = c
  106. if cb != None:
  107. val += 1
  108. await cb(val)
  109. return (val >= 10)
  110. async def get_current_temp(device):
  111. val = await characteristic1.read()
  112. num = int.from_bytes(val, "little")
  113. return num / 10.0
  114. async def get_target_temp(device):
  115. val = await characteristic3.read()
  116. num = int.from_bytes(val, "little")
  117. return num / 10.0
  118. async def set_target_temp(device, temp):
  119. attempts = 3
  120. while attempts > 0:
  121. val = int(temp * 10.0)
  122. d = val.to_bytes(4, "little")
  123. await characteristic3.write(d)
  124. attempts -= 1
  125. target = await get_target_temp(device)
  126. if abs(target - temp) < 0.5:
  127. return
  128. raise RuntimeError("Could not set target temperature")
  129. async def get_unit_is_fahrenheit(device):
  130. val = await characteristicd.read()
  131. num = int.from_bytes(val, "little")
  132. return (num & 0x200) != 0
  133. async def get_state(device):
  134. val = await characteristicc.read()
  135. num = int.from_bytes(val, "little")
  136. heater = (num & 0x0020) != 0
  137. pump = (num & 0x2000) != 0
  138. return (heater, pump)
  139. async def set_state(device, state):
  140. heater, pump = state
  141. attempts = 3
  142. while attempts > 0:
  143. attempts -= 1
  144. try:
  145. if heater == True:
  146. await characteristicf.write(int(0).to_bytes(1, "little"))
  147. elif heater == False:
  148. await characteristic10.write(int(0).to_bytes(1, "little"))
  149. if pump == True:
  150. await characteristic13.write(int(0).to_bytes(1, "little"))
  151. elif pump == False:
  152. await characteristic14.write(int(0).to_bytes(1, "little"))
  153. except:
  154. time.sleep(0.05)
  155. continue
  156. break