As the title says... Chat on...

User avatar
By xtal
#30950 I've been trying to figure out how to use flashmod -- Yes the example works great, but trying to adapt my code has got me totally confused.....
Here is the code I'm trying to adapt for flashmod , I don't really know where to start.
I'm unclear if I can put the 1st 8 lines in flash , and how to do it?

I have several vars I will be receiving updated values over the serial port [if I can get that sorted out] that will need to stay in ram ..

I thought I could put all the C:send in flash, but not sure if the vars would update when executed.
I'm at the not enough memory and can't node compile because not enough memory.... :x
Converting this could be a good learning exercise for many of us without great programming skills.

All help will be greatly appreciated :mrgreen:


Code: Select allwifi.setmode(wifi.STATION)
wifi.sta.config("12345-NETGEAR","mikeis12345")

tmr.alarm(0, 1000, 1, function(QQ)   
   print("Try Connecting:")
   ip, nm, gw=wifi.sta.getip()
  if ip ~= nil then         
      print("\nIP Info: \nIP Address: ",ip,"\n Netmask: ",nm,"\n Gateway: ",gw)         
      tmr.stop(0)
   end
end)

rfctr=0
adc_id = 0
adc_value = 512
k1=0.11
p00='P'
apc0='11111'
ppm0='1770'
cdeg0='25.1'

kta0='0.01'
lo0='12'
hi0='20'
prb='01.0'
ph='7.0'
sbuf=""--('D=dump Z=calibrate= z [table 1-5] [slot0-9] ppmcal ppmcnt\r\n'
--..'============================== EEPROM DUMP ======================\r\n'
--..'000>  00001,00032 00342,65535 00700,66535 01000,34770 01400,65535\r\n020>\r\n040>\r\n060>\r\n080>\r\n100>\r\n120>\r\n140>\r\n160>\r\n180>\r\n') 
--..'020>\r\n') --  01840,45350 02000,48550 02450,66535 02800,65535 03260,54350\r\n'
--..'040>\r\n') --  00001,00032 00342,65535 00700,66536 01000,34770 01400,65535\r\n'
--..'060>\r\n' --- 00001,00032 00342,65535 00700,66536 01000,34770 01400,65535\r\n')
--..'000>\r\n' -- 00001,00032 00342,65535 00700,66536 01000,34770 01400,65535\r\n')
--..'000>\r\n') -- 00001,00032 00342,65535 00700,66536 01000,34770 01400,65535\r\n')
--..'000>\r\n' -- 00001,00032 00342,65535 00700,66536 01000,34770 01400,65535\r\n')
--..'000>\r\n' -- 00001,00032 00342,65535 00700,66536 01000,34770 01400,65535\r\n')
--..'000>\r\n' -- 00001,00032 00342,65535 00700,66536 01000,34770 01400,65535\r\n')
--..'000>\r\n') --  00001,00032 00342,65535 00700,66536 01000,34770 01400,65535\r\n')--]]

Eopn=0
B_Opn = "http://i.imgur.com/kzt3tO8.png"
B_Clo = "http://i.imgur.com/KS1dPa7.png"
bgc="Cyan"
s_img = B_Opn

-- simple uart input appears to work
 uart.setup(0,9600,8,0,1)
--uart.on("data",40,function(data)  print("Rcv uart: ",data) end,0)       
--uart.on("data","\n\r",function(data)  print("Rcv uart: ",data) end,0)
--uart.setup(0,9600,8,0,1)
                                     
srv = net.createServer(net.TCP, 30)
srv:listen(80,function(C)
     C:on("receive", function(C, payload)               
          function esp_update() 
  --print("--PL--",payload)
                     
            m_do=string.sub(payload,postparse[2]+1,#payload)
            x=string.find(m_do,"&m_do=")
         
            if x ~= nil then
               cmd=string.sub(m_do,x+6,#m_do)
               m_do=string.sub(m_do,1,x-1)
            else               
               cmd=string.sub(m_do,1,#m_do)
               m_do=nil
            end
               
            cmd=string.gsub(cmd,"+","")         -- remove lead/trail +       
      --  print("m_do-2 ",m_do,"  cmd "..cmd)
           
           if cmd ~= nil then
  print("---",cmd)
           
             uart.write(0,cmd.."\n\r")          -- send to PIC
        --     print("-->uart-->pic=|"..cmd.."\n\r")           
           end
                             
            if m_do == "RD_ADC" then
               adc_value = adc.read(adc_id)
                    if adc_value > 1023 then
                         adc_value = 1023
                    elseif adc_value < 0 then
                         adc_value = 0
                    end                                 
            end
           
            if m_do == "BLINK" then
               if Eopn==0 then
                  s_img =B_Clo
                  Eopn=1
                bgc="Yellow"
               else
                  s_img =B_Opn
                  Eopn=0
               bgc="Cyan"
              end
            end             
            if m_do == "TEMP_DN" then
                apc0 = apc0 + 100
                cdeg0=round2((cdeg0-1),1)                           
            end   
            if m_do == "TEMP_UP" then 
                apc0 = apc0 - 100         
                cdeg0=round2((cdeg0+1),1)                           
            end           
        end     
        postparse={string.find(payload,"m_do=")}
        if postparse[2]~=nil then esp_update()end
      --C:send('HTTP/1.1 200 OK\n\n')

fdeg0 = round2((cdeg0*1.8) +32,1)
ppm1=round2(ppm0/(((cdeg0-25)*.021)+1),1)
ppm2=round2((ppm0*46.5)/(cdeg0+21.5),1 )
pha=25-cdeg0
if pha < 0 then
   pha = round2((ph + (pha*.002)),2)
else
   pha = round2((ph + (pha*.004)),2)   
end
rfh='15'   
C:send('<!DOCTYPE HTML>\n<html lang="en-US">\n<head><meta http-equiv="refresh" content="'..rfh..'" ></head>\n')
C:send('<script type = "text/javascript">\nvar timeInSecs;\nvar ticker;')
C:send('function startTimer(secs){ timeInSecs = parseInt(secs)-1; ticker = setInterval("tick()",1000); }')
C:send('function tick() { var secs = timeInSecs; if (secs>0) {timeInSecs--;} else {clearInterval(ticker); startTimer('..rfh..');}')               
C:send('document.getElementById("countdown").innerHTML = secs;} startTimer('..rfh..'); </script>')
C:send('<body><div><table bgcolor="Lawngreen" border="1" cellpadding="5"><th><h1>ESP8266 Blinker Thing!</h1></th><td>'
..'<IMG SRC="'..s_img..'" LEFT="1" WIDTH="120" HEIGHT="60" BORDER="1"></td><th id="countdown">'..rfh..'</th></table><div><br>')

C:send('<textarea name="cmds" cols="70" rows="14">'..sbuf..'</textarea></div><br>')
C:send('<textarea name="cmds" cols="70" rows="6">'..sbuf..'</textarea></div><br>') 
 
C:send('<div><table bgcolor="'..bgc..'" border="1" cellpadding="5">')
C:send('<tr><th>Sensor</th><th>TempC</th><th>TempF</th><th>PPMcntr</th><th>PPM__</th><th>Adj__1</th><th>Adj__2</th></tr>')
C:send('<tr><td>DS18B20'..p00..'</td><td>'..cdeg0..'</td><td>'..fdeg0..'</td><td>'..apc0..'</td><td>'..ppm0..'</td><td>'..ppm1..'</td>')
C:send('<td>'..ppm2..'</td></tr><tr><th>Probe-K</th><th>Pool-PH</th><th>PH-Adj</th><th>ADC Value</th></tr>')     
C:send('<tr><td>'..prb..'</td><td>'..ph..'</td><td>'..pha..'</td><td bgcolor="Red">'..adc_value..'</td></tr></table></div><br>')

C:send('<form action="" method="POST">\n<input type="submit" name="m_do" value="BLINK" style="padding:5px;10px;">')
C:send('<input type="submit" name="m_do" value="RD_ADC" style="padding:5px;10px;">')     
C:send('<input type="submit" name="m_do" value="TEMP_DN" style="padding:5px;10px;">')
C:send('<input type="submit" name="m_do" value="TEMP_UP" style="padding:5px;10px;"><br>')                   
C:send('"CMD:"<input type="text" name="m_do" value="" maxlength="100" style="padding:5px;10px;">')
C:send('<input type="submit" value="Submit"></form>\n<br></body></html>\n')
       
C:on("sent", function(C) C:close() end)
       
     end)
 
rfctr=(rfctr+1) 
rfc=math.floor((rfctr/2)+0.5)  -- seems to double count so /2 and round
if (rfc % 2)==0 then
   bgc="Cyan"    --even
else
    bgc="Yellow" --odd
end   
             
end)

function round2(num, idp)
  return tonumber(string.format("%." .. (idp or 0) .. "f", num))
end

function si(sIN)   
   buffer=sIN
 
if string.find(sIN,"=si('") then
   sa=string.find(sIN,"=si('I")
   sb=string.find(sIN,"')")
   sbuf=sbuf..string.sub(sIN,sa+6,sb)
end
     
      -- print("\n\rbuf--",buffer)
      if string.find(sIN,"co") then
      if string.find(sIN,"bo") then             
      cx=(string.find(sIN,"co"))+2
      cdeg0 = string.sub(sIN,cx,cx+3)         
       if string.find(sIN,"ao") then
       cx=(string.find(sIN,"bo"))+2             
       ppm0=string.sub(sIN,cx,cx+3)                               
        if string.find(sIN,"xo") then
        cx=(string.find(sIN,"ao"))+2
        apc0 = string.sub(sIN,cx,cx+4)
        end
       end   
      end
      end
     
 
     
end



function cbuf()
  if #sbuf + #sIN > 200 then
     sbuf = sIN
  else
     sbuf = sbuf..sIN
  end
end       
 
       
function all_trm(s)
   return s:match( "^%s*(.-)%s*$" )
end
   
function all_trim(s)
  return s:match"^%s*(.*)":match"(.-)%s*$"
end             
User avatar
By xtal
#31163 I guess not many people using nodeMCU want to put their functions in flash...
I thought I would try simplify 2 if-then's into a function and save some ram...WRONG...not enough memory..
nodeMCU makes no damn sense when it come's to not enough memory
node.heap() says I have 12000+ but if I add 1 short line to send html code , I don't have enough memory...
This is what I tried to convert .. then after undoing the change I had to delete 2 more line from my code to get a good load,
just makes no sense......

Code: Select all if m_do == "TEMP_DN" then   
               apc0 = apc0 + 100
              cdeg0 = round2((cdeg0 -1),1)
            end         
            if m_do == "TEMP_UP" then 
               apc0 = apc0 - 100
               cdeg0 = round2((cdeg0 +1),1)
            end
 
-- The above code that would allow good save was converted to the function below
-- function over head must eat a lot of ram

- - if m_do == "TEMP_DN" then Tadj(-1) end
-- if m_do == "TEMP_UP" then Tadj(1) end

function Tdaj(mod)                        --mod = +1 or -1
      apc0 = apc0 - (mod * 100)
      cdeg0 = round2((cdeg0 +mod),1)
end
User avatar
By TerryE
#31233 Have a look at the FAQ linked below where I explain some of the issues. The later dev builds include luac.cross and I've got about 44Kb free RAM after boot on the latest dev140 build.
Last edited by TerryE on Mon Oct 19, 2015 7:08 am, edited 1 time in total.
User avatar
By xtal
#31618 Well-wasted another day trying to convert the previous code POST#1 for use with flashmod.lua ... :cry:
I've come to the conclusion that the instructions suck, unless you only want to print a few lines.

Its amazing that people spend a lot of time creating and posting , but come way short in delivering a real world
working example, that works 1st try , I just don't understand. :oops:

Flashmod looks promising, IF I CAN EVER FIGURE OUT HOW TO MODIFY THE ABOVE CODE POST#1 TO USE IT!!! :cry:

Moderator - can you change TOPIC to FLASHMOD.LUA instead of FLADHMOD.LUA