Chat about current Lua tools and IDEs

User avatar
By TerryE
#16423 Robo, my first programming job was on a machine that had a team of 4 programmers, 6 operators, 12 punch girls (and yes in those days it was a job for women only) and a data prep department of 20 on a computer with 24Kbytes of RAM, 4 tape drives and no disk storage -- but it was used to run a largish manufacturing company. The first Intel 8008 and 8080 systems were becoming available with 4-8Kb so I was brought up working in tight memory systems. 5 years later, grads were being introduced to computing with VAX virtual memory systems where you could have Mb working sets, and later these guys had to work on the early Windows systems had strange 64 Kb limits which could cause you to die.

In the IoT world for the next few years we are again back in this tiny memory world. nodeMCU and Lua isn't a mess, IMO; I feel that you seem to be want to treat a $2 system as if it is $25 or $100 one.
  • break you program into phases or overlays, one per lua source file.
  • make sure that the context passed between each phase is as small as needed, and pass this using few global variables / tables.
  • Lua processing is trigger by events firing -- a timer alarm, an on() event, etc and runs to completion, at which point all of the dead stuff is reclaimed.
Once you get used to working this way then you should be able to achieve your goals. If you can't then perhaps you might achieve more with an Arduino or RPi type of approach.
User avatar
By robo
#16444 good morning.

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

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

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

            file.open("y.htm","r")
            local sH = file.read()
            for k, v in pairs(s) do
                sH,n = string.gsub(sH,"_"..k,v)
            end               
            print(sH)
            file.close()
            client:send(sH)
            client:close()
 
            collectgarbage()
        end)
    end)
end


have moved the html into a template:
Code: Select all<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/>
<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/>
<input type=submit value='submit'/>
</form></body></html>
 

the "x" setting file looks like this:
Code: Select allhost=81.169.145.148
path=do.lua
ssid=G3_1234
pwd=password
err=x
domain=robosoft.de

It is created when the form is posted. But always nice to add a "x" to err and let ESPlorer upload it to go back to access point mode

Terry you are a nice guy but you should take for granted that i am not.

collectgarbage("count") yields 11 kB. Don't know why i am so close to outOfMemory.

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 guess, the init.lua could have a timer to look for a new download "once a day" while the downloaded lua file is running :-)

I post the code here so someone with more expertise might be happy to NOT help me
but make the world a better place.
yet in this ego driven toy world.. everyone only works for himself..
User avatar
By TerryE
#16458 Robo, As I said I am doing my own framework and just google github terrye esp8266 to find it. As i said it's still apha because I am rewriting big chunks of it and I am not even sure that the last uploaded version works, but it will give you an idea of some of the approaches to use. I did download your code and started to pick it apart but there are too many beginners mistakes. I've got about 40 years of programming experience but this is my first Lua code and I am still picking up subtleties that I missed on on reading the manuals -- e.g. for variable are treated as semantically as having been declared as local within the for loop.

As to how I understand how the GC works etc, its just basic scientific method: use the data (e.g. the Lua source), formulate hypotheses and run experiments to see if the data supports them. My framework allows me to upload, download, compile, do basic file operation, execute remote commands (e.g. collect ds18b20 data), and even bootstrap a bare flashed lua system, with a runtime overhead of about 3.5Kb. So it can be done. I just suggest that you take a look and try to understand what I am doing.
User avatar
By robo
#16467 https://github.com/TerryE/ESP8266-harness/blob/master/host/ESP8266-host.lua

Well by now,i don't really want to replace the ESPlorer with a webIDE. It is great for developing / debugging. I have tried http://www.NodeLua.org and it is a great simple and plain webIDE where you can see the print("logging / debuggin") in your browser while the ESP is connected to the internet. Sadly the creator seems to have vanished from earth ?

When i will have a dozen ESPs all over the house and nearby friends, i will need a wifi distribution system to update the code. That is how i started this thread. No server web ide. Just ftp the latest update to the internet and all my ESPs will fetch them.
And if some ESPs run as access points, i would like to upload updates via the the simple "x"-settings form.

My electric bike is still controlled by a PIC, yet i moved to Arduino already some years ago, now moving from Arduino to ESP.
(and backing https://www.kickstarter.com/projects/70 ... for-things as well as https://www.kickstarter.com/projects/on ... nternet-of )

I think NodeMCU needs a very simple way to update/distribute code without any cables or IDE.

From what i see on github i have this feeling that you are building some telnet-style console to control the ESP via the internet. That would be the typical (stupid) linux-approach :-(

Roland,
alias Robo Durden
you either lose.. or fail !!!!!!!!!!!!!!!!!!!!!!!!!!