Lua-script automat. Aktualisieren v. Meßwerten

  • StefanL38
  • StefanL38s Avatar Autor
  • Offline
  • Moderator
  • Moderator
  • Hobby-Elektroniker, Modellflieger
Mehr
03 Sep 2017 21:37 - 03 Sep 2017 21:38 #274 von StefanL38
StefanL38 erstellte das Thema Lua-script automat. Aktualisieren v. Meßwerten
Hallo,

bin gerade dabei ein Lua-script das Temperatur/Luftfeuchte-Meßwerte des Sensors DHT11 auf einer Internetseite ausgibt. Die Ausgabe erfolgt so, dass der Browser alle 2 Sekunden die Webseite neu vom ESP8266 lädt anstatt den Browser-Cache zu benutzen.
SSID = "YourSSID"
PW   = "YourPassword"
wifi.setmode(wifi.STATION)
wifi.sta.config(SSID,PW)
----------------------------------
-- WiFi Connection Verification --
----------------------------------
tmr.alarm(0, 1000, 1, function()
   if wifi.sta.getip() == nil then
      print("trying to connect to AccessPoint #"..SSID.."# \n")
   else
      ip, nm, gw=wifi.sta.getip()
      print("connected to AccessPoint: #"..SSID.."#")
      print("IP Info: \nIP Address: ",ip)
      print("Netmask: ",nm)
      print("Gateway Addr: ",gw,"\n")
      print("type IP-ADress "..wifi.sta.getip().." into your browser to display website")
      tmr.stop(0)  -- stop sending status-information to serial interface
      tmr.start(1) -- start timer that reads temperature and hunidity-values from DHT-Sensor
   end
end)


pin = 4
counter = 0
status, temp, humi, temp_dec, humi_dec = 0

local duration = 1000    -- 1000 milliseconds = 1 second duration for timer

tmr.register(1, duration, 1, function ()  -- register timer does what it says. The command tmr.start(1) makes the timer run

  status, temp, humi, temp_dec, humi_dec = dht.read(pin)
  if status == dht.OK then
      -- Integer firmware using this example
      print(string.format("DHT Temperature:%d.%03d;Humidity:%d.%03d\r\n",
            math.floor(temp),
            temp_dec,
            math.floor(humi),
            humi_dec
            ))
  
      -- Float firmware using this example
      print("DHT Temperature:"..temp..";".."Humidity:"..humi)
  
  elseif status == dht.ERROR_CHECKSUM then
      print( "DHT Checksum error." )
  elseif status == dht.ERROR_TIMEOUT then
      print( "DHT timed out." )
  end

end)

srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
    conn:on("receive", function(client,request)
        local buf = "";
        counter = counter + 1
        local MyInfoHeader = "DHT auto-update";
        local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
        if(method == nil)then
            _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
        end
        local _GET = {}
        if (vars ~= nil)then
            for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
                _GET[k] = v
            end
        end

        -- some html-info http-expires content=0 forces the browser to reload website instead of using cache-data
        -- buf = buf.."<head> <meta http-equiv=expires content=0> </head>"

        --buf = buf..<head> <meta http-equiv=refresh content=2; URL=http://192.168.178.31> </head>
        -- refresh means force browser reloading a website every x seconds 
        -- content=x defines x seconds  URL must be the IP-adress of your own ESP8266
        -- if the URL is the IP-adress of your own ESP8266 this means auto-re-load = autoupdate your ESP8266-website
        buf = buf.."<head> <meta http-equiv=refresh content=2; URL=http://192.168.178.31> </head>"
        buf = buf.."<h1> ESP8266 Web Server</h1>";
        buf = buf.."<h2>"..MyInfoHeader.." "..counter.."</h1>";
        buf = buf.."<p>DHT Temperature:"..temp.." ".."Humidity:"..humi.."</p>";

        client:send(buf);
        client:close();
        collectgarbage();
    end)
end)

Wenn etwas nicht funktionieren sollte bitte hier im Forum fragen.

viele Grüße

Stefan Ludwig
Letzte Änderung: 03 Sep 2017 21:38 von StefanL38.

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Moderatoren: StefanL38
Powered by Kunena Forum