Chat about current Lua tools and IDEs

User avatar
By cal
#16625
admin wrote:This board was created by me for a place for people to share ideas, and share their work, if you do not like the work, then walk away. I do not have time to read all the posts and see who I think is right and who is wrong. I am not into banning, just please people play nice :-)

Richard.


Thank you Richard for providing this forum!
May i beg you to fix the template issued of posts that make "by" and name one word?
Many thanks!
User avatar
By robo
#16628 i guess i should put nodeMCU into the trashbin..

Today i tried to upload lua.code via the the settings form:

Image

But when lua file gets bigger then about 300 bytes nodeMCU always crashes for some unknown reasons.. Everything with nodeMCU/ESPLorer is so unreliable :-/

Here the latest 1-file-installer:
Code: Select all--file:install.lua

print("install ESPLoad")

function WriteFile()
    print("add file: " .. sFile .. ": " .. string.len(sData))
    file.close()

    file.remove(sFile)
    file.open(sFile,"w+")
    file.write(sData)
    file.close()

    file.open("install.lua","r")
    file.seek("set",iSeek)
   
    sData = ""
    collectgarbage()
end


file.open("install.lua","r")

sData = ""
sFile = nil
iSeek=0

s=file.readline()
while s ~= nil do
--    if sFile then
--        print("'"..s.."'")
--    end
    iSeek = iSeek + string.len(s)
    sNewFile = string.match(s, "^--file:([%w.]+)")
    if sNewFile then
        --print(sNewFile)
        if sFile then
            WriteFile(sFile,sData)
        end
        sFile = sNewFile
    else
        if sFile then
            sData = sData .. s
        end
    end
    s=file.readline()
end
WriteFile(sFile,sData)

file.close()
file.remove("install.lua")
print("install finished :-)")

--[[

--file:start.lua

function LoadX()
    s = {ssid="", pwd="", host="", domain="", path="", err="no data",update=0,upload=""}
    if (file.open("s.txt","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
end

print(collectgarbage("count").." kB used")
LoadX()

if s.err == "" then
    if (tonumber(s.update)>0) then
        tmr.alarm (0, tonumber(s.update)*60000, 1, function()
                print("checking for update")
            end)
    end
    dofile("client.lua")   
else
    dofile("server.lua")   
end

--file:server.lua

function unescape(s)
    local h = {}
    for x in string.gmatch(s, "%%(%x%x)") do
        h[x] = 1
    end
    for x, v in pairs(h) do
        s,n = string.gsub(s,"%%"..x,string.char(tonumber(x, 16)))
    end               
    s,n = string.gsub(s,"+"," ")
    return s
end

function SaveX(sErr)
    if (sErr) then
        print (sErr)
        s.err = sErr
    end
    file.remove("s.txt")
    file.open("s.txt","w+")
    for k, v in pairs(s) do
        file.writeline(k .. "=" .. v)
    end               
    file.close()
    collectgarbage()
end


print(collectgarbage("count").." kB: server.lua")
LoadX()


wifi.setmode(wifi.SOFTAP)
wifi.ap.config({ssid="node_"..node.chipid(), pwd=""})
srv=net.createServer(net.TCP)
print(wifi.sta.getip())

srv:listen(1985,function(conn)
    conn:on("receive", function(client,request)
       print("1985..")
       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

            if s.upload ~= "" then
                s.upload = unescape(s.upload)
                print(string.len(s.upload))
                sF = string.match(s.upload, "^--file:([%w.]+)")
                if sF then
                    print("write " .. sF)
                    file.remove(sF)
                    file.open(sF,"w+")
                    for i = 0,string.len(s.upload),10 do
                        file.write(string.sub(s.upload,i,i+9))
                        collectgarbage()
                    end
                    file.close()
                    s.path = sF
                    s.err = sF..": "..string.len(s.upload) .. " bytes :-)"
                else
                    s.err = "no --file:do.lua"
                end
            end
        end

        file.open("y.htm","r")
        local sH = file.read()
        for k, v in pairs(s) do
            sH,n = string.gsub(sH,"_"..k,unescape(v))
        end               

        s.upload = ""
        if (vars ~= nil)then
            SaveX()
            if s.err == "" then
                node.restart()
            end
        end   
        print(string.len(sH))
        file.close()
        client:send(sH)
        client:close()

        collectgarbage()
       print("1985 done")
    end)
end)
if s.path ~= "" then
    if file.open(s.path,"r") then
        dofile(s.path)
    end
end

--file:y.htm
<html><body><form>
error: <input name=err value='_err'/><br/>
<h2>access point:</h2>
SSID: <input name=ssid value='_ssid'/><br/>
pwd: <input name=pwd value='_pwd'/><br/>
upload:<br/>
<textarea style="font-size:8pt;" cols=80 rows=10 name=upload>_upload</textarea>
<h2>fetch lua:</h2>
ip: <input name=host value='_host'/><br/>
domain: <input name=domain value='_domain'/><br/>
path: <input name=path value='_path'/><br/>
reboot: <input type=number size=4 name=update value='_update'/> minutes<br/>
<input type=submit value='submit'/>
</form></body></html>
 

--file:client.lua

function SaveX(sErr)
    if (sErr) then
        print (sErr)
        s.err = sErr
    end
    file.remove("s.txt")
    file.open("s.txt","w+")
    for k, v in pairs(s) do
        file.writeline(k .. "=" .. v)
    end               
    file.close()
    collectgarbage()
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,7) ~= "--file:") 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

print("fetch lua..")
LoadX()

wifi.setmode (wifi.STATION)
wifi.sta.config(s.ssid, s.pwd)
wifi.sta.autoconnect (1)

iFail = 20
tmr.alarm (1, 1000, 1, function ( )
  iFail = iFail -1
  print(iFail)
  if (iFail == 0) then
    SaveX("could not access "..s.ssid)
    node.restart()
  end     
 
  if wifi.sta.getip ( ) == nil then
    print(s.ssid)
  else
    print ("ip: " .. wifi.sta.getip ( ))
    tmr.stop (1)
    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)

 print(collectgarbage("count").." kB: client.lua")
--]]



also made a make.lua that makes the install.lua :-)
Code: Select all--file:make.lua
sFiles = "install.lu,start.lua,server.lua,y.htm,client.lua"

file.remove("install.lua")

for sF in string.gfind(sFiles, "([%w.]+)") do
    if file.open(sF,"r") then
        print("adding " .. sF)
        local sData = file.read()
        file.close()
       
        file.open("install.lua","a+")
        file.writeline(sData)
        file.close()
    else
        print("missing file: "..sF)
    end
end

print("done :-)")


make.lua needs the template
Code: Select all--file:install.lua

print("install ESPLoad")

function WriteFile()
    print("add file: " .. sFile .. ": " .. string.len(sData))
    file.close()

    file.remove(sFile)
    file.open(sFile,"w+")
    file.write(sData)
    file.close()

    file.open("install.lua","r")
    file.seek("set",iSeek)
   
    sData = ""
    collectgarbage()
end


file.open("install.lua","r")

sData = ""
sFile = nil
iSeek=0

s=file.readline()
while s ~= nil do
--    if sFile then
--        print("'"..s.."'")
--    end
    iSeek = iSeek + string.len(s)
    sNewFile = string.match(s, "file:([%w.]+)")
    if sNewFile then
        --print(sNewFile)
        if sFile then
            WriteFile(sFile,sData)
        end
        sFile = sNewFile
    else
        if sFile then
            sData = sData .. s
        end
    end
    s=file.readline()
end
WriteFile(sFile,sData)

file.close()
file.remove("install.lua")
print("install finished :-)")

--[[


this buggy nodeMCU / ESPLorer is nothing for me.
I have worked with PIC and Arduino, grew up with the C64, am a physicist, have worked with all languages (but fortran), have invented and implemented 3 programming languages myself and sorry to say, it is either nodeMCU or ESPlorer or the ESP8266 that sucks :'(

and not me :-)

"failures always fail to lose" - Robo Durden

ideas welcome
Attachments
ESPLoad form.png
User avatar
By robo
#16630 these files are working:

Code: Select all--install.lua
print("install ESPLoad")

function WriteFile()
    print("add file: " .. sFile .. ": " .. string.len(sData))
    file.close()

    file.remove(sFile)
    file.open(sFile,"w+")
    file.write(sData)
    file.close()

    file.open("install.lua","r")
    file.seek("set",iSeek)
   
    sData = ""
    collectgarbage()
end


file.open("install.lua","r")

sData = ""
sFile = nil
iSeek=0

s=file.readline()
while s ~= nil do
--    if sFile then
--        print("'"..s.."'")
--    end
    iSeek = iSeek + string.len(s)
    sNewFile = string.match(s, "^--file:([%w.]+)")
    if sNewFile then
        --print(sNewFile)
        if sFile then
            WriteFile(sFile,sData)
        end
        sFile = sNewFile
    else
        if sFile then
            sData = sData .. s
        end
    end
    s=file.readline()
end
WriteFile(sFile,sData)

file.close()
file.remove("install.lua")
print("install finished :-)")

--[[

--file:start.lua

function LoadX()
    s = {ssid="", pwd="", host="", domain="", path="", err="no data",update=0,upload=""}
    if (file.open("s.txt","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
end

print(collectgarbage("count").." kB used")
LoadX()

if s.err == "" then
    if (tonumber(s.update)>0) then
        tmr.alarm (0, tonumber(s.update)*60000, 1, function()
                print("checking for update")
            end)
    end
    dofile("client.lua")   
else
    dofile("server.lua")   
end

--file:server.lua

function unescape(s)
    local h = {}
    for x in string.gmatch(s, "%%(%x%x)") do
        h[x] = 1
    end
    for x, v in pairs(h) do
        s,n = string.gsub(s,"%%"..x,string.char(tonumber(x, 16)))
    end               
    s,n = string.gsub(s,"+"," ")
    return s
end

function SaveX(sErr)
    if (sErr) then
        print (sErr)
        s.err = sErr
    end
    file.remove("s.txt")
    file.open("s.txt","w+")
    for k, v in pairs(s) do
        file.writeline(k .. "=" .. v)
    end               
    file.close()
    collectgarbage()
end


print(collectgarbage("count").." kB: server.lua")
LoadX()


wifi.setmode(wifi.SOFTAP)
wifi.ap.config({ssid="node_"..node.chipid(), pwd=""})
srv=net.createServer(net.TCP)
print(wifi.sta.getip())

srv:listen(1985,function(conn)
    conn:on("receive", function(client,request)
       print("1985..")
       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

            if s.upload ~= "" then
                s.upload = unescape(s.upload)
                print(string.len(s.upload))
                sF = string.match(s.upload, "^--file:([%w.]+)")
                if sF then
                    print("write " .. sF)
                    file.remove(sF)
                    file.open(sF,"w+")
                    for i = 0,string.len(s.upload),10 do
                        file.write(string.sub(s.upload,i,i+9))
                        collectgarbage()
                    end
                    file.close()
                    s.path = sF
                    s.err = sF..": "..string.len(s.upload) .. " bytes :-)"
                else
                    s.err = "no --file:do.lua"
                end
            end
        end

        file.open("y.htm","r")
        local sH = file.read()
        for k, v in pairs(s) do
            sH,n = string.gsub(sH,"_"..k,unescape(v))
        end               

        s.upload = ""
        if (vars ~= nil)then
            SaveX()
            if s.err == "" then
                node.restart()
            end
        end   
        print(string.len(sH))
        file.close()
        client:send(sH)
        client:close()

        collectgarbage()
       print("1985 done")
    end)
end)
if s.path ~= "" then
    if file.open(s.path,"r") then
        dofile(s.path)
    end
end

--file:y.htm
<html><body><form>
error: <input name=err value='_err'/><br/>
<h2>access point:</h2>
SSID: <input name=ssid value='_ssid'/><br/>
pwd: <input name=pwd value='_pwd'/><br/>
upload:<br/>
<textarea style="font-size:8pt;" cols=80 rows=10 name=upload>_upload</textarea>
<h2>fetch lua:</h2>
ip: <input name=host value='_host'/><br/>
domain: <input name=domain value='_domain'/><br/>
path: <input name=path value='_path'/><br/>
reboot: <input type=number size=4 name=update value='_update'/> minutes<br/>
<input type=submit value='submit'/>
</form></body></html>
 

--file:client.lua

function SaveX(sErr)
    if (sErr) then
        print (sErr)
        s.err = sErr
    end
    file.remove("s.txt")
    file.open("s.txt","w+")
    for k, v in pairs(s) do
        file.writeline(k .. "=" .. v)
    end               
    file.close()
    collectgarbage()
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,7) ~= "--file:") 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

print("fetch lua..")
LoadX()

wifi.setmode (wifi.STATION)
wifi.sta.config(s.ssid, s.pwd)
wifi.sta.autoconnect (1)

iFail = 20
tmr.alarm (1, 1000, 1, function ( )
  iFail = iFail -1
  print(iFail)
  if (iFail == 0) then
    SaveX("could not access "..s.ssid)
    node.restart()
  end     
 
  if wifi.sta.getip ( ) == nil then
    print(s.ssid)
  else
    print ("ip: " .. wifi.sta.getip ( ))
    tmr.stop (1)
    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)

 print(collectgarbage("count").." kB: client.lua")
--]]


Code: Select all--install.lu
print("install ESPLoad")

function WriteFile()
    print("add file: " .. sFile .. ": " .. string.len(sData))
    file.close()

    file.remove(sFile)
    file.open(sFile,"w+")
    file.write(sData)
    file.close()

    file.open("install.lua","r")
    file.seek("set",iSeek)
   
    sData = ""
    collectgarbage()
end


file.open("install.lua","r")

sData = ""
sFile = nil
iSeek=0

s=file.readline()
while s ~= nil do
--    if sFile then
--        print("'"..s.."'")
--    end
    iSeek = iSeek + string.len(s)
    sNewFile = string.match(s, "file:([%w.]+)")
    if sNewFile then
        --print(sNewFile)
        if sFile then
            WriteFile(sFile,sData)
        end
        sFile = sNewFile
    else
        if sFile then
            sData = sData .. s
        end
    end
    s=file.readline()
end
WriteFile(sFile,sData)

file.close()
file.remove("install.lua")
print("install finished :-)")

--[[
User avatar
By enaon
#18625
robo wrote:good morning.

Yesterday it took me hours to flash the firmeware (NodeMCU 0.9.5 build 20150318) and get the ESPlorer running :-(

I wrongly used the ssid+pwd of my smartphone hotspot (LG G3) on my ESP-201 in the first time.
And even so my G3 is turned off, i have flashed the 201 several times and there is no such ssid in the lua code any longer, the 201 still and only shows up with the ssid+pwd of my smartphone - what a messy behavior :-(


I like your approach on serving files to esp without an ide and so on, but I get the feeling you are a bit on a hurry.

The ssid/pwd settings are kept in eeprom, no strange behavior here, it's only you being hasty.