Skip to content

Instantly share code, notes, and snippets.

@jimbolimbo3
Created May 25, 2016 18:05
Show Gist options
  • Save jimbolimbo3/8310a2cf113315e0d0e8c102bb65e2bb to your computer and use it in GitHub Desktop.
Save jimbolimbo3/8310a2cf113315e0d0e8c102bb65e2bb to your computer and use it in GitHub Desktop.
init.lua for Temperature Sensor using DHT22, DS18B20 and NodeMCU
require('ds18b20')
dht=require("dht")
tmr.alarm(1, 5000, 1, function()
status,temp,humi,temp_decimial,humi_decimial = dht.read(2)
if( status == dht.OK ) then
--temperature = temp.."."..(temp_decimial) --/100
--humidity = humi.."."..(humi_decimial) --/100
--print("Temperature: "..temperature.." deg C")
--print("Humidity: "..humidity.."%")
print("Temperatura:" ..temp.." C")
print("Umidità: " ..humi.."%")
elseif( status == dht.ERROR_CHECKSUM ) then
print( "DHT Checksum error" )
temperature=-1 --TEST
elseif( status == dht.ERROR_TIMEOUT ) then
print( "DHT Time out" )
temperature=-2 --TEST
end
end)
port = 80
-- ESP-01 GPIO Mapping
gpio0, gpio2 = 3, 4
ds18b20.setup(gpio2)
srv=net.createServer(net.TCP)
srv:listen(port,
function(conn)
conn:send("HTTP/1.1 200 OK\nContent-Type: text/html\nRefresh: 5\n\n" ..
"<!DOCTYPE HTML>" ..
"<html><body>" ..
"<b>ESP8266</b></br>" ..
"Temperature : " .. temp .. "<br>" ..
"Humidity : " .. humi .. "<br>" ..
"External Temperature : " .. ds18b20.read() .. "<br>" ..
"Node ChipID : " .. node.chipid() .. "<br>" ..
"Node MAC : " .. wifi.sta.getmac() .. "<br>" ..
"Node Heap : " .. node.heap() .. "<br>" ..
"Timer Ticks : " .. tmr.now() .. "<br>" ..
"</html></body>")
conn:on("sent",function(conn) conn:close() end)
end
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment