ESP8266 NodeMCU 内存不足

Posted

技术标签:

【中文标题】ESP8266 NodeMCU 内存不足【英文标题】:ESP8266 NodeMCU out of memory 【发布时间】: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 我正在查看是否有办法调整内存以处理一次,但我想这还不可能。 【参考方案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)

【讨论】:

以上是关于ESP8266 NodeMCU 内存不足的主要内容,如果未能解决你的问题,请参考以下文章

NodeMCU:可用堆大小下降,直到内存不足错误

ESP8266 NodeMcu机智云SOC方案开发经验分享

arduino nano 链接 esp 8266 wifi模块

项目相关问题

ESP8266(nodemcu)引脚和I2C的坑!!!

ESP8266-NodeMCU项目:ESP8266-NodeMCU+Blinker+DHT11+小爱同学