severRFCOMMBluetooth.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. # file: GoKitchen-serverRFCOMMBluetooth.py
  2. # auth: Marc Bayon <[email protected]>
  3. # desc: Rfcomm server for GPIO handling.
  4. #
  5. from bluetooth import *
  6. import RPi.GPIO as GPIO
  7. import time
  8. from time import sleep
  9. GPIO_BT_CONNECTED = 21
  10. GPIO_ON_OFF = 20
  11. GPIO_POT5 = 16
  12. GPIO_POT4 = 12
  13. GPIO_POT3 = 25
  14. GPIO_POT2 = 24
  15. GPIO_POT1 = 23
  16. GPIO_BUZZ = 18
  17. HOB_POWER = 0
  18. POWER_ON = False
  19. def main():
  20. while True:
  21. HOB_POWER = 0
  22. POWER_ON = False
  23. GPIO.setmode(GPIO.BCM)
  24. GPIO.setwarnings(False)
  25. GPIO.setup(GPIO_BT_CONNECTED,GPIO.OUT)
  26. GPIO.setup(GPIO_ON_OFF,GPIO.OUT)
  27. GPIO.setup(GPIO_POT5,GPIO.OUT)
  28. GPIO.setup(GPIO_POT4,GPIO.OUT)
  29. GPIO.setup(GPIO_POT3,GPIO.OUT)
  30. GPIO.setup(GPIO_POT2,GPIO.OUT)
  31. GPIO.setup(GPIO_POT1,GPIO.OUT)
  32. GPIO.setup(GPIO_BUZZ,GPIO.OUT)
  33. clearGPIO(True)
  34. server_sock=BluetoothSocket( RFCOMM )
  35. server_sock.bind(("",PORT_ANY))
  36. server_sock.listen(1)
  37. port = server_sock.getsockname()[1]
  38. uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
  39. advertise_service( server_sock, "SampleServer",
  40. service_id = uuid,
  41. service_classes = [ uuid, SERIAL_PORT_CLASS ],
  42. profiles = [ SERIAL_PORT_PROFILE ],
  43. # protocols = [ OBEX_UUID ]
  44. )
  45. print("Waiting for connection on RFCOMM channel %d" % port)
  46. client_sock, client_info = server_sock.accept()
  47. print("Accepted connection from ", client_info)
  48. try:
  49. while True:
  50. data = client_sock.recv(1024)
  51. if len(data) == 0: break
  52. print("received [%s]" % data)
  53. if data == "STATUS":
  54. print ("Connected")
  55. GPIO.output(GPIO_BT_CONNECTED,GPIO.HIGH)
  56. #server_sock.send("OK")
  57. print("Send [OK]")
  58. if data == "ON":
  59. print ("Vitro on")
  60. GPIO.output(GPIO_ON_OFF,GPIO.HIGH)
  61. POWER_ON = True
  62. #server_sock.send("ON_OK")
  63. print("Send [ON_OK]")
  64. buzzer(1)
  65. if data == "OFF":
  66. print ("Vitro OFF")
  67. clearGPIO(False)
  68. HOB_POWER = 0
  69. POWER_ON = False
  70. #server_sock.send("OFF_OK")
  71. print("Send [OFF_OK]")
  72. buzzer(2)
  73. if data == "PWUP":
  74. print ("Power UP")
  75. if POWER_ON:
  76. HOB_POWER = HOB_POWER+1
  77. if DisplayPower(HOB_POWER):
  78. #server_sock.send("PWUP_OK")
  79. print("Send [PWUP_OK]")
  80. buzzer(HOB_POWER)
  81. else :
  82. HOB_POWER = HOB_POWER -1
  83. #server_sock.send("PWUP_NOK")
  84. print("Send [PWUP_NOK]")
  85. else:
  86. #server_sock.send("PWUP_NOK")
  87. print("Send [PWUP_NOK]")
  88. if data == "PWDOWN":
  89. if POWER_ON:
  90. print ("Power DOWN")
  91. HOB_POWER = HOB_POWER-1
  92. if DisplayPower(HOB_POWER):
  93. #server_sock.send("PWDOWN_OK")
  94. print("Send [PWDOWN_OK]")
  95. buzzer(HOB_POWER)
  96. else :
  97. HOB_POWER = HOB_POWER +1
  98. #server_sock.send("PWDOWN_NOK")
  99. print("Send [PWDOWN_NOK]")
  100. else:
  101. #server_sock.send("PWDOWN_NOK")
  102. print("Send [PWDOWN_NOK]")
  103. except IOError:
  104. pass
  105. print("disconnected")
  106. clearGPIO(True)
  107. client_sock.close()
  108. server_sock.close()
  109. print("all done")
  110. def buzzer (times):
  111. for x in range(0,times):
  112. GPIO.output(GPIO_BUZZ,GPIO.HIGH)
  113. sleep(0.2)
  114. GPIO.output(GPIO_BUZZ,GPIO.LOW)
  115. sleep(0.2)
  116. def clearGPIO(hardOff):
  117. if hardOff:
  118. GPIO.output(GPIO_BT_CONNECTED,GPIO.LOW)
  119. GPIO.output(GPIO_ON_OFF,GPIO.LOW)
  120. GPIO.output(GPIO_POT1,GPIO.LOW)
  121. GPIO.output(GPIO_POT2,GPIO.LOW)
  122. GPIO.output(GPIO_POT3,GPIO.LOW)
  123. GPIO.output(GPIO_POT4,GPIO.LOW)
  124. GPIO.output(GPIO_POT5,GPIO.LOW)
  125. HOB_POWER = 0
  126. POWER_ON = 0
  127. def DisplayPower(power):
  128. if power == 1:
  129. GPIO.output(GPIO_POT1,GPIO.HIGH)
  130. GPIO.output(GPIO_POT2,GPIO.LOW)
  131. GPIO.output(GPIO_POT3,GPIO.LOW)
  132. GPIO.output(GPIO_POT4,GPIO.LOW)
  133. GPIO.output(GPIO_POT5,GPIO.LOW)
  134. return True
  135. if power == 2:
  136. GPIO.output(GPIO_POT1,GPIO.HIGH)
  137. GPIO.output(GPIO_POT2,GPIO.HIGH)
  138. GPIO.output(GPIO_POT3,GPIO.LOW)
  139. GPIO.output(GPIO_POT4,GPIO.LOW)
  140. GPIO.output(GPIO_POT5,GPIO.LOW)
  141. return True
  142. if power == 3:
  143. GPIO.output(GPIO_POT1,GPIO.HIGH)
  144. GPIO.output(GPIO_POT2,GPIO.HIGH)
  145. GPIO.output(GPIO_POT3,GPIO.HIGH)
  146. GPIO.output(GPIO_POT4,GPIO.LOW)
  147. GPIO.output(GPIO_POT5,GPIO.LOW)
  148. return True
  149. if power == 4:
  150. GPIO.output(GPIO_POT1,GPIO.HIGH)
  151. GPIO.output(GPIO_POT2,GPIO.HIGH)
  152. GPIO.output(GPIO_POT3,GPIO.HIGH)
  153. GPIO.output(GPIO_POT4,GPIO.HIGH)
  154. GPIO.output(GPIO_POT5,GPIO.LOW)
  155. return True
  156. if power == 5:
  157. GPIO.output(GPIO_POT1,GPIO.HIGH)
  158. GPIO.output(GPIO_POT2,GPIO.HIGH)
  159. GPIO.output(GPIO_POT3,GPIO.HIGH)
  160. GPIO.output(GPIO_POT4,GPIO.HIGH)
  161. GPIO.output(GPIO_POT5,GPIO.HIGH)
  162. return True
  163. if power > 5 or power < 0:
  164. return False
  165. if __name__ == "__main__":
  166. main()