【问题标题】:ESP8266 NodeMCU out of memoryESP8266 NodeMCU 内存不足
【发布时间】:2019-01-18 02:22:49
【问题描述】:

NodeMCU 信息

> Lua 5.1.4 
> SDK 2.2.1
> Memory Usage :
> Total : 3260490 bytes 
> Used  : 9287 bytes 
> Remain: 3251203 bytes

当我尝试发送带有大 json 字符串响应 (json_response) 的 HTTP 响应时出现错误

PANIC: unprotected error in call to Lua API (file.lua:5: out of memory)

代码:

  -- a simple HTTP server
    srv = net.createServer(net.TCP)
    srv:listen(80, function(conn)
        conn:on("receive", function(sck, payload)
            sck:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"..json_response)
        end)
        conn:on("sent", function(sck) sck:close() end)
    end)

【问题讨论】:

  • 这里需要任何进一步的反馈吗?
  • @MarcelStör 我正在查看是否有办法调整内存以处理一次,但我想这还不可能。

标签: memory-management lua out-of-memory esp8266 nodemcu


【解决方案1】:

是的,如果您要发送大量数据,这将不起作用。您需要逐个发送。 Our API documentation 展示了两种方法(您可以在此处找到更多关于 SO 的参考资料)第一种是:

srv = net.createServer(net.TCP)

function receiver(sck, data)
  local response = {}

  -- if you're sending back HTML over HTTP you'll want something like this instead
  -- local response = {"HTTP/1.0 200 OK\r\nServer: NodeMCU on ESP8266\r\nContent-Type: text/html\r\n\r\n"}

  response[#response + 1] = "lots of data"
  response[#response + 1] = "even more data"
  response[#response + 1] = "e.g. content read from a file"

  -- sends and removes the first element from the 'response' table
  local function send(localSocket)
    if #response > 0 then
      localSocket:send(table.remove(response, 1))
    else
      localSocket:close()
      response = nil
    end
  end

  -- triggers the send() function again once the first chunk of data was sent
  sck:on("sent", send)

  send(sck)
end

srv:listen(80, function(conn)
  conn:on("receive", receiver)
end)

【讨论】:

    猜你喜欢
    • 2016-09-15
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 2021-09-20
    • 2021-07-25
    • 1970-01-01
    相关资源
    最近更新 更多