Created
December 18, 2014 01:05
-
-
Save thenrich/052feebab3573c8e961a to your computer and use it in GitHub Desktop.
Python Bluetooth Gate Opener Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime, time, signal, sys | |
from bluetooth import * | |
import RPi.GPIO as GPIO | |
# Setup GPIO | |
PIN = 19 | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(PIN, GPIO.OUT, GPIO.PUD_DOWN, 0) | |
server_sock = BluetoothSocket(RFCOMM) | |
server_sock.bind(("", 10)) | |
server_sock.listen(1) | |
port = server_sock.getsockname()[1] | |
uuid = '00001101-0000-1000-8000-00805F9B34FB' | |
advertise_service(server_sock, 'Gate', service_id=uuid, service_classes=[uuid, SERIAL_PORT_CLASS], profiles=[SERIAL_PORT_PROFILE]) | |
def signal_handler(sig, frame): | |
# Cleanup any open pins | |
GPIO.cleanup() | |
# Bail | |
sys.exit(0) | |
def open_gate(): | |
print '{}: Opening gate'.format(str(datetime.datetime.utcnow())) | |
GPIO.output(PIN, 1) | |
time.sleep(2) | |
GPIO.output(PIN, 0) | |
actions = { | |
'OPEN_GATE': open_gate | |
} | |
def handle_conn(client): | |
data = client.recv(1024) | |
print data | |
if data in actions: | |
actions[data]() | |
if __name__ == '__main__': | |
signal.signal(signal.SIGINT, signal_handler) | |
while True: | |
print 'Waiting..' | |
client_sock, client_info = server_sock.accept() | |
print 'accepted..' | |
handle_conn(client_sock) | |
client_sock.send('OK') | |
client_sock.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment