-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlichtschranke.py
104 lines (92 loc) · 3.31 KB
/
lichtschranke.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
import ctypes
import serial
from serial.tools import list_ports
import sys
from flask import Flask, render_template, redirect, url_for
from flask_socketio import SocketIO, send, emit
from multiprocessing import Process, Value
import logging
app = Flask(__name__, static_folder='static')
socketio = SocketIO(app)
video_trigger = Value(ctypes.c_bool, False)
sensor_lock = Value(ctypes.c_bool, True)
class AjaxFilter1(logging.Filter):
def filter(self, record):
return "socket.io" not in record.getMessage()
class AjaxFilter2(logging.Filter):
def filter(self, record):
return "static" not in record.getMessage()
log = logging.getLogger('werkzeug')
log.addFilter(AjaxFilter1())
log.addFilter(AjaxFilter2())
def portscan():
for i in list_ports.comports():
print(f"Port {i} found")
return
def autoconnect():
for i in list_ports.comports():
try:
arduino = serial.Serial(port=f"{i.device}", baudrate=115200, timeout=.1)
print(f"Arduino connected on port {i.device}")
return arduino
except Exception as e:
print(f"Arduino not connected on port {i.device}: {str(e)}")
@app.route('/toggle_sensor')
def toggle_sensor():
sensor_lock.value = not sensor_lock.value
return redirect(url_for('control'))
def listener(video_trigger, sensor_lock, com_port):
try:
if (com_port == None): arduino = autoconnect()
else: arduino = serial.Serial(port=com_port, baudrate=115200, timeout=.1)
except Exception as e:
print(f"Arduino not connected on port {com_port}: {str(e)}")
sys.exit()
while True:
try:
message = arduino.readline().decode("utf-8")
except:
continue
if(message.replace("\n", "").replace("\r", "") == "Trigger"):
print("Trigger")
if sensor_lock.value: video_trigger.value = True
@app.route('/')
def index():
return render_template('index.html')
@app.route('/test')
def test():
return render_template('test.html')
@app.route('/control')
def control():
if sensor_lock.value: return render_template('control.html', sensor_state="Unlocked")
else: return render_template('control.html', sensor_state="Locked")
@socketio.on('request trigger')
def handle_request(data):
if video_trigger.value:
emit("trigger")
video_trigger.value = False
if __name__ == "__main__":
for arg in sys.argv:
if arg.startswith("COM") or arg.startswith('/dev'): com_port = sys.argv[sys.argv.index(arg)]
else: com_port = None
if arg == "portscan":
portscan()
sys.exit()
if arg == "help":
print("Usage: python3 lichtschranke.py [COMx] [portscan] [help]")
print(" COMx: COM port of Arduino")
print(" portscan: Scans for available COM ports")
print(" help: Shows this help")
print("")
print("Example: python3 lichtschranke.py COM3")
print("")
print("Drivers needed: https://www.silabs.com/developers/usb-to-uart-bridge-vcp-drivers")
sys.exit()
print(com_port)
p = Process(target=listener, args=(video_trigger, sensor_lock, com_port))
p.start()
try:
socketio.run(app, host='localhost', port=3000)
except:
sys.exit()
p.join()