Post your best Lua script examples here
User avatar
By jankop
#24406 Image
Demo server for I2C barometric sensor BMP180 or BMP085. Driver bmp180.lua is here and core srvbmp180.lua is here .
New lua versions coming applicable!
I like this flasher.


Code: Select all--      Demo http server for sensors BMP180/BMP085
--    Tested with Lua NodeMCU 0.9.6 build 20150704 floating point only!
-- 1. Flash Lua NodeMCU 0.9.6 build 20150704 floating point to esp8266 module.
-- 2. Login esp8266 module to your AP - wifi.setmode(wifi.STATION),wifi.sta.config("yourSSID","yourPASSWORD")
-- 3. You can rename the program "srvbmp180.lua" to "init.lua" and correct altitude
-- 4. Load program "bmp180.lua" and "srvbmp180.lua" to ESP8266 with a LuaLoader
-- 5. HW reset module
-- 6. Read ip address of module esp8266 from console "= wifi.sta.getip()"
-- 7. Run program "srvbmp180.lua" - dofile("srvbmp180.lua")
-- 8. Test it with your browser and true IP address of module.
-- 9. The sensor is repeatedly reading every 10s
--11. Data on http page are auto refreshed every 10s too.
--12. The pictures on page are external.

--**************************************************************
   alti=170 -- set correction for your altitude location in meters
--**************************************************************

   wifi.setmode(wifi.STATION)
   --wifi.sta.config("yourSSID","yourPASSWORD")
   wifi.sta.connect()
   sda=4   -- GPIO2 connect to SDA BMP180
   scl=3   -- GPIO0 connect to SCL BMP180
   pres=0    -- pressure
   temp=0    -- temperature
   fare=0  -- temperature
   oss=0   -- over sampling setting

--load BMP module for reading sensor
function ReadBMP()
   bmp180 = require("bmp180")
   bmp180.init(sda, scl)
   bmp180.read(oss)
   pres = bmp180.getPressure()/100+alti/8.43
   temp = bmp180.getTemperature()/10
   fare=(temp*9/5+32)
   print("Pressure:    "..string.format("%.1f",pres).." hPa")
   print("Temperature: "..string.format("%.1f",temp).." deg C")
   print("Temperature: "..string.format("%.1f",fare).." deg F")
   print(" ")
   -- release module
   bmp180 = nil
   package.loaded["bmp180"]=nil
end
-- First reading data
ReadBMP()
-- Periodic reading of the sensor
tmr.alarm(1,10000,1, function()ReadBMP()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>\
      <meta http-equiv="refresh" content="10;url=/"></head>\
      <body bgcolor="#ffe4c4"><h2>Barometer with I2C sensor<br>BMP180 or BMP85</h2>\
      <h3><font color="green">\
      <IMG SRC="http://esp8266.fancon.cz/bmp180/bar.gif"WIDTH="64"HEIGHT="64"><br>\
      <input style="text-align: center"type="text"size=6 name="j"value="'..string.format("%.1f",pres)..'"> Atmos. pressure hPa<br><br>\
      <IMG SRC="http://esp8266.fancon.cz/bmp180/tmp.gif"WIDTH="64"HEIGHT="64"><br>\
      <input style="text-align: center"type="text"size=4 name="p"value="'..string.format("%.1f",temp)..'"> Temperature grade C<br>\
      <input style="text-align: center"type="text"size=4 name="s"value="'..string.format("%.1f",fare)..'"> Temperature grade F</font></h3>\
      <h6>Tested with Lua NodeMCU<br> 0.9.6 build 20150704 float!</h6>')
      end
    conn:on("sent",function(conn) conn:close() end)
   end)
end)
User avatar
By SRA
#37005 Congratulations.
Cute done nice work
thanks
Cunva can send this data to a site and you can see orunde if I connect to the site?
I can do something like online thermostat that is to start and stop relays directly from a site?
If so and I would like to know how to do it
You see that you do interesting things but I can not see how to put them on a site that is host to reach a www data from anywhere in the world you can see and activate or Arduino Pin esp if the connected voucher go :)
Local go play
Once again, congratulations and Happy Holidays with your loved ones