1
0
mirror of https://github.com/simisimis/blinkmeter.git synced 2025-12-06 08:42:26 +02:00
Files
blinkmeter/application.lua
Simonas Narbutas 11928fa855 first commit
2018-11-28 15:50:45 +01:00

43 lines
1010 B
Lua

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