-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAlaWeb.py
178 lines (140 loc) · 4.15 KB
/
AlaWeb.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
from flask import Flask, render_template, request
import serial, serial.tools.list_ports
import sys
import time
import socket
app = Flask(__name__)
arduino = None
currLevel = 0.4
currColor = ""
currAnim = ""
currPal = ""
@app.route("/")
def home():
templateData = {}
return render_template('main.html', **templateData);
@app.route("/brightness/", methods=['POST'])
def brightness():
btn_name = get_btn_name(request)
print ("Brightness:", btn_name )
arduino_send_cmd("B="+str(btn_name))
templateData = {}
return render_template('main.html', **templateData);
@app.route("/duration/", methods=['POST'])
def duration():
duration = request.form['duration']
print ("Duration:", duration)
arduino_send_cmd("D="+str(duration))
templateData = {}
return render_template('main.html', **templateData);
@app.route("/color/", methods=['POST'])
def color():
btn_name = get_btn_name(request)
print ("Color:", btn_name )
arduino_send_cmd("C="+str(btn_name))
templateData = {}
return render_template('main.html', **templateData);
@app.route("/palette/", methods=['POST'])
def palette():
global currLevel
global currColor
global currAnim
global currPal
btn_name = get_btn_name(request)
print ("Palette:", btn_name)
arduino_send_cmd("P="+str(btn_name))
templateData = {}
return render_template('main.html', **templateData);
@app.route("/anim/", methods=['POST'])
def anim():
btn_name = get_btn_name(request)
print ("Animation code:", btn_name)
arduino_send_cmd("A="+str(btn_name))
templateData = {}
return render_template('main.html', **templateData);
@app.route("/pal/", methods=['POST'])
def pal():
global currLevel
global currColor
global currAnim
global currPal
if 'btnRgb' in request.form:
arduino_send_cmd("a");
currPal = "RGB";
elif 'btnRainbow' in request.form:
arduino_send_cmd("b");
currPal = "Rainbow";
elif 'btnParty' in request.form:
arduino_send_cmd("d");
currPal = "Party";
elif 'btnFire' in request.form:
arduino_send_cmd("f");
currPal = "Fire";
templateData = {
'currAnim': currAnim,
'currPal': currPal,
'currLevel': currLevel,
'currColor': currColor
};
return render_template('main.html', **templateData);
def get_btn_name(request):
btn_name=""
for key in request.form.keys():
#print ("Button pressed:", key)
btn_name = key
return btn_name
def arduino_get_resp(s):
time.sleep(.1);
while (s.in_waiting > 0):
print(s.readline().decode(), end="");
def arduino_send_cmd(s):
arduino.flush();
s = s+'\n'
arduino.write(s.encode());
arduino_get_resp(arduino);
time.sleep(.1);
arduino.flush()
# try to detect the USB port where Arduino is connected
def arduino_get_port():
print("Listing ports")
port = None
ports = serial.tools.list_ports.comports()
for p in ports:
print(p)
if "Arduino" in p[1]:
port = p[0]
print("Arduino detected on port", port)
return port
def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# doesn't even have to be reachable
s.connect(('10.255.255.255', 1))
ip = s.getsockname()[0]
except:
ip = '127.0.0.1'
finally:
s.close()
return ip
if __name__ == "__main__":
port = None
# use the USB port name if passed
if len(sys.argv)>1:
port = sys.argv[1]
print("Arduino port: " + port)
# otherwise tries to detect the port
# this seems to work only on Windows if Arduino USB driver is installed
while(port==None):
port = arduino_get_port()
if port==None:
print("Arduino not found. Retrying...")
time.sleep(5);
# open the serial interface
arduino = serial.Serial(port, 9600, timeout=1)
time.sleep(.5);
print("Port", arduino)
print("Current IP is", get_ip())
print("Point your browser to http://", get_ip(), sep="")
print()
#app.run(host='0.0.0.0', port=80, debug=True)
app.run(host='0.0.0.0', port=80)