Post your best Lua script examples here

User avatar
By xtal
#36195 I've been attempting to use the require ("xxx") to attempt to gain some
more ram..

1st attempt was to put my c:send Buttons as a module

local xxx = {}
function xxx.F1()
cbuf ="~~~~~~~~~~~~~~"
c:send
end
return xxx

then main xxx = require("xxx")
xxx.F1()
This didn't work because of c:send

then removed csend from module and put in main
then main xxx = require("xxx")
xxx()
c:send
return -- to exit function since not last page
- -had to enclose in If stmt else got error

This didn't work because of cbuf was local
made cbuf global - now displays but apparently closes the connection
since the last c:send does not goto to webpage
I have no clue why....

next tried my function uartRX(sdin)

? so how to I get the module to use sdin ?

I'm using more ram than I'm saving.......

Any suggestions appreciated.......

EDIT........................... The following worked for serial input
Code: Select all        --saved as serdat.lua/lc
--[[ local serdat = {}
    function serdat.S1(sdin)     
       if #sdin <= 2 then return end        -- suspect just crlf ??   
        xdin=string.sub(sdin,1,4)
        ydin=string.sub(sdin,1,5)           -- 000>cr
        vt.ESP = #sdin..","..ydin           
        cx = string.find(sdin,"]")             
        if string.sub(sdin,1,2)=="[c" and cx ~= nil then
           cx  = string.find(sdin,"/")
           vt.cdeg0 = string.sub(sdin,3,cx-1)
           sdin = string.sub(sdin,cx+1,#sdin)
           cx  = string.find(sdin,"/")
           vt.ppm0 = string.sub(sdin,1,cx-1)
           sdin = string.sub(sdin,cx+1,#sdin)
           cx  = string.find(sdin,"/")
           vt.apc0 = string.sub(sdin,1,cx-1)
           sdin = string.sub(sdin,cx+1,#sdin)
           cx  = string.find(sdin,"/")
           vt.ph = string.sub(sdin,1,cx-1)
           sdin = string.sub(sdin,cx+1,#sdin)         
           cx  = string.find(sdin,"]")
           vt.prb = string.sub(sdin,1,cx-1) 
           --uart.write(0,"HB\r\n")                  -
        else                           
           vt.sbuf=vt.sbuf..sdin
        end                                           
        local strt,endx,sz
        sz = #vt.sbuf
        if sz >= 730 then         
         strt,endx=string.find(vt.sbuf,"\r\n")
         vt.sbuf = string.sub(vt.sbuf,endx+1,sz)
       end   
    end     
return serdat   
--]]   


function uartRX(sdin)
serdat = require("serdat")
serdat.S1(sdin)
end
User avatar
By xtal
#36246 NodeMCU-- After trying to put the c:send data in modules I've decided this is not a good idea..
Before starting I had 20k heap, I'm now down to 16k ,, apparentely using require is not good for
data buffer usage...[ web page data with ..var.. etc]

While redoing the code had some startling happenings...
1> had problems when relocating some if/else/end statements within the same code block
-- not enough mem 20k heap
2> moving some buffered webpage data from 1st send into 2nd send -- not enough mem 20k heap

memory mgt is apparently is quite lacking with nodemcu sdk 1.4.0
I think I'm wasting my time with REQUIRE.
I think I'm better off using as much local as possible

3> I've tried several times to move all but the 1st web page line to the 2nd send
I get not enough mem , but every thing is fine if I leave it in the 1st send .
Makes no sense!!!!!!!!!!!!

Comments Please!