This repository was archived by the owner on Oct 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebserver.lua
More file actions
157 lines (146 loc) · 4.62 KB
/
Webserver.lua
File metadata and controls
157 lines (146 loc) · 4.62 KB
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
function startWebserver()
srv = net.createServer(net.TCP, 10)
srv:listen(80, function(conn)
conn:on("receive", function(sck, payload)
local path = parseRequest(payload)
print(path)
local status = "200 OK";
local contenttype = "text/html; charset=UTF-8"
local response = nil;
local cache = true
if (path == "/") then
response = getMainPage()
elseif (path == "/hotspot-detect.html") then --captive portal (ios)
response = getMainPage()
elseif (path == "/json") then
contenttype = "application/json"
response = getJson()
elseif (path == "/common.css") then
contenttype = "text/css"
response = loadFile("common.css")
elseif (path == "/script.js") then
contenttype = "text/javascript"
response = loadFile("script.js")
else
local commandresponse = executeCommand(path)
response = '{ "status": "' .. commandresponse .. '" }'
if (commandresponse == 'invalid request') then
print("404 - " .. path)
status = "404 Not Found";
else
cache = false
end
end
local header = { }
header[#header + 1] = 'HTTP/1.1 ' .. status
header[#header + 1] = 'Content-Length: ' .. string.len(response)
if (cache == false) then
header[#header + 1] = 'Cache-Control: no-cache, no-store, must-revalidate'
header[#header + 1] = 'Pragma: no-cache'
header[#header + 1] = 'Expires: 0'
end
header[#header + 1] = 'Connection: close'
header[#header + 1] = 'Content-Type: ' .. contenttype
sck:send(table.concat(header," \r\n") .. "\r\n\r\n" .. response)
end)
end)
end
function loadFile(staticfile)
file.open(staticfile)
local chunk = file.read(1024)
file.close()
return chunk
end
function getMainPage()
local t = { }
t[#t + 1] = '<html>'
t[#t + 1] = '<head>'
t[#t + 1] = '<title>Bath Control</title>'
t[#t + 1] = '<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">'
t[#t + 1] = '<link rel="stylesheet" href="common.css">'
t[#t + 1] = '</head>'
t[#t + 1] = '<body>'
t[#t + 1] = '<h1>Bath Control</h1>'
t[#t + 1] = '<table class="primary">'
t[#t + 1] = '<tr>'
t[#t + 1] = '<td>' .. dhthum .. '</td><td>' .. dhttemp .. '</td>'
t[#t + 1] = '</tr><tr>'
t[#t + 1] = '<th>Humidity</th><th>Temperature</th>'
t[#t + 1] = '</tr>'
t[#t + 1] = '</table>'
t[#t + 1] = '<table>'
t[#t + 1] = '<tr>'
t[#t + 1] = '<th width="140">Sensor State</th><td>' .. sensorState .. '</td>'
t[#t + 1] = '</tr><tr>'
t[#t + 1] = '<th>Uptime</th><td>' .. rtctime.get() .. 's</td>'
t[#t + 1] = '</tr><tr>'
t[#t + 1] = '<th>Treshold</th><td>' .. logichumtreshold .. '</td>'
t[#t + 1] = '</tr><tr>'
t[#t + 1] = '<th>Hysteresis</th><td>' .. logichumhysteresis .. '</td>'
t[#t + 1] = '</tr><tr>'
t[#t + 1] = '<th>Fan State</th><td>' .. (fanState == 1 and 'on' or 'off') .. '</td>'
t[#t + 1] = '</tr>'
t[#t + 1] = '</table>'
t[#t + 1] = '<button onclick="command(\'fan/on\')">fanon</button>'
t[#t + 1] = '<button onclick="command(\'fan/off\')">fanoff</button>'
t[#t + 1] = '<script src="script.js"></script>'
t[#t + 1] = '</body></html>'
return table.concat(t, "\n")
end
function getJson()
local t = { }
t[#t + 1] = '{'
t[#t + 1] = '"humidity": ' .. dhthum .. ','
t[#t + 1] = '"temperature": ' .. dhttemp .. ','
t[#t + 1] = '"sensorState": "' .. sensorState .. '",'
t[#t + 1] = '"uptime": ' .. rtctime.get() .. ','
t[#t + 1] = '"treshold": ' .. logichumtreshold .. ','
t[#t + 1] = '"hysteresis": ' .. logichumhysteresis .. ','
t[#t + 1] = '"fanActive": ' .. (fanState == 1 and 'true' or 'false') .. ','
t[#t + 1] = '"heap": ' .. node.heap()
t[#t + 1] = '}'
return table.concat(t, "\n")
end
function parseRequest(payload)
local getLength = 4
local indexOfGet = string.find(payload, 'GET', 1)
if (indexOfGet == nil)
then
return nil
end
local indexOfHttp = string.find(payload, 'HTTP', indexOfGet + getLength)
if (indexOfHttp == nil)
then
return nil
end
return string.sub(payload, indexOfGet + getLength, indexOfHttp - 1 - indexOfGet)
end
function executeCommand(path)
for p1, p2, p3 in string.gmatch(path, "(%w+)/(%w+)/(%w+)") do
if (p1 == "command") then
if (p2 == "fan") then
if (p3 == "on") then
if (setFan(1)) then
return 'fan started'
end
return 'fan cannot start'
elseif (p3 == "off") then
if (setFan(0)) then
return 'fan stopped'
end
return 'fan cannot stop'
end
end
elseif (p1 == "wlan") then
setWifi(p2, p3)
print(p2 .. "-" .. p3)
return 'accept change'
elseif (p1 == "config") then
logichumtreshold = tonumber(p2)
logichumhysteresis = tonumber(p3)
return 'accept change'
end
return 'unknown command'
end
return 'invalid request'
end