Post your best Lua script examples here

User avatar
By jankop
#8233 I also use WPA2 and I connected over WAN too. The response is almost immediate, delay is seen in moments of reconnection to AP. ESP8266 is blocked tantalum and ceramic capacitors and my power is 12V/2A connected to Power Supply Module 3.3V/ 5V for breadboard with maximum current 700mA. The esp8266 works all day without rebooting.
User avatar
By kartman
#8377 I made a proper power supply for the esp board. It now seems to be much more stable. I should slap myself - I should've known better. As for the speed,it seems to be due to my tplink wireless adsl router. If I connect to my other router running openwrt, the performance is much better - not quite as snappy as I'd like, but adequate.
User avatar
By jankop
#8515 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

   PIN = 4 --  data pin, GPIO2
--load DHT module and read sensor
function ReadDHT()
   if sensorType=="dht11"then
   print("Humidity:    "..humi.."%")
   print("Temperature: "..temp.." deg C")
   print("Temperature: "..fare.." deg F")
   -- release module

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)
   --print(payload) -- for debugging only
   strstr={string.find(payload,"GET / HTTP/1.1")}
   --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\
    <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=""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=""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="'..sensorType..'.gif"WIDTH="200"HEIGHT="230"BORDER="2"></body></html>') end
    conn:on("sent",function(conn) conn:close() end)