Hc-06 Raspberry Pi

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

# File: LEDControlServer.

py
# Author: BehindTheSciences.com
# Description: A simple BT server that accepts connection from a phone and controls
LEDs
#

from bluetooth import *


import RPi.GPIO as GPIO
import serial
arduino = serial.Serial('/dev/ttyUSB0' , 9600)

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)

advertise_service( server_sock, "BTS",


service_id = uuid,
service_classes = [ uuid, SERIAL_PORT_CLASS ],
profiles = [ SERIAL_PORT_PROFILE ],
)

print("Waiting for connection on RFCOMM channel %d" % port)

client_sock, client_info = server_sock.accept()


print("Accepted connection from ", client_info)

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")

You might also like