← Back to ratslair.com
aboutsummaryrefslogtreecommitdiff
path: root/main.py
blob: 27e5e52b01f1ba618c9d26801a8eabd6f571b748 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import RPi.GPIO as GPIO
from time import sleep

import os
import glob
import bluetooth

import re

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
GPIO.output(17, False)
GPIO.output(27, False)

target = "D4:D2:52:ED:14:89"

server_sock = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
server_sock.bind(("", bluetooth.PORT_ANY))
server_sock.listen(1)

port = server_sock.getsockname()[1]

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

client_sock, client_info = server_sock.accept()

while client_sock:
    read = client_sock.recv(1024)
    read = read.decode("utf-8").strip('\n')
    print(read)

    if read == "11":
        GPIO.output(17, True)
        GPIO.output(27, False)
    elif read == "22":
        GPIO.output(17, False)
        GPIO.output(27, True)
    else:
        GPIO.output(17, False)
        GPIO.output(27, False)
pi@pigroup3:~/TNE107-RPI$ cat main.py 
import RPi.GPIO as GPIO
from time import sleep

import os
import glob
import bluetooth

import re

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
GPIO.output(17, False)
GPIO.output(27, False)

target = "D4:D2:52:ED:14:89"

server_sock = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
server_sock.bind(("", bluetooth.PORT_ANY))
server_sock.listen(1)

port = server_sock.getsockname()[1]

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

client_sock, client_info = server_sock.accept()

while client_sock:
    read = client_sock.recv(1024)
    read = read.decode("utf-8").strip('\n')
    print(read)

    if read == "11":
        GPIO.output(17, True)
        GPIO.output(27, False)
    elif read == "22":
        GPIO.output(17, False)
        GPIO.output(27, True)
    else:
        GPIO.output(17, False)
        GPIO.output(27, False)

GPIO.output(17, False)
GPIO.output(27, False)

print("Shutting down...")
server_sock.close()
print("Done.")