ESP8266 NodeMCU หน่วยความจำไม่เพียงพอ

ข้อมูล NodeMCU

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

ฉันได้รับข้อผิดพลาดเมื่อฉันพยายามส่งการตอบสนอง HTTP ด้วยการตอบสนองสตริง json ขนาดใหญ่ (json_response)

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)

person Mohamed Embaby    schedule 10.08.2018    source แหล่งที่มา
comment
ต้องการข้อเสนอแนะเพิ่มเติมใด ๆ ที่นี่?   -  person Marcel Stör    schedule 23.11.2018
comment
@ MarcelStör ฉันเห็นว่ามีวิธีปรับหน่วยความจำเพื่อจัดการมันสักครั้งหรือไม่ แต่ฉันเดาว่ามันยังเป็นไปไม่ได้   -  person Mohamed Embaby    schedule 23.11.2018


คำตอบ (1)


ใช่ มันจะไม่ทำงานหากคุณพยายามส่งข้อมูลจำนวนมาก คุณต้องส่งชิ้นนี้ทีละชิ้น เอกสารประกอบ API ของเรา แสดงสองแนวทาง (คุณจะพบเพิ่มเติม การอ้างอิงที่นี่ใน 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)
person Marcel Stör    schedule 10.08.2018