-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.lua
42 lines (30 loc) · 1.15 KB
/
web.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
local module = {}
function module.start()
wifi.setmode(wifi.STATION)
wifi.sta.config(config.SSID,config.WIFIPASS)
-- wifi.sta.connect()
tmr.delay(2000000) -- wait 1,000,000 us = 1 second
print(wifi.sta.status())
print(wifi.sta.getip())
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
tgtfile = string.sub(payload,string.find(payload,"GET /")
+5,string.find(payload,"HTTP/")-2)
responseText = ""
if(tgtfile == config.WEATHER_ENDPOINT) then
temp, hum = DHT.getTempHum()
responseText = "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\nCache-Control: private, no-store\r\n\r\n{\"temperature\": " .. temp .. ", \"humidity\":" .. hum .. "}"
end
if(responseText == "") then
responseText = "Not found 404"
end
conn:send(responseText)
conn:close()
collectgarbage();
responseText = nil
tgtfile = nil
end)
end)
end
return module