Post your best Lua script examples here

User avatar
By CaqKa
#32392 Thank you Janko, that works for me
working on dht 11 and nodemcu devkit 0.9


Would be realy great if this version would be put on th top :)

jankop wrote:Previous program does not work correctly in floating point. That's why I edited it for floating point arithmetic. For proper operation, it is necessary to set in program fpmdht.lua sensor type. This is parameter typeSensor = "dht11" or "dht22".

Load program fpmdht.lua and dht.lua to ESP8266 with LuaLoader


Code: Select all--    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)
User avatar
By foxt
#32934 I have a devkip 0.9, running nodemcu 0.9.6 20150704 build (floating point version), and can't get the sensor to report correctly. There is a consistent checksum error when testing with wots.lua:

dofile("wots.lua")
checksum 183
checksumTest 192
humidity - timing of bits 0 1 1 1 5 4 5 1 1 1 1 1 1 1 1 1
temperat - timing of bits 1 1 1 1 1 5 5 1 4 2 5 1 4 5 1 1
checksum - timing of bits 5 1 4 5 1 10 10 10
Humidity: 0%
Temperature: 170.8 deg C

When I run fpmdht.lua (with sensor type correctly set to dht22), I get the following error:

dofile("fpmdht.lua")
fpmdht.lua:37: attempt to call field 'getHumidity' (a nil value)

I do have dht.lua installed.

Any suggestions for either problem?
User avatar
By foxt
#32946 On the error related to the dht.lua call, I renamed dht.lua to dht1.lua, changed the call from fpmdht, and it now executes - must be a dht function already included in my firmware build?

The data coming back is still wrong, so I need to focus on the bad results from the testing code - the checksum error is the clue, what would cause that?
User avatar
By foxt
#32986 Progress report: I decided to try running with nodemcu 0.9.5, and I now get the test script to return the correct temperature. However, when running either fpmdht.lua or monDHT22.lua, the values returned for temp and humidity vary from those returned by wots.lua. I tried this with both integer and floating point versions of 0.9.5 - same result; wots.lua returns what seems like reasonable values for temp and humidty, and the other two scripts return 0C for temp and -1.9% for humidity.

Why would that be?