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

时间:2018-04-29 08:00:26

标签: java http lua esp8266 nodemcu

我的NodeMCU面临一些麻烦,在接入点模式下运行一个简单的服务器,使用ESPlorer在LUA中编程。

这是LUA脚本:

local SSID = "NodeMCU"
local SSID_PASSWORD = "12345678"

function connect (conn)
   print ("Hello connect")
   conn:on ("receive",
       function (cn, req_data)
           print(req_data)
           print("Available memory :")
           print(node.heap())
           --local query_data = get_http_req (req_data)
           local query_data = {}

           cn:send("HTTP/1.1 200 OK\n\n", 
           function()
               cn:close()
               --collectgarbage()
           end)
      end)
end

-- Configure the ESP as a station (client)
wifi.setmode (wifi.SOFTAP)

cfg={}
cfg.ssid=SSID
cfg.pwd=SSID_PASSWORD
wifi.ap.config(cfg)

cfg={}
cfg.ip="192.168.1.1";
cfg.netmask="255.255.255.0";
cfg.gateway="192.168.1.1";
wifi.ap.setip(cfg)

print("Set up UART config")
uart.setup(1, 921600, 8, uart.PARITY_NONE, uart.STOPBITS_1, 1)

-- Create the httpd server
svr = net.createServer (net.TCP, 30)

-- Server listening on port 80, call connect function if a request is received
svr:listen (80, connect)

一旦这个程序在NodeMCU上运行,我将我的PC连接到NodeMCU,我用这段Java代码发送一些http POST请求:

public void sendPost(RGBWPixel[][] LedMatrix) throws Exception {

    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    System.out.println("Sending post");
    // add reuqest header
    con.setRequestMethod("POST");
    con.setRequestProperty("matrixValues", new String(convertTo1DCharArray(LedMatrix)));
    con.setDoOutput(true);


    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.flush();
    wr.close();

    con.getResponseCode();

}

obj 是与NodeMCU IP地址对应的URL连接。 与 matrixValues 对应的String总是有2050长度。

请注意,我将LUA srcipt减少到导致问题发生的最小函数。更确切地说,当我有cn:send()部分时会发生这种情况,但我不知道是否可以在不发送响应的情况下接收和处理请求,因为当我不运行请求时不会发送请求来自Java程序的con.getResponseCode()。我是http的初学者,所以我还不了解所有的协议。

以下是来自NodeMCU方面的输出,在ESPlorer中:

> dofile("init.lua");
Set up UART config
> Hello connect
POST / HTTP/1.1
matrixValues: 
Available memory :
38088

Available memory :
37032
Hello connect
POST / HTTP/1.1
matrixValues: 
Available memory :
37688

Available memory :
36664
Hello connect
POST / HTTP/1.1
matrixValues: 
Available memory :
37440

Available memory :
36264

经过几十次迭代后,发生这种情况并重新启动NodeMCU:

Hello connect
POST / HTTP/1.1
matrixValues: 
Available memory :
4680

Available memory :
3600
E:M 1584
PANIC: unprotected error in call to Lua API (init.lua:19: out of memory)

ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x40100000, len 26704, room 16 
tail 0
chksum 0x0c
load 0x3ffe8000, len 2184, room 8 
tail 0
chksum 0x9a
load 0x3ffe8888, len 136, room 8 
tail 0
chksum 0x44
csum 0x44

第19行对应于cn:send()的行。 我想我在LUA脚本中声明一些变量,函数或回调函数时做错了,堆栈直到没有更多内存... 此外,我不明白为什么有2个调用conn:on callback funtion(其中打印node.heap())仅用于1“Hello connect”。这就像第二个void http请求总是发送...

非常感谢你宝贵的时间和潜在的帮助,如果你到这篇文章的最后!

2 个答案:

答案 0 :(得分:0)

sck:send(data, fn)相当于sck:send(data); sck:on("sent", fn) 传递给:on()的闭包必须直接引用对象(explanations)。
使用回调函数的第一个参数而不是引用upvalue。

cn:send(
   "HTTP/1.1 200 OK\n\n", 
   function(s)  -- s here has the same value as cn
      s:close()
      --collectgarbage()
   end
)

答案 1 :(得分:0)

发送标题:

Connection: close

同时提出请求和回复。现代http服务器和客户端应用程序的默认设置是保持活动状态。当此标头出现时,另一方将在发送所有数据后明确关闭连接。连接关闭后,将释放内存。

相关问题