Load program fpmdht.lua and dht.lua to ESP8266 with LuaLoader
-- Demo http server for sensors DHT11/22
-- Tested with Lua NodeMCU 0.9.5 build 20150127 floating point !!!
-- 1. Flash Lua NodeMCU to ESP module.
-- 2. Set in program fpmdht.lua sensor type. This is parameter typeSensor="dht11" or "dht22".
-- 3. You can rename the program fpmdht.lua to init.lua
-- 4. Load program fpmdht.lua and dht.lua to ESP8266 with LuaLoader
-- 5. HW reset module
-- 6. Login module to your AP - wifi.setmode(wifi.STATION),wifi.sta.config("yourSSID","yourPASSWORD")
-- 7. Run program fpmdht.lua - dofile(fpmdht.lua)
-- 8. Test IP address - wifi.sta.getip()
-- 9. Test it with your browser and true IP address of module.
--10. The sensor is repeatedly read every minute.
--11. The pictures on page are external.
--12. The length of html code is limited to 1460 characters including header.
-- The author of the program module dht.lua for reading DHT sensor is Javier Yanez
--*******************************
sensorType="dht22" -- set sensor type dht11 or dht22
--*******************************
wifi.setmode(wifi.STATION)
--wifi.sta.config("yourSSID","yourPASSWORD")
wifi.sta.connect()
tmr.delay(1000000)
humi="XX"
temp="XX"
fare="XX"
count=1
PIN = 4 -- data pin, GPIO2
--load DHT module and read sensor
function ReadDHT()
dht=require("dht")
dht.read(PIN)
if sensorType=="dht11"then
humi=dht.getHumidity()/256
temp=dht.getTemperature()/256
else
humi=dht.getHumidity()/10
temp=dht.getTemperature()/10
end
fare=(temp*9/5+32)
print("Humidity: "..humi.."%")
print("Temperature: "..temp.." deg C")
print("Temperature: "..fare.." deg F")
-- release module
dht=nil
package.loaded["dht"]=nil
end
ReadDHT()
tmr.alarm(1,60000,1,function()ReadDHT()count=count+1 if count==5 then count=0 wifi.sta.connect()print("Reconnect")end end)
srv=net.createServer(net.TCP) srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
--print(payload) -- for debugging only
strstr={string.find(payload,"GET / HTTP/1.1")}
if(strstr[1]~=nil)then
--generates HTML web site
conn:send('HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n\
<!DOCTYPE HTML>\
<html><head><meta content="text/html;charset=utf-8"><title>ESP8266</title></head>\
<body bgcolor="#ffe4c4"><h2>Hygrometer with<br>'..sensorType..' sensor</h2>\
<h3><font color="green">\
<IMG SRC="http://esp8266.fancon.cz/common/hyg.gif"WIDTH="64"HEIGHT="64"><br>\
<input style="text-align: center"type="text"size=4 name="j"value="'..humi..'"> % of relative humidity<br><br>\
<IMG SRC="http://esp8266.fancon.cz/common/tmp.gif"WIDTH="64"HEIGHT="64"><br>\
<input style="text-align: center"type="text"size=4 name="p"value="'..temp..'"> Temperature grade C<br>\
<input style="text-align: center"type="text"size=4 name="p"value="'..fare..'"> Temperature grade F</font></h3>\
<IMG SRC="http://esp8266.fancon.cz/common/'..sensorType..'.gif"WIDTH="200"HEIGHT="230"BORDER="2"></body></html>') end
conn:on("sent",function(conn) conn:close() end)
end)
end)