Post your best Lua script examples here

User avatar
By manoelps
#28735 [English]

I found an example of how to set up the ESP via browser, put SSID and PASSWORD and then it displays the IP address that was assigned (link below), but is occurring errors when I record in the ESP-01, so that concludes recording displays error timeout and starts displaying the error below:

NodeMCU 0.9.5 build 20150318 powered by Moon 5.1.4
bÊÊf¾ø? jrüóÆ? ªÄrF! úAÊöø

NodeMCU 0.9.5 build 20150318 powered by Moon 5.1.4
7! A! G, ÿ? EB "û? Ev0] It?) ³O

NodeMCU 0.9.5 build 20150318 powered by Moon 5.1.4
"ʃ
? þ: 6 ®? ùAÂH | ?? ³O

Someone managed to use the script?

I realized that a bug occurs in this part of the script:

sv = net.createServer (net.tcp, 30)
sv: listen (9999, function (c)
c: on ("receive", function (c, pl)

http://www.instructables.com/id/ESP8266 ... -purpos-1/


-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

[Português]

Configurar o ESP8266 via navegador.

Encontrei o exemplo de como configurar o ESP via browser, colocar SSID e SENHA e depois ele exibir o endereço IP que foi atribuido (link abaixo), mas está ocorrendo erros, quando gravo no ESP-01, assim que conclui a gravação apresenta erro de Timeout e começa a exibir o erro abaixo:

NodeMCU 0.9.5 build 20150318 powered by Lua 5.1.4
bÊÊf¾øjrüóƪÄrF!úAÊöø

NodeMCU 0.9.5 build 20150318 powered by Lua 5.1.4
7!a!ˆ G,ÿÈB”ûÈV0]ÿ)³ø

NodeMCU 0.9.5 build 20150318 powered by Lua 5.1.4
"ʃ
þ:6® ùAÂH|  ³ø

Alguém já conseguiu usar o script?

Percebi que ocorre erro ocorre nesta parte do script:

sv=net.createServer(net.TCP, 30)
sv:listen(9999,function(c)
c:on("receive", function(c, pl)

http://www.instructables.com/id/ESP8266 ... -purpos-1/
User avatar
By Ripper121
#29022 init.lua
Code: Select allssid = "MySSID"
password  = "12345678"
psw  = ""
timeout=0

if pcall(function ()
   dofile("config.lc")
end) then
   print("Connecting to WIFI...")
   realtype = wifi.sleeptype(wifi.MODEM_SLEEP)
   wifi.setmode(wifi.STATION)
   wifi.sta.config(ssid,password)
   wifi.sta.connect()

   tmr.alarm(1, 1000, 1, function()
      if wifi.sta.getip() == nil then       
         print("IP unavaiable, waiting... " .. timeout)
      timeout = timeout + 1
      if timeout >= 60 then
       file.remove('config.lc')
       node.restart()
      end
      else
         tmr.stop(1)
         print("Connected, IP is "..wifi.sta.getip())
         dofile("run_program.lua")
      end
   end)
else
   print("Enter configuration mode")
   dofile("run_config.lua")
end


run_config.lua
Starts a html server for config.
List all AP´s in range and password input
Code: Select allprint("Get available APs")
wifi.setmode(wifi.STATION)
wifi.sta.getap(function(t)
   available_aps = ""
   if t then
      for k,v in pairs(t) do
         ap = string.format("%-10s",k)
         ap = trim(ap)
         available_aps = available_aps .. "<option value='".. ap .."'>".. ap .."</option>"
      end
      setup_server(available_aps)
   end
end)

local unescape = function (s)
   s = string.gsub(s, "+", " ")
   s = string.gsub(s, "%%(%x%x)", function (h)
         return string.char(tonumber(h, 16))
      end)
   return s
end

function setup_server(aps)
   print("Setting up Wifi AP")
   wifi.setmode(wifi.SOFTAP)
  cfg = {}
   cfg.ssid = ssid
   --cfg.pwd  = psw
   wifi.ap.config(cfg)     
  wifi.ap.setip({ip="192.168.1.1",netmask="255.255.255.0",gateway="192.168.1.1"})
   print("Setting up webserver")
   srv = nil
   srv=net.createServer(net.TCP)
   srv: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+)=([^%&]+)&*") do
                   _GET[k] = unescape(v)
               end
           end
             
           if (_GET.psw ~= nil and _GET.ap ~= nil and _GET.ipTCP ~= nil) then
              client:send("Saving data..")
              file.open("config.lua", "w")
                file.writeline('ssid = "' .. _GET.ap .. '"')
                file.writeline('password = "' .. _GET.psw .. '"')
            file.writeline('ipTCP = "' .. _GET.ipTCP .. '"')
                 file.close()
                 node.compile("config.lua")
                 file.remove("config.lua")
                 client:send(buf)
                node.restart()
           end
   
           buf = "<html><body>"
          buf = buf .. "<h3>Config</h3><br>"
           buf = buf .. "<form method='get' action='http://" .. wifi.ap.getip() .."'>"
            buf = buf .. "Select access point: <select name='ap'>" .. aps .. "</select><br>"         
            buf = buf .. "Enter wifi password: <input type='password' name='psw'></input><br>"
          buf = buf .. "Server-IP: <input name='ipTCP' value='192.168.178.1'></input><br>"
          buf = buf .. "<br><button type='submit'>Save</button>"                   
            buf = buf .. "</form></body></html>"
           client:send(buf)
           client:close()
           collectgarbage()
       end)
   end)
   print("Please connect to: " .. wifi.ap.getip())
end

function trim(s)
  return (s:gsub("^%s*(.-)%s*$", "%1"))
end



run_program.lua
Here you can setup your programm which will run after connection to wifi is ready
Code: Select all
file.remove('config.lc')

sk=net.createConnection(net.TCP, 0)
 sk:on("receive", function(sck, c) uart.write(0,c) end )
 sk:connect(1234,ipTCP)

uart.on("data", 1,
      function(data)
        sk:send(data)       
    end, 0)
User avatar
By manoelps
#29079 Ripper121 ...
Thanks for the feedback!

I Efetuei tests with the script, it worked \ o / ... but only carrying the AP list .. after configured began to display the message below:

PANIC: unprotected error in call to Lua API (not enough memory), and is restarting

Studying the code for testing ...
User avatar
By forlotto
#29309 Hrmmmm I wonder if it would be easier just to update the data on the nodemcu such as the password and ssid via a web browser somehow have the data to where it is easily replaceable so if you took it somewhere to show off your project you could just update the info without having to reprogram the thing basically the same idea more or less but may be able to be accomplished with less code IDK for sure but I like this idea anyone can just ask for or scan the ssid's available with their phone and simply ask for the password. Update the info stored and viola. Well either way I do like the idea. There is a slight problem though this thing has way to little memory/space. Can't wait for a more robust version of the esp8266 if it does not exist already.