Chat about current Lua tools and IDEs

User avatar
By TerryE
#16148 Hey Robo, I've just offered to help you as an incidental of my trying to contribute to the entire community. I come here to help people, not to get flamed. :(
User avatar
By TerryE
#16198 I'm getting brain ache!! I decided to avoid, Python, C, PHP, Perl, ... for the server / PC side and do everything in Lua so that anyone using this only needs to work in the one Language. My problem is that I need to do non-blocking I/O to both the USB UART and the inbound TCP request from the ESP to do the total-bootstrap. Easy in most languages, but unfortunately the core Lua implementation doesn't support this -- so I need to include the POSIX wrapper add-on. The documentation on this is pretty crap as are the examples, so its al lot of plowing through the lposix.c source in the extension to work out what the API calls really do :(

Still, I am getting there. It now takes me about 10sec to reimage my ESP application if its core is still functional -- or 60sec if it is broken and I need toI plug in the USB UART and bootstrap from a clean firmware level.
User avatar
By robo
#16411 as usual.. i have to help myself..
wasted my first day on lua and nodeMCU:
init.lua:
Code: Select allfunction SaveX(sErr)
    if (sErr) then
        print (sErr)
        s.err = sErr
    end
    file.remove("x")
    file.open("x","w+")
    for k, v in pairs(s) do
        file.writeline(k .. "=" .. v)
    end               
    file.close()
    collectgarbage()
end

--local iFail = 10
function wait_for_wifi_conn ( )
   tmr.alarm (1, 1000, 1, function ( )
--      iFail = iFail -1
--      if (iFail == 0) then
--        SaveX("could not access "..s.ssid)
--        node.restart()
--      end     
     
      if wifi.sta.getip ( ) == nil then
 --        print(iFail)
            print(s.ssid)
      else
         tmr.stop (1)
         print ("ip: " .. wifi.sta.getip ( ))
        sk=net.createConnection(net.TCP, 0)
        sk:on("receive", NewLua)
        sk:connect(80,s.host)
        sGet = "GET /".. s.path .. " HTTP/1.1\r\nHost: " .. s.domain .. "\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n"
        print(sGet)
        sk:send(sGet)
      end
      collectgarbage()
   end)
end

function NewLua(sck,c)
    local nStart, nEnd = string.find(c, "\n\n")
    if (nEnde == nil) then
        nStart, nEnd = string.find(c, "\r\n\r\n")
    end
    c = string.sub(c,nEnd+1)
    print("lua length: "..string.len(c))

    if (string.sub(c,0,5) ~= "--lua") then
        SaveX(s.domain.."/"..s.path .. " does not begin with --lua")
        node.restart()
        return
    end
    file.remove("do.lua")
    file.open("do.lua","w+")
    file.writeline(c)
    file.close()
    node.compile("do.lua")
    dofile("do.lua")
    dofile("do.lc")   
    collectgarbage()
end

s = {ssid="", pwd="", host="", domain="", path="", err=""}
if (file.open("x",r)) then
    local sF = file.read()
    print("setting: "..sF)
    file.close()
    for k, v in string.gmatch(sF, "([%w.]+)=([%S ]+)") do   
        s[k] = v
        print(k .. ": " .. v)
    end
end

if ((s.err == "") and s.host and s.domain and s.path) then
    wifi.setmode (wifi.STATION)
    wifi.sta.config(s.ssid, s.pwd)
    wifi.sta.autoconnect (1)
    wait_for_wifi_conn ( )
else
    wifi.setmode(wifi.SOFTAP)
    wifi.ap.config({ssid="node_"..node.chipid(), pwd=""})
    srv=net.createServer(net.TCP)
    print(wifi.sta.getip())
    srv:listen(80,function(conn)
        conn:on("receive", function(client,request)
           local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
           if (vars ~= nil)then
                for k, v in string.gmatch(vars, "(%w+)=([^&]*)&*") do
                    s[k],n = string.gsub(v,"%%2F","/")
                    print(k .. " = " .. s[k])
                end
                SaveX()
                if ((s.err == "") and s.host and s.domain and s.path) then
                    node.restart()
                end
            end

            do
                local buf =
"<html><body><form>" ..
"error: <input name=err value='"..s.err.."'/><br/>" ..
"<h2>access point:</h2>" ..
"SSID: <input name=ssid value='"..s.ssid.."'/><br/>" ..
"pwd: <input name=pwd value='"..s.pwd.."'/><br/>"
            client:send(buf);
            end
            do
                local buf =
"<h2>fetch lua:</h2>" ..
"ip: <input name=host value='"..s.host.."'/><br/>" ..
"domain: <input name=domain value='"..s.domain.."'/><br/>"
            client:send(buf);
            end

            do
                local buf =
"path: <input name=path value='"..s.path.."'/><br/>" ..
"<input type=submit value='submit'/></form></body></html>"
            client:send(buf);
            end

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


the code checks a setting file "x" and if fully populated it downloads for example

host=81.169.145.148
domain=robosoft.de
path=do.lua

if the download does not begin with "--lua", an error message is saved to "x" and after restart init.lua starts it's own access point to update "x"

i am totally new to lua and nodeMCU always gets "out of memory" :-(
had to remove the timeout error while trying to connect to access point :-(
i guess this is only a RAM issue and the RAM could be freed before executing the downloaded lua file :-/
and all wifi stuff is not really reliable :-/

the file should be split in 2 or 3 files anyway. So that the downloaded lua code could check "once a day" for a new file to download.

lua and nodeMCU is a mess :-/

don't know if i will have the happiness to waste more time on this.