-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.lua
42 lines (35 loc) · 1010 Bytes
/
application.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
dofile("counter.lua")
dofile("blinkDetector.lua")
local counter = Counter
local blinkDetector = BlinkDetector
function countBlinks()
local value = adc.read(0)
if blinkDetector:isBlinking(value) then
counter:increment()
end
end
function metrics()
local buffer = {"# TYPE watts gauge\n", "watts ", counter:get(), "\n"}
local body = table.concat(buffer)
counter:reset()
return body
end
function response()
local header = "HTTP/1.1 200 OK\r\nServer: NodeMCU on ESP8266\r\nContent-Type: text/plain; version=0.0.4\r\n\r\n"
local response = header .. metrics()
print("> " .. response)
return response
end
-- Initiate blink counting thread
tmr.create():alarm(10, tmr.ALARM_AUTO, countBlinks)
srv = net.createServer(net.TCP, 20) -- 20s timeout
if srv then
srv:listen(80, function(conn)
conn:on("receive", function(conn, data)
print("< " .. data)
conn:send(response(), function(finishConn)
finishConn:close()
end)
end)
end)
end