Post your best Lua script examples here

User avatar
By xtal
#34552 Partial working code still under construction... Microchip pic collects data and sends over serial port to ESP
Needs sdk 1.4.0 , serial stuff still in works
code is approaching mem limits
Code: Select all--------ver 7a  use sdk 1.4.0
function c_station()         
     if wifi.sta.getip() == nil then
        print("Connect")
     else     
        ip, nm, gw = wifi.sta.getip()
        do_next = c_server
        print("IP: "..ip,"NM: "..nm,"GW: "..gw.."\n")           
     end
     collectgarbage()
   end
-------------------------------
function c_server() 
    do_next = nocode                   -- just in case timewaste
    sv=net.createServer(net.TCP, 30)   --create server
    sv:listen(80,function(c)           --listen on port 80
       c:on("sent", Send80)
       c:on("receive", Rcv80)          --when rcv do function rcv99         
       end)       
end
-----------------------------
function nocode()
   collectgarbage()
 --uart.write(0,"++")
   uart.on("data", "\n",                    --000>crlf
      function(sdin)                        --only executes if data avail
        if string.sub(sdin,1,2)=="nh" then vt.heap = node.heap() end
        if #sdin <= 2 then return end            --was only crlf   
        if string.sub(sdin,1,2)=="[c" 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,"/")
         --  prb = 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)           
        else                 
           ydin=string.sub(sdin,1,5)           -- 000>cr
           vt.ESP=ydin.."-"..#sdin
           xdin=string.sub(sdin,1,4)           --         
                        --print("S_Rcv:",#sdin,sdin,#ydin,ydin,#xdin,xdin)           
           if xdin=="000>" then vt.sbuf="" end
           vt.sbuf=vt.sbuf..sdin
        end                                           
chkbufsize()   
    end, 0) --0)
end
 
---------------------------
function process1()
end
function process2()
end
--------------------------------------------------------
function round2(num, idp)
  return tonumber(string.format("%." .. (idp or 0) .. "f", num))
end
------------------------------------------------
function trim (s)            -- String trim left and right
  return (s:gsub ("^%s*(.-)%s*$", "%1"))
end
---------------------------
function chkbufsize()
  local strt,endx,sz
  sz = #vt.sbuf
      if sz >= 671 then                     -- buffer limit
        print("<--",sz)
         strt,endx=string.find(vt.sbuf,"\r\n")
         vt.sbuf = string.sub(vt.sbuf,endx+1,sz)
         print(strt,endx,#vt.sbuf)
      end
end
---------------------------
function Rcv80(c, pl)
-- print("PL ",pl)
   local B_Opn = "http://i.imgur.com/kzt3tO8.png"
   local B_Clo = "http://i.imgur.com/KS1dPa7.png"
   local s_img = B_Opn
   local crlf=0x0D..0x0A     
   local strt_ndx,end_ndx,cmv,tmp
   vt.cmx="--"
   cmv=""
       strt_ndx, end_ndx = string.find (pl, "pwmi=")                -- find 1st pwmi
       if strt_ndx ~= nil then                                      -- POST occurred       
          cmd = string.sub(pl, end_ndx+1,#pl)                       -- save fld 1
          strt_ndx, end_ndx = string.find (cmd, "&pwmi=")           -- 2nd &pwmi if submit
          if strt_ndx then
             cmd = string.sub(cmd,1,strt_ndx-1)                     -- fld 2
             cmv=cmd                                  --submit
          end   
             
       end       
    if cmd==nil then cmd="--"   
       elseif cmd=="EyO" then s_img=B_Opn   
       elseif cmd=="EyC" then s_img=B_Clo                                       
       elseif cmd=="Adc" then  adc_value = adc.read(0) 
       elseif cmd=="Tup" then vt.cdeg0 = vt.cdeg0 + 0.1                 
       elseif cmd=="Tdn" then vt.cdeg0 = vt.cdeg0 - 0.1
       elseif cmd=="ClrB" then vt.sbuf=""
       elseif cmd=="Rfh" then
        vt.rfh=vt.rfh+5
        if vt.rfh==35 then vt.rfh=5 end
     end                                 
     tmp=0                             
     if cmd=="I" then tmp=1                 
       elseif cmd=="D" then tmp=1                                                                                       
       elseif cmd=="V" then tmp=1                                         
       elseif cmd=="W" then tmp=1                 
       elseif cmd=="K1" or cmd=="K2" or cmd=="K3" then tmp=1                     
       elseif cmd=="K%d" then  tmp=1   -- print("\n"..vt.cmd)                     
       elseif cmd=="Z%d" or vt.cmd=="Z%d" then tmp=1  --  print("\n"..vt.cmd)     
     end
     if tmp==1 then
       uart.write(0,cmd..crlf)
     end
   
    if cmd ~= "--" then vt.cmx=cmd end
    vt.heap = node.heap()     
    cmd="--"     
    adc_value = adc.read(0)             --- Temperature Adjustments + -----
    fdeg0 = round2((vt.cdeg0*1.8) +32,1)
    ppm1=round2(vt.ppm0/(((vt.cdeg0-25)*.021)+1),1)
    ppm2=round2((vt.ppm0*46.5)/(vt.cdeg0+21.5),1 )
    pha=25-vt.cdeg0
    pha = round2((vt.ph + (pha*.004)),2)
    if pha < 0 then pha = round2((vt.ph + (pha*.002)),2) end
             
    collectgarbage()   
 
    adc_value = adc.read(0) 
    rfh=vt.rfh
    Scnt=0
    cmx = vt.cmx
    if Scnt==0 then
    local 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><div><table bgcolor="Lawngreen" border="1" cellpadding="5"><th>'
         
     --..'<h1>ESP8266 Blinker Thing!</h1></th><td>'
      ..zver 

      ..'<IMG SRC="'..s_img..'" LEFT="1" WIDTH="120" HEIGHT="60" BORDER="1"></td>'
      ..'<th id="countdown">'..rfh..'</th></table><div><br>'

 c:send(cbuf)
 --   print("CB:",#cbuf)   
    cbuf=nil 
    Scnt=Scnt+1
  end
end
----------------------------
function Send80(c)
  --uart.write(0,Scnt)   
  if Scnt==1 then       
   local cbuf='<div><table bgcolor="'..vt.bgc..'" border="1" cellpadding="5">'
      ..'<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>'
      ..'<tr><td>DS18B20'..vt.p00..'</td><td>'..vt.cdeg0..'</td><td>'..fdeg0..'</td>'
      ..'<td>'..vt.apc0..'</td><td>'..vt.ppm0..'</td><td>'..ppm1..'</td>'
      ..'<td>'..ppm2..'</td></tr><tr><th>Probe-K</th><th>Pool-PH</th><th>PH-Adj</th>'
      ..'<th>ADC Value</th><th> ESP --> PIC </th><th> PIC --> ESP </th><th> Heap </th></tr>'     
      ..'<tr><td>'..vt.prb..'</td><td>'..vt.ph..'</td><td>'..pha..'</td>'
      ..'<td bgcolor="Red">'..adc_value..'</td><td>'..vt.cmx..'</td>'
      ..'<td>'..vt.ESP..'</td><td>'..vt.heap..'</td></tr></table></div><br>'
      c:send(cbuf)
      --   print("CB:",#cbuf)   
     cbuf=nil 
     Scnt=Scnt+1
     return
  end
-------------------------------
  --uart.write(0,Scnt)   
  if Scnt==2 then       
   local cbuf='<form action="" method="POST">'
      ..'<input type="submit" name="pwmi" value="EyO">'
      ..'<input type="submit" name="pwmi" value="EyC">'
      ..'<input type="submit" name="pwmi" value="Adc">'
      ..'<input type="submit" name="pwmi" value="D">'
      ..'<input type="submit" name="pwmi" value="I">'
      ..'<input type="submit" name="pwmi" value="Tup">'
      ..'<input type="submit" name="pwmi" value="Tdn">'
      --..'<input type="submit" name="pwmi" value="B96">'
      --..'<input type="submit" name="pwmi" value="B38">'
      ..'<input type="submit" name="pwmi" value="ClrB">'
      ..'<input type="submit" name="pwmi" value="W">'
      ..'<input type="submit" name="pwmi" value="V">'
      ..'<input type="submit" name="pwmi" value="Rfh"><br>'
      ..'CMD<input type="text" name="pwmi" value="">'   
      ..'<input type="submit" value="Submit"></form><br>' 
    c:send(cbuf)
 --   print("DB:",#dbuf)   
    cbuf=nil
    Scnt=Scnt+1
    return  -- if not last send
  end
--------------------------------------   
  if Scnt==3 then
   local cbuf='<div><textarea name="cmds" cols="70" rows="12">'..vt.sbuf..'</textarea></div><br>'
   c:send(cbuf)
   cbuf=nil
--    Scnt=Scnt+1
   --return
--  end
    c:close()
    collectgarbage()
  end 
end
--------- MAIN ----------------------

wifi.setmode (wifi.STATION)
wifi.sta.config ("12345c-NETGEAR81","mikeis12345")
wifi.sta.autoconnect (1)
-------------------------------------
--aVer, bVer, cVer, cid, fid, fsz, fm, fsp = node.info();
aVer, bVer, cVer = node.info()
yver=aVer.."."..bVer.."."..cVer
-------------------------------------
zver = "<h1>ESP8266 SDK "
        ..yver
        .." Version!</h1></th><td>"
--print(yver)
collectgarbage()
-------------------------------------
do_next = c_station
tmr.alarm( 0, 250, 1, function() do_next() end) --.5ms chek next do
-------------------------------------
vt={}
vt.scb=0        -- sent call back
vt.rfh=15
vt.k1=0.11
vt.p00='P'
vt.apc0=44444
vt.ppm0=1770
vt.cdeg0=25.1
vt.prb=1.0
vt.ph=7.0
vt.heap=12345
vt.bgc ="Cyan"
vt.cmx="--"
vt.cmd="--"
vt.ESP="  "
vt.sin="  "
vt.sbuf='000>  00001,00032 00342,65535 00700,66535 01000,34770 01400,65535\r\n'
..'020>  00001,00032 00342,65535 00700,66535 01000,34770 01400,65535\r\n'
..'040>  00001,00032 00342,65535 00700,66535 01000,34770 01400,65535\r\n'
..'060>  00001,00032 00342,65535 00700,66535 01000,34770 01400,65535\r\n'
..'080>  00001,00032 00342,65535 00700,66535 01000,34770 01400,65535\r\n'
..'100>  00001,00032 00342,65535 00700,66535 01000,34770 01400,65535\r\n'
..'120>  00001,00032 00342,65535 00700,66535 01000,34770 01400,65535\r\n'
..'140>  00001,00032 00342,65535 00700,66535 01000,34770 01400,65535\r\n'
..'160>  00001,00032 00342,65535 00700,66535 01000,34770 01400,65535\r\n'
..'180>  00001,00032 00342,65535 00700,66535 01000,34770 01400,65535\r\n'
..'200>  00001,00032 00342,65535 00700,66535 01000,34770 01400,65535\r\n'
..'220>  00001,00032 00342,65535 00700,66535 01000,34770 01400,65535\r\n'
     
 

User avatar
By xtal
#34583 Additional info:

serial input of [c26.1/1660/43677/71/10]
will change Temp to 26.1c and the degrees F will update along with temperature affected values
1660 update ppm
43677 updates ppm counter the pic uses the ppm counter referenced to a table to generate ppm
71 is the ph -- need add divide/10 to code
10 will be the probe constant , will support 0-255 = 0 to 25.5 , but really just want 1,10

The D,I,W,V send querries to pic which will respond , usually using data window

Current setup:

pic--- > Bluetooth Serial >>>>38400<<<< Bluetooth Serial >>>com14-38400<<< computer |liberty basic bridge software |usb com2 >>>>9600<<<<ESP
User avatar
By xtal
#36693
serial
(1.43 KiB) Downloaded 573 times
main file
(11.62 KiB) Downloaded 517 times

The working version attached ...
load both .txt as .lua files and compile then dofile(do140-13wa.lc)
must use external serial in to change the data ...
The pic program that supplies the info I still be worked on.....
User avatar
By xtal
#38138 I'm in process of porting this to a sketch, It's been a good teaching/learning tool..

I only have the serial input left to port, and an having issues with that..

The Arduino IDE was not to hard to begin using Ver 1.6.5. I have a lot to learn yet..
I'm just using USBRxTx and ESP and Arduino IDE , I'm not using any Arduino Boards,,,