2019-12-31 00:53:54 +01:00
|
|
|
local t = require('ds18b20')
|
2015-01-10 10:58:08 +01:00
|
|
|
|
2019-12-31 00:53:54 +01:00
|
|
|
local port = 80
|
|
|
|
local pin = 3 -- gpio0 = 3, gpio2 = 4
|
|
|
|
local gconn = {} -- local variable for connection
|
2015-01-10 10:58:08 +01:00
|
|
|
|
2019-12-31 00:53:54 +01:00
|
|
|
local function readout(temps)
|
2017-02-25 22:56:45 +01:00
|
|
|
local resp = "HTTP/1.1 200 OK\nContent-Type: text/html\nRefresh: 5\n\n" ..
|
2019-12-31 00:53:54 +01:00
|
|
|
"<!DOCTYPE HTML>" ..
|
|
|
|
"<html><body>" ..
|
|
|
|
"<b>ESP8266</b></br>"
|
2017-03-10 22:10:49 +01:00
|
|
|
|
2019-12-31 00:53:54 +01:00
|
|
|
for addr, temp in pairs(temps) do
|
2022-12-14 08:31:39 +01:00
|
|
|
resp = resp .. string.format("Sensor %s: %s ℃</br>",
|
2019-12-31 00:53:54 +01:00
|
|
|
('%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X '):format(addr:byte(1,8)), temp)
|
2017-02-25 22:56:45 +01:00
|
|
|
end
|
2017-03-10 22:10:49 +01:00
|
|
|
|
2017-02-25 22:56:45 +01:00
|
|
|
resp = resp ..
|
2019-12-31 00:53:54 +01:00
|
|
|
"Node ChipID: " .. node.chipid() .. "<br>" ..
|
|
|
|
"Node MAC: " .. wifi.sta.getmac() .. "<br>" ..
|
|
|
|
"Node Heap: " .. node.heap() .. "<br>" ..
|
|
|
|
"Timer Ticks: " .. tmr.now() .. "<br>" ..
|
|
|
|
"</html></body>"
|
2015-01-10 10:58:08 +01:00
|
|
|
|
2017-02-25 22:56:45 +01:00
|
|
|
gconn:send(resp)
|
|
|
|
gconn:on("sent",function(conn) conn:close() end)
|
|
|
|
end
|
2015-01-10 10:58:08 +01:00
|
|
|
|
2019-12-31 00:53:54 +01:00
|
|
|
do
|
|
|
|
local srv = net.createServer(net.TCP)
|
|
|
|
srv:listen(port,
|
|
|
|
function(conn)
|
|
|
|
gconn = conn
|
|
|
|
-- t:read_temp(readout) -- default pin value is 3
|
|
|
|
t:read_temp(readout, pin)
|
|
|
|
end)
|
|
|
|
end
|