-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBTserver.py
177 lines (149 loc) · 4.32 KB
/
BTserver.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import bluetooth
import RPi.GPIO as GPIO
import os
import subprocess
from subprocess import Popen
import asyncio
import select
import time
GPIO.setmode(GPIO.BOARD) #setup
GPIO.setwarnings(False)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
fwd = GPIO.PWM(13, 100)
bck = GPIO.PWM(11, 100)
left = GPIO.PWM(18, 100)
right = GPIO.PWM(16, 100)
client_sock = None
server_sock = None
timeout = 2
def startCamera():
subprocess.Popen(['./mjpg-streamer.sh start'], shell=True, cwd="/home/pi/mjpg-streamer")
def stopCamera():
subprocess.Popen(['./mjpg-streamer.sh stop'], shell=True, cwd="/home/pi/mjpg-streamer")
def sayNo():
left.start(100)
time.sleep(0.2)
left.stop()
right.start(100)
time.sleep(0.2)
right.stop()
left.start(100)
time.sleep(0.2)
left.stop()
right.start(100)
time.sleep(0.2)
right.stop()
def surpriseMe():
fwd.start(100)
time.sleep(1)
fwd.stop()
bck.start(100)
time.sleep(0.3)
bck.stop()
fwd.start(100)
time.sleep(0.3)
fwd.stop()
bck.start(100)
time.sleep(0.3)
bck.stop()
bck.start(100)
time.sleep(0.3)
bck.stop()
fwd.start(100)
time.sleep(0.3)
fwd.stop()
bck.start(100)
time.sleep(0.3)
bck.stop()
sayNo()
sayNo()
def sayYes():
fwd.start(100)
time.sleep(0.5)
fwd.stop()
bck.start(100)
time.sleep(0.5)
bck.stop()
def lookAround():
fwd.start(100)
left.start(100)
time.sleep(3)
fwd.stop()
left.stop()
def restart():
fwd.stop()
bck.stop()
left.stop()
right.stop()
startCamera()
server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
server_sock.bind(("", bluetooth.PORT_ANY))
server_sock.listen(1)
port = server_sock.getsockname()[1]
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
bluetooth.advertise_service(server_sock, "SampleServer", service_id=uuid,
service_classes=[uuid, bluetooth.SERIAL_PORT_CLASS],
profiles=[bluetooth.SERIAL_PORT_PROFILE],
)
print("Waiting for connection on RFCOMM channel", port)
client_sock, client_info = server_sock.accept()
print("Accepted connection from", client_info)
try:
client_sock.setblocking(0)
while True:
ready = select.select([client_sock], [], [], 2)
if ready[0]:
data = client_sock.recv(1024)
if not data:
GPIO.cleanup()
break
if data:
if data == 'F'.encode(): #up start
fwd.start(100)
elif data == 'f'.encode():
fwd.stop()
elif data == 'B'.encode(): #down start
bck.start(100)
elif data == 'b'.encode():
bck.stop()
elif data == 'R'.encode():
right.start(100)
elif data == 'r'.encode(): #right stop
right.stop()
elif data == 'L'.encode():
left.start(100)
elif data == 'l'.encode():
left.stop()
elif data == 'No'.encode(): #say no
sayNo()
elif data == 'Sr'.encode(): #Surprise me
surpriseMe()
elif data == 'LA'.encode(): #Look around
lookAround()
elif data == 'Yes'.encode(): # Spin
sayYes()
print("Received", data)
else:
fwd.stop()
bck.stop()
right.stop()
left.stop()
except bluetooth.btcommon.BluetoothError:
print("Bluetooth connection lost!. Reloading program...")
client_sock.close()
server_sock.close()
restart()
try:
restart()
except(KeyboardInterrupt):
print ('Finishing up')
GPIO.cleanup()
stopCamera()
if not(server_sock is None):
server_sock.close()
if not(client_sock is None):
client_sock.close()
quit()