-
Notifications
You must be signed in to change notification settings - Fork 1
/
webserver.lua
202 lines (198 loc) · 8.81 KB
/
webserver.lua
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
function sendWebPage(conn,answertype)
buf="HTTP/1.1 200 OK\nServer: NodeMCU\nContent-Type: text/html\n\n"
buf = buf .. "<html><body>\n"
buf = buf .. "<h1>Welcome to the WS2812-Ambi</h1>"
buf = buf.. "<h2>Configuration</h2><form action=\"\" method=\"POST\">"
buf = buf.. "<label for=\"ssid\">WIFI-SSID: <input id=\"ssid\" name=\"ssid\" value=\"" .. ssid .. "\"></label><br/>"
buf = buf.. "<label for=\"password\">Password: <input id=\"password\" name=\"password\"></label><br/>"
buf = buf.. "<label for=\"mqttserver\">MQTT Server: <input id=\"mqttserver\" name=\"mqttserver\" value=\"" .. mqttserver .. "\"></label><br/>"
buf = buf.. "<label for=\"mqttbasetopic\">MQTT Topic: <input id=\"mqttbasetopic\" name=\"mqttbasetopic\" value=\"" .. mqttbasetopic .. "\"></label><br/>"
buf = buf.. "<input type=\"submit\" value=\"Configure WS2812Ambi\"></form>"
if answertype>1 then
buf = buf .. "<h2>New configuration saved</h2\n>"
end
buf = buf .. "\n</body></html>"
conn:send(buf)
buf=nil
end
function startWebServer()
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive", function(conn,payload)
ssid, password, bssid_set, bssid = wifi.sta.getconfig()
if (payload:find("GET /switch=on") ~= nil) then
--here is code for handling http request from a web-browser
gpio.write(1,gpio.HIGH)
sendWebPage(conn,1)
conn:on("sent", function(conn) conn:close() end)
elseif (payload:find("GET /switch=off") ~= nil) then
gpio.write(1,gpio.LOW)
sendWebPage(conn,1)
conn:on("sent", function(conn) conn:close() end)
elseif (payload:find("GET /strip=red") ~= nil) then
--here is code for handling http request from a web-browser
ledbuffer:fill(0,255,0)
ws2812.write(ledbuffer)
sendWebPage(conn,1)
conn:on("sent", function(conn) conn:close() end)
tmr.stop(1)
tmr.unregister(1)
if (payload:find("blink=%d+") ~= nil) then
for i in string.gmatch(payload, "blink=(%d+)") do blinkblink(tonumber(i)) end
end
if (payload:find("duration=%d+") ~= nil) then
for i in string.gmatch(payload, "duration=(%d+)") do blink(tonumber(i)) end
end
elseif (payload:find("GET /strip=blue") ~= nil) then
--here is code for handling http request from a web-browser
ledbuffer:fill(0,0,255)
ws2812.write(ledbuffer)
sendWebPage(conn,1)
conn:on("sent", function(conn) conn:close() end)
tmr.stop(1)
tmr.unregister(1)
if (payload:find("blink=%d+") ~= nil) then
for i in string.gmatch(payload, "blink=(%d+)") do blinkblink(tonumber(i)) end
end
if (payload:find("duration=%d+") ~= nil) then
for i in string.gmatch(payload, "duration=(%d+)") do blink(tonumber(i)) end
end
elseif (payload:find("GET /strip=green") ~= nil) then
--here is code for handling http request from a web-browser
ledbuffer:fill(255,0,0)
ws2812.write(ledbuffer)
sendWebPage(conn,1)
conn:on("sent", function(conn) conn:close() end)
tmr.stop(1)
tmr.unregister(1)
if (payload:find("blink=%d+") ~= nil) then
for i in string.gmatch(payload, "blink=(%d+)") do blinkblink(tonumber(i)) end
end
if (payload:find("duration=%d+") ~= nil) then
for i in string.gmatch(payload, "duration=(%d+)") do blink(tonumber(i)) end
end
elseif (payload:find("GET /strip=white") ~= nil) then
--here is code for handling http request from a web-browser
ledbuffer:fill(255,255,255)
ws2812.write(ledbuffer)
sendWebPage(conn,1)
conn:on("sent", function(conn) conn:close() end)
tmr.stop(1)
tmr.unregister(1)
if (payload:find("blink=%d+") ~= nil) then
for i in string.gmatch(payload, "blink=(%d+)") do blinkblink(tonumber(i)) end
end
if (payload:find("duration=%d+") ~= nil) then
for i in string.gmatch(payload, "duration=(%d+)") do blink(tonumber(i)) end
end
elseif (payload:find("GET /strip=rgb") ~= nil) then
--here is code for handling http request from a web-browser
ledbuffer:replace(string.char(255,0,0,255,0,0,255,0,0,0,255,0,0,255,0,0,255,0,0,0,255,0,0,255,0,0,255):rep(33))
ws2812.write(ledbuffer)
sendWebPage(conn,1)
conn:on("sent", function(conn) conn:close() end)
tmr.stop(1)
tmr.unregister(1)
if (payload:find("blink=%d+") ~= nil) then
for i in string.gmatch(payload, "blink=(%d+)") do blinkblink(tonumber(i)) end
end
if (payload:find("duration=%d+") ~= nil) then
for i in string.gmatch(payload, "duration=(%d+)") do blink(tonumber(i)) end
end
elseif (payload:find("GET /strip=off") ~= nil) then
--here is code for handling http request from a web-browser
ledbuffer:fill(0,0,0)
ws2812.write(ledbuffer)
sendWebPage(conn,1)
conn:on("sent", function(conn) conn:close() end)
blinkblink(0)
tmr.stop(1)
tmr.unregister(1)
elseif (payload:find("GET /ledvalue?") ~= nil) then
_, datastart = payload:find("ledvalue?")
local _GET = {}
for i,gruen,rot,blau in string.gmatch(payload, "(%d+)=(%d+),(%d+),(%d+)") do
ledbuffer:set(tonumber(i),tonumber(gruen),tonumber(rot),tonumber(blau))
ws2812.write(ledbuffer)
end
sendWebPage(conn,1)
conn:on("sent", function(conn) conn:close() end)
tmr.stop(1)
tmr.unregister(1)
if (payload:find("blink=%d+") ~= nil) then
for i in string.gmatch(payload, "blink=(%d+)") do blinkblink(tonumber(i)) end
end
if (payload:find("duration=%d+") ~= nil) then
for i in string.gmatch(payload, "duration=(%d+)") do blink(tonumber(i)) end
end
elseif (payload:find("GET /ledrange?") ~= nil) then
_, datastart = payload:find("ledrange?")
ledbuffer:fill(0,0,0)
local _GET = {}
pointer = 1;
for i,gruen,rot,blau in string.gmatch(payload, "(%d+)=(%d+),(%d+),(%d+)") do
for j=pointer,tonumber(i),1 do
ledbuffer:set(j,tonumber(gruen),tonumber(rot),tonumber(blau))
end
pointer=tonumber(i)+1
ws2812.write(ledbuffer)
end
sendWebPage(conn,1)
conn:on("sent", function(conn) conn:close() end)
tmr.stop(1)
tmr.unregister(1)
if (payload:find("blink=%d+") ~= nil) then
for i in string.gmatch(payload, "blink=(%d+)") do blinkblink(tonumber(i)) end
end
if (payload:find("duration=%d+") ~= nil) then
for i in string.gmatch(payload, "duration=(%d+)") do blink(tonumber(i)) end
end
elseif (payload:find("GET /") ~= nil) then
sendWebPage(conn,1)
conn:on("sent", function(conn) conn:close() end)
else if (payload:find("POST /") ~=nil) then
--code for handling the POST-request (updating settings)
_, postdatastart = payload:find("\r\n\r\n")
--Next lines catches POST-requests without POST-data....
if postdatastart==nil then postdatastart = 1 end
postRequestData=string.sub(payload,postdatastart+1)
local _POST = {}
for i, j in string.gmatch(postRequestData, "(%w+)=([^&]+)&*") do
_POST[i] = j
end
postRequestData=nil
if ((_POST.ssid~=nil) and (_POST.password~=nil) and (_POST.mqttserver~=nil) and (_POST.mqttbasetopic~=nil)) then
tmr.stop(1)
mqtttopic,l=string.gsub(_POST.mqttbasetopic,"%%2F","/")
save_wifi_param(_POST.ssid,_POST.password,_POST.mqttserver,mqtttopic)
sendWebPage(conn,2)
else
ssid, password, bssid_set, bssid = wifi.sta.getconfig()
sendWebPage(conn,1)
conn:on("sent", function(conn) conn:close() end)
end
else
--here is code, if the connection is not from a webbrowser, i.e. telnet or nc
global_c=conn
function s_output(str)
if(global_c~=nil)
then global_c:send(str)
end
end
node.output(s_output, 0)
global_c:on("receive",function(c,l)
node.input(l)
end)
global_c:on("disconnection",function(c)
node.output(nil)
global_c=nil
end)
print("Welcome to WS2812Ambi CLI")
end
end
end)
conn:on("disconnection", function(c)
node.output(nil) -- un-register the redirect output function, output goes to serial
end)
end)
end