Professional Documents
Culture Documents
Hc-06 Raspberry Pi
Hc-06 Raspberry Pi
Hc-06 Raspberry Pi
py
# Author: BehindTheSciences.com
# Description: A simple BT server that accepts connection from a phone and controls
LEDs
#
server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)
port = server_sock.getsockname()[1]
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(16,GPIO.OUT)
GPIO.output(16,GPIO.LOW)
GPIO.setup(20,GPIO.OUT)
GPIO.output(20,GPIO.LOW)
GPIO.setup(21,GPIO.OUT)
GPIO.output(21,GPIO.LOW)
try:
while True:
data = client_sock.recv(1024)
if len(data) == 0: break
print("received [%s]" % data.decode("utf-8"))
if data == "a":
GPIO.output(16,GPIO.HIGH)
if data == "b":
GPIO.output(16,GPIO.LOW)
if data == "c":
GPIO.output(20,GPIO.HIGH)
if data == "d":
GPIO.output(20,GPIO.LOW)
if data == "e":
GPIO.output(21,GPIO.HIGH)
if data == "f":
GPIO.output(21,GPIO.LOW)
if data == "g":
arduino.write('A')
print('CLIMA ENCENDIDO')
if data == "h":
arduino.write('B')
print('CLIMA APAGADO')
if data == "i":
arduino.write('C')
print('TV ENCENDIDO')
if data == "j":
arduino.write('D')
print('TV APAGADA')
except IOError:
pass
print("Disconnected")
arduino.close()
client_sock.close()
server_sock.close()
print("All Closed")