Post your best Lua script examples here

User avatar
By zhivko
#11979 Is it possible to fetch a text that user entered in textbox to lua variable?
I have 2 esp8266, one with relay and another with temperature sensor. I would like to enter temperature when relay triggers - and I want to this data to be captured from http web server page.
Is this possible somehow?
Attachments
IMG_20150311_222226.jpg
IMG_20150311_222241.jpg
User avatar
By zhivko
#12006
zhivko wrote:Is it possible to fetch a text that user entered in textbox to lua variable?
I have 2 esp8266, one with relay and another with temperature sensor. I would like to enter temperature when relay triggers - and I want to this data to be captured from http web server page.
Is this possible somehow?

Seems to me that clever heads already did it with following code (taken from http://randomnerdtutorials.com/esp8266-web-server/) - with following lua code:
Code: Select allsrv:listen(80,function(conn)
    conn:on("receive", function(client,request)
        local buf = "";
        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
User avatar
By Gadgit
#12017 Just to give my experience for anyone also trying this.

Not yet exactly sure why but none of these different examples worked for me except Jankop's viewtopic.php?p=11189#p11189

I'm on NodeMCU 0.9.5 build 20150213 using a DHT11 on this ESP8266 SDK board: http://scottsnowden.co.uk/?p=393

I think it is something to do with the timing, but also the lines which interpret the bitStream are different in Jankop's version:

Code: Select all 
for i = 1, 16, 1 do
   if (bitStream[i] > 3) then
      humidity = humidity + 2 ^ (16 - i)
   end
    if (bitStream[i + 16] > 3) then
      temperature = temperature + 2 ^ (16 - i)
   end
  end


which works vs:

Code: Select all  --DHT data acquired, process.
  for i = 1, 16, 1 do
    if (bitStream[i + 0] > 2) then
      humidity = humidity + 2 ^ (16 - i)
    end
    if (bitStream[i + 16] > 2) then
      temperature = temperature + 2 ^ (16 - i)
    end
  end


which doesn't. The only difference I can see is that Jankop starts 1 bit later. I can't understand why one works for some people and not for others