Esp-8266 Nodemcu Lua获取web和udp服务器

时间:2017-07-10 14:38:49

标签: web lua client nodemcu

如果我删除了"获取部分"我记得这个错误: PANIC:调用Lua API时出现无保护错误(内存不足) PANIC:调用Lua API时无保护错误(尝试调用字符串值)

如果我单独运行2脚本,则不报告错误 获取部分是一个下载脚本选项,如果存在于Web服务器中,则下载(以远程升级lua scritps)

这是我的窗户盾牌。错误在哪里?

-- UPGRADE WEB SERVER
WEBSRV="192.168.1.101" --  for ex. Fenix web server is free and ok

--- - Download new script
--conn1 = net.createConnection(net.TCP, 0)
--conn1:on("receive", function(conn1, payload)
--  print(payload)
--  payloadOffset = string.find(payload, "--CODE") -- script to be uploaded must start with that   , this will be removed automatically when downloading
--  if (payloadOffset) then -- if a valid file is present then delete old one and write the new one
--    file.remove("code.lua"); file.open("code.lua", "w+")
--    file.write(string.sub(payload, payloadOffset)) file.flush() payloadFound = true
--    conn1:close()
--  end
--end)
--conn1:on("connection", function(cn1, answer)
--  cn1:send("GET /code.lua HTTP/1.1\r\n"
--          .. "Host: " .. WEBSRV .. "\r\n"
--          .. "Connection: close\r\nAccept: */*\r\n\r\n")
--end)
--conn1:connect(80, WEBSRV)

-- Server UDP
s = net.createServer(net.UDP)
s:on("receive", function(s, pl)
  print("Command Reveived")
  tmr.alarm(0, 20000, 1, function()
    print("UDP TIMER")
  end)

  if (pl == "SU") then
    print("UDPSU")
  elseif (pl == "GIU") then
    print("UDPGIU")
  end
end)
s:listen(5000)

-- Server TCP
srv = net.createServer(net.TCP)
srv:listen(80, function(conn)
  conn:on("receive", function(client, request)
    local buf = "";
    local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
    if (method == nil) then
      _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
    end
    local _GET = {}
    if (vars ~= nil) then
      for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
        _GET[k] = v
      end
      tmr.alarm(0, 20000, 1, function()
        print("TIMER")
        tmr.stop(0)
      end)
    end


    buf = buf .. "HTTP/1.1 200 OK\r\n Content-Type: text/html\r\n\r\n <!DOCTYPE html><html><head><meta name='viewport' content='width=device-width, initial-scale=1' />"
    buf = buf .. "<title>Tapparella sala piccola</title>"
    buf = buf .. "</head><body>"
    buf = buf .. "<p align='center'><b>Tapparelle sala piccola</b><br>";
    buf = buf .. "<a href=\'?pin=SU\'><button>SU</button></a>&nbsp;<a href=\'?pin=GIU\'><button>GIU</button></a>&nbsp;<a href=\'?pin=STOP\'><button>STOP</button></a></p>";
    buf = buf .. "</body></HTML>\r\n";


    local _on, _off = "", ""


    if (_GET.pin == "SU") then
      print("SU")
    elseif (_GET.pin == "GIU") then
      print("GIU")
    elseif (_GET.pin == "STOP") then
      print("STOP")
    end
    client:send(buf);
    client:close();
    collectgarbage();
  end)
end)

0 个答案:

没有答案