Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.54 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. import time
  3. import os
  4. import glob
  5. import subprocess
  6. from bluetooth import *
  7.  
  8. os.system('modprobe w1-gpio')
  9. os.system('modprobe w1-therm')
  10.  
  11. GPIO.setmode(GPIO.BCM)
  12.  
  13. GPIO.setup(18, GPIO.OUT)
  14. GPIO.setup(23, GPIO.OUT)
  15. GPIO.setup(24, GPIO.OUT)
  16. GPIO.setup(9, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  17. GPIO.setup(8, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  18. GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  19.  
  20. #RELAYS
  21. GPIO.setup(2, GPIO.OUT)
  22. GPIO.setup(3, GPIO.OUT)
  23. GPIO.setup(4, GPIO.OUT)
  24.  
  25.  
  26. #base_dir = '/sys/bus/w1/devices/'
  27. #device_folder = glob.glob(base_dir + '28*')[0]
  28. #device_file = device_folder + 'w1_slave'
  29.  
  30. server_sock = BluetoothSocket(RFCOMM)
  31. server_sock.bind(("", PORT_ANY))
  32. server_sock.listen(1)
  33.  
  34. port = server_sock.getsockname()[1]
  35.  
  36. uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
  37.  
  38. advertise_service(server_sock, "Malinova potica PY",
  39. service_id = uuid,
  40. service_classes = [uuid, SERIAL_PORT_CLASS],
  41. profiles = [SERIAL_PORT_PROFILE],
  42. )
  43.  
  44.  
  45.  
  46.  
  47.  
  48. mode = 1
  49.  
  50. potValue = 2
  51.  
  52. #RELAYS
  53. GPIO.output(2, False)
  54. GPIO.output(3, False)
  55. GPIO.output(4, True)
  56.  
  57.  
  58. def checkBTConnection():
  59. try:
  60. client_sock.getpeername()
  61. print "CONN OK"
  62. return True
  63. except:
  64. print "CONN DOWN"
  65. return False
  66.  
  67.  
  68. #CALLBACKS (INTERRUPTS)
  69. relay2On = False
  70. relay3On = False
  71. relay4On = False
  72.  
  73. def buttonPress():
  74. global relay2On
  75. global relay3On
  76. global relay4On
  77. relay2On = not relay2On
  78. GPIO.output(2, relay2On)
  79. print "BTN PRESS"
  80. print relay2On
  81. #GPIO.output(18, True)
  82. if checkBTConnection() == True:
  83. relay4On = not relay4On
  84. #GPIO.output(4, relay4On)
  85. if relay2On == True:
  86. client_sock.send("Button_ON")
  87. else:
  88. client_sock.send("Button_OFF")
  89.  
  90. else:
  91. relay3On = not relay3On
  92. #GPIO.output(3, relay3On)
  93.  
  94. def buttonPressCallback(channel):
  95.  
  96.  
  97. if GPIO.input(9) != 0:
  98. return False
  99. buttonPress()
  100.  
  101.  
  102.  
  103. pot1Change = False
  104. pot2Change = False
  105.  
  106. def upFunction():
  107. global potValue
  108. potValue = potValue + 1
  109. if checkBTConnection() == True:
  110. client_sock.send("Up_" + str(potValue))
  111. def downFunction():
  112. global potValue
  113. potValue = potValue - 1
  114. if checkBTConnection() == True:
  115. client_sock.send("Down_" + str(potValue))
  116.  
  117. def pot1ChangeCallback(channel):
  118. #print "POT1"
  119.  
  120. if relay2On == True:
  121. return False
  122.  
  123. global pot1Change
  124. global pot2Change
  125. if pot2Change == True:
  126. print "Levo"
  127. downFunction()
  128.  
  129. pot1Change = False
  130. pot2Change = False
  131. else:
  132. pot1Change = not pot1Change
  133.  
  134.  
  135. def pot2ChangeCallback(channel):
  136. #print "POT2"
  137.  
  138. if relay2On == True:
  139. return False
  140.  
  141. global pot1Change
  142. global pot2Change
  143. if pot1Change == True:
  144. print "Desno"
  145. upFunction()
  146.  
  147. pot1Change = False
  148. pot2Change = False
  149. else:
  150. pot2Change = not pot2Change
  151.  
  152.  
  153.  
  154. GPIO.add_event_detect(9, GPIO.FALLING, callback=buttonPressCallback, bouncetime = 300)
  155.  
  156. GPIO.add_event_detect(8, GPIO.BOTH, callback=pot1ChangeCallback, bouncetime = 500)
  157. GPIO.add_event_detect(25, GPIO.BOTH, callback=pot2ChangeCallback, bouncetime = 500)
  158.  
  159.  
  160. while True:
  161.  
  162. try:
  163.  
  164. GPIO.output(23, False)
  165. GPIO.output(24, True)
  166.  
  167. subprocess.call(['sudo','sh','/home/pi/Desktop/BluetoothPY/BTsetup.sh'])
  168.  
  169. print "Cakam povezavo port %d" % port
  170.  
  171. client_sock, client_info = server_sock.accept()
  172. print "Sprejeto od ", client_info
  173.  
  174. #SEND STATUS
  175. statusString = "BUTTON_"
  176. if relay2On == True:
  177. statusString += "ON;"
  178. else:
  179. statusString += "OFF;"
  180. statusString += "POTVALUE_" + str(potValue)
  181. client_sock.send(statusString)
  182.  
  183. GPIO.output(24, False)
  184. GPIO.output(23, True)
  185.  
  186. pot1 = GPIO.input(8)
  187. pot2 = GPIO.input(25)
  188.  
  189. ignoreChange = False
  190.  
  191. while True:
  192.  
  193. if mode == 1:
  194.  
  195. data = client_sock.recv(1024)
  196. if len(data) == 0: break
  197.  
  198. print "Podatki: "
  199. print data
  200.  
  201. if data == "on":
  202. GPIO.output(18, True)
  203. client_sock.send("LED_ON")
  204. elif data == "off":
  205. GPIO.output(18, False)
  206. client_sock.send("LED_OFF")
  207. elif data == "Button":
  208. buttonPress()
  209. elif data == "Down":
  210. downFunction()
  211. elif data == "Up":
  212. upFunction()
  213. else:
  214. client_sock.send("Unknown")
  215.  
  216. elif mode == 2:
  217.  
  218. #OLD TESTING MODE
  219.  
  220. #if GPIO.input(8) != pot1:
  221. # print "Rotate"
  222. # client_sock.send("Rotate")
  223. # pot1 = GPIO.input(8)
  224. # pot2 = GPIO.input(25)
  225.  
  226. #if pot1 != GPIO.input(8):
  227. # if ignoreChange == False:
  228. # print 'Levo'
  229. # client_sock.send("Levo")
  230. # ignoreChange = True
  231. # else:
  232. # ignoreChange = False
  233. # pot1 = GPIO.input(8)
  234.  
  235. #if pot2 != GPIO.input(25):
  236. # if ignoreChange == False:
  237. # print 'Desno'
  238. # client_sock.send("Desno")
  239. # ignoreChange = True
  240. # else:
  241. # ignoreChange = False
  242.  
  243. # pot2 = GPIO.input(25)
  244. time.sleep(0.01)
  245.  
  246.  
  247. else:
  248. print "Unknown mode"
  249.  
  250. except IOError:
  251. print "IOERROR"
  252. pass
  253. except KeyboardInterrupt:
  254. print "Konec"
  255. client_sock.close()
  256. server_sock.close()
  257. print "Cleanaup narjen"
  258. break
  259. #!bin/bin/sh
  260.  
  261.  
  262. sudo hciconfig hci0 name "Malinova potica"
  263. sudo hciconfig hci0 piscan
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement