Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import RPi.GPIO as GPIO
- import time
- import os
- import glob
- import subprocess
- from bluetooth import *
- os.system('modprobe w1-gpio')
- os.system('modprobe w1-therm')
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(18, GPIO.OUT)
- GPIO.setup(23, GPIO.OUT)
- GPIO.setup(24, GPIO.OUT)
- GPIO.setup(9, GPIO.IN, pull_up_down=GPIO.PUD_UP)
- GPIO.setup(8, GPIO.IN, pull_up_down=GPIO.PUD_UP)
- GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_UP)
- #RELAYS
- GPIO.setup(2, GPIO.OUT)
- GPIO.setup(3, GPIO.OUT)
- GPIO.setup(4, GPIO.OUT)
- #base_dir = '/sys/bus/w1/devices/'
- #device_folder = glob.glob(base_dir + '28*')[0]
- #device_file = device_folder + 'w1_slave'
- server_sock = BluetoothSocket(RFCOMM)
- server_sock.bind(("", PORT_ANY))
- server_sock.listen(1)
- port = server_sock.getsockname()[1]
- uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
- advertise_service(server_sock, "Malinova potica PY",
- service_id = uuid,
- service_classes = [uuid, SERIAL_PORT_CLASS],
- profiles = [SERIAL_PORT_PROFILE],
- )
- mode = 1
- potValue = 2
- #RELAYS
- GPIO.output(2, False)
- GPIO.output(3, False)
- GPIO.output(4, True)
- def checkBTConnection():
- try:
- client_sock.getpeername()
- print "CONN OK"
- return True
- except:
- print "CONN DOWN"
- return False
- #CALLBACKS (INTERRUPTS)
- relay2On = False
- relay3On = False
- relay4On = False
- def buttonPress():
- global relay2On
- global relay3On
- global relay4On
- relay2On = not relay2On
- GPIO.output(2, relay2On)
- print "BTN PRESS"
- print relay2On
- #GPIO.output(18, True)
- if checkBTConnection() == True:
- relay4On = not relay4On
- #GPIO.output(4, relay4On)
- if relay2On == True:
- client_sock.send("Button_ON")
- else:
- client_sock.send("Button_OFF")
- else:
- relay3On = not relay3On
- #GPIO.output(3, relay3On)
- def buttonPressCallback(channel):
- if GPIO.input(9) != 0:
- return False
- buttonPress()
- pot1Change = False
- pot2Change = False
- def upFunction():
- global potValue
- potValue = potValue + 1
- if checkBTConnection() == True:
- client_sock.send("Up_" + str(potValue))
- def downFunction():
- global potValue
- potValue = potValue - 1
- if checkBTConnection() == True:
- client_sock.send("Down_" + str(potValue))
- def pot1ChangeCallback(channel):
- #print "POT1"
- if relay2On == True:
- return False
- global pot1Change
- global pot2Change
- if pot2Change == True:
- print "Levo"
- downFunction()
- pot1Change = False
- pot2Change = False
- else:
- pot1Change = not pot1Change
- def pot2ChangeCallback(channel):
- #print "POT2"
- if relay2On == True:
- return False
- global pot1Change
- global pot2Change
- if pot1Change == True:
- print "Desno"
- upFunction()
- pot1Change = False
- pot2Change = False
- else:
- pot2Change = not pot2Change
- GPIO.add_event_detect(9, GPIO.FALLING, callback=buttonPressCallback, bouncetime = 300)
- GPIO.add_event_detect(8, GPIO.BOTH, callback=pot1ChangeCallback, bouncetime = 500)
- GPIO.add_event_detect(25, GPIO.BOTH, callback=pot2ChangeCallback, bouncetime = 500)
- while True:
- try:
- GPIO.output(23, False)
- GPIO.output(24, True)
- subprocess.call(['sudo','sh','/home/pi/Desktop/BluetoothPY/BTsetup.sh'])
- print "Cakam povezavo port %d" % port
- client_sock, client_info = server_sock.accept()
- print "Sprejeto od ", client_info
- #SEND STATUS
- statusString = "BUTTON_"
- if relay2On == True:
- statusString += "ON;"
- else:
- statusString += "OFF;"
- statusString += "POTVALUE_" + str(potValue)
- client_sock.send(statusString)
- GPIO.output(24, False)
- GPIO.output(23, True)
- pot1 = GPIO.input(8)
- pot2 = GPIO.input(25)
- ignoreChange = False
- while True:
- if mode == 1:
- data = client_sock.recv(1024)
- if len(data) == 0: break
- print "Podatki: "
- print data
- if data == "on":
- GPIO.output(18, True)
- client_sock.send("LED_ON")
- elif data == "off":
- GPIO.output(18, False)
- client_sock.send("LED_OFF")
- elif data == "Button":
- buttonPress()
- elif data == "Down":
- downFunction()
- elif data == "Up":
- upFunction()
- else:
- client_sock.send("Unknown")
- elif mode == 2:
- #OLD TESTING MODE
- #if GPIO.input(8) != pot1:
- # print "Rotate"
- # client_sock.send("Rotate")
- # pot1 = GPIO.input(8)
- # pot2 = GPIO.input(25)
- #if pot1 != GPIO.input(8):
- # if ignoreChange == False:
- # print 'Levo'
- # client_sock.send("Levo")
- # ignoreChange = True
- # else:
- # ignoreChange = False
- # pot1 = GPIO.input(8)
- #if pot2 != GPIO.input(25):
- # if ignoreChange == False:
- # print 'Desno'
- # client_sock.send("Desno")
- # ignoreChange = True
- # else:
- # ignoreChange = False
- # pot2 = GPIO.input(25)
- time.sleep(0.01)
- else:
- print "Unknown mode"
- except IOError:
- print "IOERROR"
- pass
- except KeyboardInterrupt:
- print "Konec"
- client_sock.close()
- server_sock.close()
- print "Cleanaup narjen"
- break
- #!bin/bin/sh
- sudo hciconfig hci0 name "Malinova potica"
- sudo hciconfig hci0 piscan
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement