title: NodeMCU : Available Heaps size falls until out of memory error
body: <p>I am facing some troubles with my NodeMCU, running a simple server in access point mode, programmed in LUA using ESPlorer.</p>
  
  <p>Here is the LUA script :</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Once this program is running on NodeMCU, I connect my PC in WiFi to the NodeMCU, and I send it some http POST requests with this piece of Java code :</p>
  
  <pre><code>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();
  
  }
  </code></pre>
  
  <p><em>obj</em> is the URLconnection corresponding to the NodeMCU IP adress.
  The String corresponding to <em>matrixValues</em> always has a 2050 length.</p>
  
  <p>Please notice that I reduced the LUA srcipt to the minimal functions that make the problem happend. More exactly, it happens when I had the cn:send() part, but I don't know if it is possible to receive and process the request without sending a response, because the request is not sent when I don't run the con.getResponseCode() from the Java program. I am a beginner with http, so I don't understand all the protocols yet.</p>
  
  <p>Here is how the output looks like from the NodeMCU side, in ESPlorer :</p>
  
  <pre><code>&gt; dofile("init.lua");
  Set up UART config
  &gt; 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
  </code></pre>
  
  <p>And after a few dozens of iterations, this happend and the NodeMCU restarts :</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Line 19 corresponds to the line of cn:send().
  I guess I made something wrong declaring some variables, functions or callback funtions in the LUA script, that stack until there is no more memory...
  Also, I don't understand why there is 2 calls to the conn:on callback funtion (where node.heap() is printed) for only 1 "Hello connect". It is like a second void http request is always sent...</p>
  
  <p>Thank you very much for your precious time and your potential help, if you came to the end of this post!</p>
  
  <p>Paul</p>