ESP32 DevKit WIFI+BLE WebServer POST problem

Posted on
  • Hello everyone!
    I'm using ESP32 DevKit with espruino_2v00_esp32
    Flashed:
    python "../../esptool/esptool.py" --chip esp32 --port COM6 --baud 921600 --after hard_reset write_flash-z --flash_mode dio --flash_freq 40m --flash_size detect 0x1000 bootloader.bin 0x8000 partitions_espruino.bin 0x10000 espruino_esp32.bin

    ESP on startup connects to my local Wifi and create Web Server that serves web page. I can connect to this Web Server and get web page without any problem. JS on page generates every 2 seconds GET request and receives JSON string. All OK. But when I'm starting send POST request after several such requests Web Server not responses anymore . Also I noticed that after every POST increases memory usage.
    So I need your help... Is it my code or something else?

    Console Log:

    ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v00 (c) 2018 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    >Initialization...
    Starting...
    Connecting to WiFi...
    Connect to http://10.10.10.100
    WARNING: setsockopt(SO_REUSPORT) failed
    >process.memory(); // <--- After code upload
    ={ free: 2174, usage: 626, total: 2800, history: 601,
      gc: 0, gctime: 2.607 }
    >process.memory(); // <--- After connection to web server
    ={ free: 2134, usage: 666, total: 2800, history: 604,
      gc: 0, gctime: 2.634 }
    {"mode":0}
    >process.memory(); // <--- POST received
    ={ free: 2037, usage: 763, total: 2800, history: 604,
      gc: 0, gctime: 2.727 }
    {"mode":1}
    >process.memory(); // <--- POST received
    ={ free: 1940, usage: 860, total: 2800, history: 604,
      gc: 0, gctime: 2.989 }
    {"mode":0}
    >process.memory(); // <--- POST received
    ={ free: 1844, usage: 956, total: 2800, history: 604,
      gc: 0, gctime: 2.88 }
    {"mode":3}
    >process.memory(); // <--- POST received
    ={ free: 1746, usage: 1054, total: 2800, history: 604,
      gc: 0, gctime: 2.962 }
    {"mode":4}
    >process.memory(); // <--- POST received
    ={ free: 1650, usage: 1150, total: 2800, history: 604,
      gc: 0, gctime: 3.037 }
    {"mode":4}
    >process.memory(); // <--- POST received
    ={ free: 1552, usage: 1248, total: 2800, history: 604,
      gc: 0, gctime: 3.13 }
    >process.memory(); // <--- POST not received / No response
    ={ free: 1552, usage: 1248, total: 2800, history: 604,
      gc: 0, gctime: 3.126 }
    >process.memory();
    ={ free: 1552, usage: 1248, total: 2800, history: 604,
      gc: 0, gctime: 3.126 }
    >E.getErrorFlags();
    =[  ]
    >console.log(mode);
    0
    =undefined
    
    // Button pressed --->  [ CRASH ] ---> :(
    
    >/mnt/c/Users/rhys/esp32/EspruinoBuildTo­ols/esp32/build/esp-idf/components/freer­tos/./queue.c:1442 (xQueueGenericReceive)- assert failed!
    abort() was called at PC 0x4008a168 on core 0
    Backtrace: 0x4008c743:0x3ffdbf90 0x4008c76f:0x3ffdbfb0 0x4008a168:0x3ffdbfd0 0x40107bda:0x3ffdc010 0x40109f04:0x3ffdc030 0x4010a253:0x3ffdc050 0x4010b536:0x3ffdc0a0 0x40136075:0x3ffdc120 0x401af1b1:0x3ffdc150 0x40135229:0x3ffdc170 0x401597fa:0x3ffdc1b0 0x4012e394:0x3ffdc210 0x4012999f:0x3ffdc230 0x40129b05:0x3ffdc2a0 0x4014a845:0x3ffdc2c0
    Rebooting...
    ets Jun  8 2016 00:22:57
    rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
    configsip: 0, SPIWP:0xee
    clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_d­rv:0x00,hd_drv:0x00,wp_drv:0x00
    mode:DIO, clock div:2
    load:0x3fff0018,len:4
    load:0x3fff001c,len:2364
    load:0x40078000,len:0
    load:0x40078000,len:10880
    entry 0x40078c4c
    E (29) boot: ota data partition invalid, falling back to factory
    Loading 1654 bytes from flash...
    WARNING: setsockopt(SO_REUSPORT) failed
    > 
    
  • Upd: I've tried this example and result the same. Each POST increases memory usage and then Web Server not responding.

    Google Chrome:

    This page isn’t working 10.10.10.100 didn’t send any data.
    ERR_EMPTY_RESPONSE

     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v00 (c) 2018 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    >
    Connected!
    Connect to http://10.10.10.100
    WARNING: setsockopt(SO_REUSPORT) failed
    {
      "mytext": "Testing"
     }
    We got sent the text  Testing
    >process.memory();
    ={ free: 2172, usage: 428, total: 2600, history: 215,
      gc: 0, gctime: 2.224 }
    {
      "mytext": "Testing"
     }
    We got sent the text  Testing
    >
    >process.memory();
    ={ free: 2015, usage: 585, total: 2600, history: 218,
      gc: 0, gctime: 2.324 }
    {
      "mytext": "Testing"
     }
    We got sent the text  Testing
    >process.memory();
    ={ free: 1858, usage: 742, total: 2600, history: 218,
      gc: 0, gctime: 2.419 }
    {
      "mytext": "Testing"
     }
    We got sent the text  Testing
    >process.memory();
    ={ free: 1701, usage: 899, total: 2600, history: 218,
      gc: 0, gctime: 2.52 }
    {
      "mytext": "Testing"
     }
    We got sent the text  Testing
    >process.memory();
    ={ free: 1544, usage: 1056, total: 2600, history: 218,
      gc: 0, gctime: 2.637 }
    {
      "mytext": "Testing"
     }
    We got sent the text  Testing
    >process.memory();
    ={ free: 1387, usage: 1213, total: 2600, history: 218,
      gc: 0, gctime: 2.863 }
    {
      "mytext": "Testing"
     }
    We got sent the text  Testing
    >process.memory();
    ={ free: 1230, usage: 1370, total: 2600, history: 218,
      gc: 0, gctime: 2.822 }
    {
      "mytext": "Testing"
     }
    We got sent the text  Testing
    >process.memory();
    ={ free: 1073, usage: 1527, total: 2600, history: 218,
      gc: 0, gctime: 2.935 }
    > 
    
  • Can you post your code?

  • Wilberforce
    Can you post your code?

    I've found what is the source of problem. If you don't put callback function (as in example below) into POST handler your memory usage increases with every POST request and program after a while crashed. With such callback everything works now, I think :)

    // This handles any received data from the POST request
    function handlePOST(req, callback) {
      var data = "";
      req.on('data', function(d) { data += d; });
      req.on('end', function() {
        // All data received from the client, so handle the url encoded data we got
        // If we used 'close' then the HTTP request would have been closed and we
        // would be unable to send the result page.
        postData = {};
        data.split("&").forEach(function(el) {
          var els = el.split("=");
          postData[els[0]] = decodeURIComponent(els[1]);
        });
        // finally our data is in postData
        console.log(postData);
        // do stuff with it!
        console.log("We got sent the text ", postData.mytext);
        digitalWrite(LED1, postData.led1);
        digitalWrite(LED2, postData.led2);
        // call our callback (to send the HTML result)
        callback();
      });
    }
    
  • Could you post your entire code?

    There's a high likelihood that you're just not closing the POST request with .end() - it's then left open, eating up memory space until memory runs out.

  • Sorry, I can't post that code. I have deleted it. I believe that I didn't use .end(). So it can be the reason.
    Though there is a part of new code that works fine now:

    // On POST Request Handler
    function onPOST(req, res) {
      let data = '';
      req.on('data', d => data += d);
      req.on('end', () => {
        try {
          let obj = JSON.parse(data);
          mod = obj.mode;
          res.writeHead(200, { 'Content-Type': 'text/plain' });
          res.end('Mode Set');
        } catch (err) {
          res.writeHead(200, { 'Content-Type': 'text/plain' });
          res.end(`Error: ${err}`);
        }
      });
    }
    
  • Yes - not calling end would do that!

  • I've tested some code without .end(). It eats memory and crashes like I described above.
    Gordon, Wilberforce thank you for help!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

ESP32 DevKit WIFI+BLE WebServer POST problem

Posted by Avatar for Engineer @Engineer

Actions