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

User avatar
By xtal
#33794 dev 0,9.6 method local function connect(conn,data)
conn:on("recieve",function(cn),PL)
<misc code>
cn:send(~~~~~~~)
cn:send(~~~~~~~)
|
cn:send(~~~~~~~)
cn:close()

SDK 1.4.0 is only allowing 1 cn:send and limits to < 1460 characters
I need to be send more than 1460, I have found little info on doing this.
Also have found little info on 1.4.0 doc's /info/restrictions etc........
Would somebody in the knowhow please provide a code snip....

..'<form action="" method="POST">\n'
..'<input type="submit" name="pwmi" value="Blink">\n</form><br>')

if I only send thru "POST">\n</form><br>') it will send
if I send thru value="Blink">\n</form><br>') it fails with need <1460 error
This must be the 1460 limit

Tried storing all in a buffer, apparently buffersize is also limited ....

SO HOW DO I send a WPAGE > 1460


Code: Select all 
vt={}
vt.cmd="--"
vt.rfh=15
vt.cmx = "--"
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.sin="                                                                              "
vt.sbuf=('000>  00001,00032 00342,65535 00700,66535 01000,34770 01400,65535\r\n' 
..'020>  01840,45350 02000,48550 02450,66535 02800,65535 03260,54350\r\n' 
..'040>  00001,00032 00342,65535 00700,66536 01000,34770 01400,65535\r\n') 
--..'060>  00001,00032 00342,65535 00700,66536 01000,34770 01400,65535\r\n')
--..'080>  00001,00032 00342,65535 00700,66536 01000,34770 01400,65535\r\n')
--[[..'100>  00001,00032 00342,65535 00700,66536 01000,34770 01400,65535\r\n'
..'120>  00001,00032 00342,65535 00700,66536 01000,34770 01400,65535\r\n'
..'140>  00001,00032 00342,65535 00700,66536 01000,34770 01400,65535\r\n'
..'160>  00001,00032 00342,65535 00700,66536 01000,34770 01400,65535\r\n'
..'180>  00001,00032 00342,65535 00700,66536 01000,34770 01400,65535\r\n')--]]
local SSID = "12345-NETGEAR81"
local SSID_PASSWORD = "mikeis12345"
local B_Opn = "http://i.imgur.com/kzt3tO8.png"
local B_Clo = "http://i.imgur.com/KS1dPa7.png"
local s_img = B_Opn
local fdeg0 = 75.1
local ppm1=1766
local ppm2=1766
local pha=7.0
local bgc='Cyan'
local adc_value = 512
local cmd = "--"

local function connect (conn, data)
   local query_data                               -- local var
   --  uart.setup(0,9600,8,0,1)
 
       
   conn:on ("receive", function (cn, req_data)
    query_data = get_http_req (req_data)   
    if cmd=="Blink" then
           if s_img == B_Opn then s_img = B_Clo else s_img = B_Opn end                         
    elseif cmd=="Adc" then  adc_value = adc.read(0)                               
    elseif cmd=="I" then uart.write(0,"I")                 
    elseif cmd=="D" then uart.write(0,"D")               
    elseif cmd=="Tup" then vt.cdeg0 = vt.cdeg0 + 0.1                 
    elseif cmd=="Tdn" then vt.cdeg0 = vt.cdeg0 - 0.1                 
    elseif cmd=="B038" then  uart.setup(0,38400,8,0,1)               
    elseif cmd=="B115" then uart.setup(0,115200,8,0,1)                 
    elseif cmd=="B115" then uart.setup(0,9600,8,0,1)                                               
    elseif cmd=="V" then uart.write(0,"V")                                         
    elseif cmd=="W" then uart.write(0,"W")
    elseif cmd=="RFH" then if vt.rfh==15 then vt.rfh=30 else vt.rfh=15  end       
    elseif cmd=="K1" or cmd=="K2" or cmd=="K3" then uart.write(0,cmd)                     
    elseif cmd=="K%d" then
           uart.write(0,cmd)
           -- print("\n"..vt.cmd)                     
    elseif cmd=="Z%d" or vt.cmd=="Z%d" then
           uart.write(0,cmd)
           --  print("\n"..vt.cmd)                                                                       
    end
----------------------------   
 --  uart.on("data","\r",
 --      function(sIN)
 --      cmd=sIN
     --  if cmd=='=node.heap()' then  print(node.heap())           
    --   elseif cmd=='=node.restart()' then '='..node.restart()     
     --  end
       
  --     end,1)
---------------------------- 
  --  function si(value)
  --     cmd=value
  --  end
--[[function si(sIN)   
   cmd=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
      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 --]]
                 
  if cmd ~= "--" then vt.cmx=cmd end
print("Heap ",node.heap(),vt.cmx)   
  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()     
rfh=vt.rfh
cn:send('<!DOCTYPE HTML>\n<html lang="en-US">\n<head><meta http-equiv="refresh" content="'..rfh..'" ></head>\n'
..'<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>'
..'<IMG SRC="'..s_img..'" LEFT="1" WIDTH="120" HEIGHT="60" BORDER="1"></td><th id="countdown">'..rfh..'</th></table><div><br>'
..'<div><table bgcolor="'..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>----CMD----</th></tr>'     
..'<tr><td>'..vt.prb..'</td><td>'..vt.ph..'</td><td>'..pha..'</td><td bgcolor="Red">'..adc_value..'</td><td>'..vt.cmx..'</td></tr></table></div><br>'
..'<div><textarea name="cmds" cols="70" rows="10">'..vt.sbuf..'</textarea></div><br>'
..'<form action="" method="POST">\n'

..'<input type="submit" name="pwmi" value="Blink">\n</form><br>')
--[[..'<input type="submit" name="pwmi" value="Adc">\n</form><br>')
 
..'<input type="submit" name="pwmi" value="D">\n'
..'<input type="submit" name="pwmi" value="I">\n'
..'<input type="submit" name="pwmi" value="Tup">\n'
..'<input type="submit" name="pwmi" value="Tdn">\n'
..'<input type="submit" name="pwmi" value="B96">\n'
..'<input type="submit" name="pwmi" value="B38">\n'
..'<input type="submit" name="pwmi" value="B115">\n'
..'<input type="submit" name="pwmi" value="W">\n'
..'<input type="submit" name="pwmi" value="V">\n'
..'<input type="submit" name="pwmi" value="RFH"><br>'
..'CMD<input type="text" name="pwmi" value="">'   
..'<input type="submit" value="Submit"></form><br>') --]] 

         cn:close ( )
   collectgarbage()
      end)
end
--------------------------------------------------------
function round2(num, idp)
  return tonumber(string.format("%." .. (idp or 0) .. "f", num))
end
--------------------------------------------------------
function wait_for_wifi_conn ( )
   local ip, nm, gw
   tmr.alarm (1, 1000, 1, function ( )
      if wifi.sta.getip ( ) == nil then
         print ("Wait Connect")
      else
         tmr.stop (1)
         ip, nm, gw = wifi.sta.getip()
         print ("Mode= " .. wifi.getmode ( ))
         print ("MAC " .. wifi.ap.getmac ( ))
         print ("IP: "..ip.."\nNM: "..nm.."\nGW: "..gw)
      end
   end)
   collectgarbage()
end
--------------------------------------------------------
function get_http_req (PL)
print("<"..#PL..">",PL)           
   local strt_ndx,end_ndx 
       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=")  -- find &pwmi
          if strt_ndx then cmd = string.sub(cmd,1,strt_ndx-1) end -- fld 2         
       end
  -- print(vt.cmd)   
---------------------------------------------------------------           
   collectgarbage()
end
------------------------------------------------
function trim (s)            -- String trim left and right
  return (s:gsub ("^%s*(.-)%s*$", "%1"))
end
-----------------------------------------------

-----------------------------------------------
-- Configure the ESP as a station (client)
wifi.setmode (wifi.STATION)
wifi.sta.config (SSID, SSID_PASSWORD)
wifi.sta.autoconnect (1)
wait_for_wifi_conn ( )
svr = net.createServer (net.TCP, 30)  -- Create the httpd server
collectgarbage()
svr:listen (80, connect)  --listen port 80, call connect upon request
collectgarbage()

User avatar
By TerryE
#33843 Please read my FAQ. Multiple send requests in the same callback should never have worked and case the SDK to run oot of resources and crash. This has been -- correctly -- tightened up in the lastest SDK and dev firmware. Use a global table to stack the records to be sent and use a sk:on("send", send_cb) to do table.remove(pending_recs,1) to unshift the list and send the next record, but remember to use a #pending_recs>1 guard to detect if the list has been emptied.

Your application is like a bead neckless. Each "bead" is a Lua callback routine which processes one event and does at most one async Wifi API call. Each individual cb can be written procedurally, but the "beads" are strung into a necklace using an overall even driven approach. You need to get your head around this way of thinking.
User avatar
By xtal
#33878 managed to save html code into file and readback and issue sends after call back...,BUT
this raises another problem..... The embeded ..vars.. are not utilized/updated .........
so what is the difference between
c:send('<h1> "abc-"..varx.."-xyz"</h1>') and c:send(data) where data = <h1>"abc-"..varx.."-xyz"</h1>
any input appreciated.....
User avatar
By TerryE
#33913 You do t he insert after the variables can be bound, so in your case if you are sending three records
Code: Select all  local insert = table.insert
  reply = { --[[list of format statements, one per send block]] }
  -- . . .
 
  insert(reply[1]:format(rfh,rfh)
  insert(reply[2]:format(s_img, rfh, vt.p00, vt.cdeg0, fdeg0, vt.apc0, vt.ppm0,
         ppm1, ppm2, vt.prb, vt.ph, pha, adc_value, vt.cmx, vt.sbuf)
  insert(reply[3])
  sock:on_send_cb() -- Kick the callback to start sending the data.