Post your best Lua script examples here

User avatar
By xtal
#40180 NodeMCU LUA SDK 1.5.1
I was showing HEAP SIZE of 20-22k,,, I wanted to increase the free memory so, I decided to place my web page into flash in 4 segments LB0,LB1,LB2,LB3 as functions and retrieve using REQUIRE then execute function.
The functions return a global buffer, I could not figure out how to return contents of local buffer..
The max size each seg is less than 1469 , more like 1100 bytes ..
I managed to cut my code line count 30-40%

MY HEAP is now 14k-16k -- How did I accomplish such a feat!
HOW DID I MANAGE TO DECREASE MY HEAP SIZE THAT MUCH ??????

Below is the code snips I did .....

Code: Select all------------------------------- Below is what I thought would be the MAGIC!! ------------------
local function LBuf(scnt,c)
   LBx = require("LB"..scnt)          --require LB0, LB1, LB2, LB3
   LBx.B0()                                             -- execute the function ie return buf data
   local cbuf = vt.cbuf                      -- save in local buffer
   vt.cbuf = nil                                     -- set global buf to nil
   vt[scnt] = #cbuf                             -- set var[x] to buffer length
   c:send(cbuf)                                    -- send buffer to client
   cbuf = nil                                           -- set local buffer to nil
   package.loaded[LBx]=nil         -- set require code to nil
   collectgarbage()                           -- tidy up
end   
------------------------------- below is the send code for each buffer ------------------------------ 
function Send80(c)   
    if Scnt==0 then
       LBuf(Scnt,c)                                -- get and send seg 0

    elseif Scnt==1 then 
       LBuf(Scnt,c)                                -- get and send seg 1
                     
    elseif Scnt==2 then   
       LBuf(Scnt,c)                               -- get and send seg 2
       
    elseif Scnt==3 then 
       LBuf(Scnt,c)                                -- get and send seg 3
             
    elseif Scnt==4 then
       vt[4]=100        --guesstimate
       local cbuf = vt[0].." | "..vt[1].." | "..vt[2].." | "..vt[3].." | "..vt[4].." | "
       ..vt[0]+vt[1]+vt[2]+vt[3]+vt[4].."    Baud: "..vt.baud.." | "..vt.lua   
       c:send(cbuf)                                -- get and send seg 5
       cbuf=nil
    end   
    if Scnt >=5 then
       c:close()                                       -- close connection
       c=nil
    end                         
         Scnt=Scnt+1                           -- point next seg
         vt.Lcb=vt[0]+vt[1]+vt[2]+vt[3]+vt[4]           --compute size
         collectgarbage()   
end --function end
------------------- Below is the typical   HTML   LB0, LB1,LB2,Lb3 code in flash ----------------
local LB0 = {}     
function LB0.B0()     
     vt.cbuf = '<!DOCTYPE HTML>\n<html lang="en-US">\n<head><meta http-equiv="refresh" content="'
      ..rfh..'" ></head>'
      ..'<script type = "text/javascript">\nvar timeInSecs;\nvar ticker;'
      ..'function startTimer(secs){ timeInSecs = parseInt(secs)-1; ticker = setInterval("tick()",1000); }'
      ..'function tick() { var secs = timeInSecs; if (secs>0) {timeInSecs--;} else {clearInterval(ticker); startTimer('..rfh..');}'               
      ..'document.getElementById("countdown").innerHTML = secs;} startTimer('..rfh..'); </script>'     
      ..'<body><table bgcolor="Lawngreen" border="1" cellpadding="5">'       
      ..'<tr><th><h3>ESP8266</h3><h3>'..vt.device..'</h3></th><th><h3>-SDK-</h3><h3>NodeMCU</h3></th>'
      ..'<th><h3>'..yver..'</h3><h3>'..vt.sware..'</h3></th><th><h3>AAA</h3><h3>BBB</h3></th>'                 
      ..'<td><IMG SRC="'..vt.s_img..'" LEFT="1" WIDTH="120" HEIGHT="60" BORDER="1"></td>'       
      ..'<th><h3 id="countdown">'..rfh..'</h3><h3>'..vt.pass..'</h3></th></tr>'     
      ..'<th bgcolor="Cyan">'..vt.mac..'</th><th bgcolor="Yellow">'..vt.ipa..'</th>'               
      ..'<th>'..vt.pass..'</th><th>'..vt.pass..'</th><th>BAUD = '..vt.baud..'</th>'
      ..'<th>Refresh = '..vt.rfh..'</th>' --</tr>'
      ..'</table><br>'
     
end     
 return LB0
User avatar
By TerryE
#40212 Check your globals; check your packages. If functions return a value then use return not a global. E.g.
Code: Select all 
   require("LB"..scnt).B0()
   package.loaded["LB"..scnt]=nil
  -- . . . 
User avatar
By xtal
#40218 Terry
I made changes you show, and now cannot run code , getting
PANIC: unprotected error in call to Lua API (?:0: attempt to index global 'LBx' (a nil value))


I changed 1 line package.loaded["LB"..scnt]=nil and this appears to of helped,
will need to monitor more...heap has increased....

I still have to use the global vt.cbuf in the LBx buffers, since the return, returns a nil value
local LB0 = {}
function LB0.B0()
vt.cbuf= ~~~~~~~~~~
~~~~~~~~~~~
end
return LB0 -- always returns nil value tried call cbuf = LBx.B0()
should I be returning something different ?

most examples show the return after the end ? is this correct in this case ?
User avatar
By xtal
#40261 I found that this will work...., but now have created a serial input problem , my long data
stream is now handled improperly.....dropping data....

local LB0 = {}
function LB0.B0()
local cbuf= ~~~~~~~~~~ <---------- changed
~~~~~~~~~~~
return cbuf <----------added this
end
return LB0 <------- still need this