有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java 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上运行,我就用WiFi将我的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地址对应的URLconnection。 与矩阵值对应的字符串的长度始终为2050

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

以下是ESPlorer中NodeMCU端的输出外观:

> 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脚本中声明了一些变量、函数或回调函数时出错了,直到没有更多内存为止。。。 另外,我不明白为什么只有1个“Hello connect”就有2个对conn:on回调函数(其中打印了node.heap())的调用。这就像总是发送第二个无效http请求

非常感谢您宝贵的时间和您潜在的帮助,如果您来到这篇文章的结尾

保罗


共 (2) 个答案

  1. # 1 楼答案

    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
    )
    
  2. # 2 楼答案

    发送标题:

    Connection: close
    

    既有请求也有响应。现代http服务器和客户端应用程序的默认设置是保持活动状态。当存在此标头时,另一端将在发送所有数据后显式关闭连接。当连接关闭时,内存被释放