Current Lua downloadable firmware will be posted here

User avatar
By Agentsmithers
#62519
Monday, February 13, 2017 12:07:30


Text upload: C:\Users\User\Dropbox\Programming\ESP8266\LUALoader\version 0.91\init.lua

> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
9805 bytes uploaded to init.lua

> = node.heap()
43280
> node.restart()
> ü! ÿ ¤„ÿ1ä)Mô1ä)=ÜŽÿ‚,>þþ
Communications Error - Check baud rate

€)!J„¶D”Œ 19Å
M¥ 0nÅ!Fÿ·ëÕ½{u{wýÉÑ¡)
>
dofile(init.lua) Monday, February 13, 2017 12:16:08

dofile("init.lua")
E:M 2056
not enough memory
stack traceback:
[C]: in function 'dofile'
stdin:1: in main chunk
>


Everyone,
I have the Wemos D1MiniPro at 16MB of memory. I have been trying to upload my script that is at 11,220 Bytes (12,288 on disk).
Anyone know why this is a limitation of the Wemos / ESP8266. I have tried multiple Chips and each one gives off this error. Thank you for your feedback. My assumtion is regardless of my Chip being handle to handle 16 Megabytes that it is perhaps the firmware that is only allocating X Amount?
I am using a firmware build from NodeMCU's cloud service.

Code: Select all--Shrink with https://mothereff.in/lua-minifier
--dofile 'includes.lua'
uart.setup(0, 9600, 8, 0, 1, 1)
print("Init Loading")
-- configure for 9600, 8N1, with echo
-- Add Host Mode
gpio0 =3
gpio2 =4
gpio.mode(gpio2,gpio.OUTPUT)
gpio.write(gpio2,gpio.LOW)

local GateWayIP = "192.168.2.1"
local UARTstatus = nil

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 StartHostAP()
   wifi.setmode(wifi.STATIONAP)
   local cfg={}
   local str=wifi.ap.getmac();
   local ssidTemp=string.format("%s%s%s",string.sub(str,10,11),string.sub(str,13,14),string.sub(str,16,17));
   cfg.ssid="ESP8266_"..ssidTemp;
   cfg.pwd="12345678"
   
   UARTstatus = "ESP8266_"..ssidTemp.."|PW: 12345678"
   
   wifi.ap.config(cfg)
   cfg={}
   cfg.ip=GateWayIP;
   cfg.netmask="255.255.255.0";
   cfg.gateway=GateWayIP;
   wifi.ap.setip(cfg);

   StartDNS()
end

function StartDNS()
   s=net.createServer(net.UDP)
   s:on("receive",function(s,c)

   transaction_id=string.sub(c,1,2)
   flags=string.sub(c,3,4)
   questions=string.sub(c,5,6)

   query = ""
   raw_query = ""
   j=13
   while true do
      byte = string.sub(c,j,j)
      j=j+1
      raw_query = raw_query .. byte
      if byte:byte(1)==0x00 then --NULL marks end of the string.
         break
      end
      for i=1,byte:byte(1) do
         byte = string.sub(c,j,j)
         j=j+1
         raw_query = raw_query .. byte
         query = query .. byte
      end
      query = query .. '.'
   end
   query=query:sub(1,query:len()-1) --strip the trailing dot.
   q_type = string.sub(c,j,j+1)
   j=j+2
   if q_type == unhex("00 01") then
   --print("Got a type A query "..query)
   class = string.sub(c,j,j+1)

   --ip = "192.168.2.1"
   ip=unhex("C0 A8 02 01")
   answers = unhex("00 01")
   flags = unhex("81 80")

   resp=transaction_id..flags..questions..answers..unhex("00 00")..unhex("00 00")..raw_query..q_type..class
   resp=resp..unhex("c0 0c")..q_type..class..unhex("00 00 00 da")..unhex("00 04")..ip
   s:send(resp)
   end
   end)
   s:on("sent",function(s)
   --s:close()
   end)
   s:listen(53)
   print("listening, free:", node.heap())
end

prodconn = nil
prodsrv=net.createServer(net.TCP)
function ProdWebServer()
   dofile 'ProdWebServer.lua'
   prodsrv:listen(80,function(prodconn)
    prodconn:on("receive", function(client,request)
        local buf = "";
      local buf2 = "";
        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 = {}
      
      buf = buf.."HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n";
      
        if (vars ~= nil)then
            for k, v in string.gmatch(vars, "(%w+)=(.+)&*") do
         --for k, v in string.gmatch(vars, "([%w+)=([%w%%]+)&*") do
                _GET[k] = v
            print("\r\nGot -" .. v)
            end
      elseif (vars == nil)then
         buf = displayWebpage()
        end
      
      if(_GET.pin == "ON1")then
         gpio.write(gpio0, gpio.HIGH);
      elseif(_GET.pin == "OFF1")then
         gpio.write(gpio0, gpio.LOW);
      elseif(_GET.pin == "ON2")then
         gpio.write(gpio2, gpio.HIGH);
      elseif(_GET.pin == "OFF2")then
         gpio.write(gpio2, gpio.LOW);
      elseif(_GET.cmd == "resetwifi")then
         file.remove("config.lc")
         node.restart()
      elseif(_GET.cmd == "reboot")then
         node.restart()
      elseif(_GET.cmdtx ~= nil)then
         uart.write(0, _GET.cmdtx)
         --print("Unescaped " .. unescape(_GET.cmdtx))
      elseif(_GET.cmdtxcr ~= nil)then
         uart.write(0, _GET.cmdtxcr.."\r")
         --print("Unescaped " .. unescape(_GET.cmdtxcr).."\r")
      end

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

function Join()
   print ("joining SSID")
   print(wifi.sta.getip())
   --nil
   wifi.setmode(wifi.STATION)
   wifi.sta.config(ssid, password)
   wifi.sta.connect()
   tmr.alarm(2, 1000, 1, function()
       if wifi.sta.getip() == nil then
          UARTstatus = "Connecting..."
       else
          tmr.stop(2)
          UARTstatus = "Connected, IP IS|" .. wifi.sta.getip()
       end
   end)
   ProdWebServer()
end

function StartConfigWebServer()
   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
      end
   end)

   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.ap ~= nil and _GET.ipTCP ~= nil) then --_GET.psw ~= nil  The password can be NULL
            client:send("Saving data..")
            file.open("config.lua", "w")
            file.writeline('ssid = "' .. _GET.ap .. '"')
            if (_GET.HostAP ~= nil) then
               file.writeline('HostAP = "true"')
            else
               file.writeline('HostAP = "false"')
            end   
            if (_GET.psw ~= nil) then
               file.writeline('password = "' .. _GET.psw .. '"')
            else
               file.writeline('password = ""')
            end            
            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'>" .. available_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.2.1'></input><br>"
         buf = buf .. "<input type='checkbox' name='HostAP' value='true'>Act as AP<br>"
         buf = buf .. "<br><button type='submit'>Save</button>"                   
         buf = buf .. "</form></body></html>"
         client:send(buf)
         client:close()
         collectgarbage()
      end)
   UARTstatus = "Please connect to:|" .. wifi.ap.getip()
   end)
end

function ConnStatus(n)
   status = wifi.sta.status()
   uart.write(0,' '..status)
   local x = n+1
   if (x < 50) and ( status < 5 ) then
      tmr.alarm(3,100,0,function() ConnStatus(x) end)
   else
      if status == 5 then
      print('\nConnected as '..wifi.sta.getip())
      --dofile('relaywebserver.lua')
      else
      print("\nConnection failed")
      end
   end
end
















   
function best_ssid(ap_db)
   local min = 100
   ssid = nil
   for k,v in pairs(ap_db) do
       if tonumber(v) < min then
          min = tonumber(v)
          ssid = k
          end
       end
       
   --gpio.write(gpio2,gpio.LOW)
   --gpio.write(gpio2,gpio.HIGH)
   return min
end

function strongest(aplist)
   print("\nAvailable Open Access Points:\n")
   for k,v in pairs(aplist) do print(k..' '..v) end
   
   ap_db = {}
   if next(aplist) then
      for k,v in pairs(aplist) do
         if '0' == string.sub(v,1,1) then
            ap_db[k] = string.match(v, '-(%d+),')
            end
          end
      signal = -best_ssid(ap_db)
      end
   if ssid then
      print("\nBest SSID: ".. ssid)
      wifi.sta.config(ssid,"")
      print("\nConnecting to "..ssid)
      ConnStatus(0)
   else
      print("\nNo available open APs")
      ssid = ''
      end
end

function unhex(str)
    str = string.gsub (str, "(%x%x) ?",
        function(h) return string.char(tonumber(h,16)) end)
    return str
end

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

function hex_dump (str)
    local len = string.len( str )
    local dump = ""
    local hex = ""
    local asc = ""
   
    for i = 1, len do
        if 1 == i % 8 then
            dump = dump .. hex .. asc .. "\n"
            hex = string.format( "%04x: ", i - 1 )
            asc = ""
        end
       
        local ord = string.byte( str, i )
        hex = hex .. string.format( "%02x ", ord )
        if ord >= 32 and ord <= 126 then
            asc = asc .. string.char( ord )
        else
            asc = asc .. "."
        end
    end

   
    return dump .. hex
            .. string.rep( "   ", 8 - len % 8 ) .. asc
end

-- when '\r' is received.
uart.on("data", "\r",
   function(data)
      data=trim (data)
        print("receive from uart:", data)
      print(hex_dump(data))
        if data=="quit()" then
         print("got quit")
         uart.on("data")
      elseif data=="scan()" then
         print("Scanning")
         UARTstatus = "scanning"
         wifi.setmode(wifi.STATION) wifi.sta.getap(function(t) if t then print("\n\nVisible Access Points:\n") for k,v in pairs(t) do l = string.format("%-10s",k) print(l.."  "..v) end else print("Try again") end  end)
      elseif data=="join()" then
         Join()
      elseif data=="strongest()" then
         wifi.setmode(wifi.STATION)
         wifi.sta.getap(function(t) strongest(t)  end)
      elseif data=="resetwifi()" then
         file.remove("config.lc")
         node.restart()
      elseif data=="config()" then
         StartHostAP()
         StartConfigWebServer()
      elseif data=="dns()" then
         StartDNS()
        end       
      --uart.on("data")
    end, 0)
   
if pcall(function () dofile("config.lc") end) then
   if HostAP == "true" then
      UARTstatus = "Loading|AccessPoint"
      print (ssid.." "..password)
      StartHostAP()
      ProdWebServer()
   else
      UARTstatus = "Settings Found|"..ssid
      Join()
   end
else
      UARTstatus = "Loading|AccessConfig"
      StartHostAP()
      StartConfigWebServer()
end
   
tmr.alarm(0,30000,1,function() print("Stopping uART") uart.on("data") tmr.stop(0) end)
tmr.alarm(1,5000,1,function() print("!"..UARTstatus) end)
User avatar
By marcelstoer
#62527
Agentsmithers wrote:> = node.heap()
43280


Sounds about right, yep.

Agentsmithers wrote:Communications Error - Check baud rate


The default baud is 115'200.

Try to shrink the script until you find the memory hog / leak.
User avatar
By Agentsmithers
#62531 Thank you for the reply, Is there a way to get a raw size comparison to the amount of Memory Available, I find it hard to believe my script is taking up 16MB?
User avatar
By marcelstoer
#62532
Agentsmithers wrote:I find it hard to believe my script is taking up 16MB?


It's not. There's only 43280 heap available with your firmware configuration. The rest of your 16MB flash (i.e. 16 - 1 = 15MB) can be used for SPIFFS.

See viewtopic.php?f=24&t=12987 and possibly http://nodemcu.readthedocs.io/en/latest ... -footprint. A nice and detailed explanation is embedded in https://www.kickstarter.com/projects/21 ... ts/1501224.