-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
47 lines (39 loc) · 1.21 KB
/
server.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
from flask import Flask, render_template
from flask_socketio import SocketIO
import dronekit
import sys
import socket
import threading
import time
import signal
socket.socket._bind = socket.socket.bind
def my_socket_bind(self, *args, **kwargs):
self.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
return socket.socket._bind(self, *args, **kwargs)
socket.socket.bind = my_socket_bind
# okay, now that that's done...
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
@app.route('/')
def index():
return render_template('index.html')
def pyr(vehicle):
while True:
time.sleep(.5)
atti = vehicle.attitude
if atti:
socketio.emit('pyr_status', {
"Pitch": atti.pitch,
"Yaw": atti.yaw,
"Roll": atti.roll,
})
else:
socket.emit('pyr_status', None)
if __name__ == '__main__':
target = sys.argv[1] if len(sys.argv) >= 2 else '/dev/ttyACM0'
print 'Connecting to ' + target + '...'
vehicle = dronekit.connect(target)
vehiclethread2 = threading.Thread(target=pyr, args=(vehicle,))
vehiclethread2.start()
socketio.run(app, host="0.0.0.0", port=8080)