You are reading a single comment by @michael_101 and its replies. Click here to read the full conversation.
  • Hello,

    I'm using MDBT42 flashed with the latest firmware (2v08) and I'm experiencing a strange behavior with http module.
    After everything initialized using require('SIM900') I got ~1300 free memory and then I started getting \posting data from\to a server every 30 seconds and after the first successful response and starting from the second request I'm getting this error but I'm still getting successful responses...

    Uncaught Error: 0, CLOSED already registered
     at line 1 col 44
    if(m[a])throw Error(a+" already registered");m[a]=d
                                               ^
    in function "registerLine" called from line 2 col 432
    ...;g[a]=void 0;l=!1;return""});else return g[a]=void 0,""
                                  ^
    in function "c" called from line 1 col 25
    e=void 0;var u;c&&(u=c(t))?(e=k,c=u):clearTimeout(h);void 0=...
                            ^
    in function "e" called from line 2 col 16
    k=!0);k||e&&e(h)}b=b.substr(a+1);if(k&&f)return d("");"\n"==...
                   ^
    in function called from system
    

    ... while the free memory decrease every request.

    FREE MEMORY - 851
    FREE MEMORY - 724
    FREE MEMORY - 713
    ...
    ...
    FREE MEMORY - 173
    FREE MEMORY - 165
    FREE MEMORY - 158
    FREE MEMORY - 149
    FREE MEMORY - 146
    FREE MEMORY - 143
    FREE MEMORY - 138
    FREE MEMORY - 134
    

    After a couple of minutes I got those errors:

    ERROR: Error processing Serial data handler - removing it.
    Execution Interrupted during event processing.
    New interpreter error: CALLBACK,MEMORY
    New interpreter error: BUFFER_FULL
    [
      "BUFFER_FULL",
      "CALLBACK",
      "LOW_MEMORY",
      "MEMORY"
     ]
    

    The issue is the same using GET and POST.

    Am I doing it wrong?
    Is there any better way to do this interval thing?

    Here is the full code for my trials.

    Thank you.

    let sim={};
    let m;
    
    function onInit(){
      E.setConsole("Bluetooth");
    
      setTimeout(() => {
        Serial1.setup(115200,{rx:D8,tx:D6});
      }, 1*1000);
    }
    
    function initGPRS(){
      setTimeout(()=>{
        sim.gprs = require('SIM900').connect(Serial1, null, (err)=>{
          if (err) throw err;
          cntGPRS((ip)=>{
            console.log(ip);
            console.log("Error Flags", E.getErrorFlags());
            m = process.memory().free;
            console.log("==========\r\nFREE MEMORY BEFORE STARTING REQUESTS:", m, "\r\n==========\r\n");
            setInterval(()=>{
              testGet1();
              //testGet2();
              //testPost();
            }, 30*1000);
          });
        });
      }, 5*1000);
    }
    
    function cntGPRS(cb){
      setTimeout(()=>{
        sim.gprs.connect('', '', '', (err)=>{
          if (err) throw err;
          setTimeout(()=>{
            sim.gprs.getIP((err, ip)=>{
              if (err) throw err;
              if (cb) cb(ip);
            });
          }, 5*1000);
        });
      }, 5*1000);
    }
    
    function testGet1(){
      if(!sim.http){
        sim.http = require("http");
      }
      try{
        sim.http.get('http://www.pur3.co.uk', (res)=> {
          let content = "";
          res.on('data', (d)=> {
            content+=d;
          });
          res.on('close', ()=>{
            //console.log(content);
            console.log("Got Response with statuscode:", res.statusCode);
            console.log("Error Flags", E.getErrorFlags());
            m = process.memory().free;
            console.log("==========\r\nFREE MEMORY - GET1", m, "\r\n==========\r\n");
          });
        }).on('error', (e)=>{
          console.log("ERROR:", e);
          console.log("After ERROR GET", m);
        });
      }catch(e){
        console.log("ERROR http.get:", e);
      }
    }
    
    function testGet2(){
      require("http").request({
        "host":"http://www.pur3.co.uk",
        "path":"/",
        "method":"GET",
      }, (res)=> {
        let content = "";
        res.on('data', (d)=>{ content+=d;});
        res.on('close', (d)=>{
          //console.log(content);
          console.log("Got Response with statuscode:", res.statusCode);
          console.log("Error Flags", E.getErrorFlags());
          m = process.memory().free;
          console.log("==========\r\nFREE MEMORY - GET2", m, "\r\n==========\r\n");
        });
      }).end();
    }
    
    function testPost(){
      postJSON({postURL:"http://www.pur3.co.uk", data: {d:7}, callback: (d)=>{
        if(d) console.log("Response:", d);
        console.log("Error Flags", E.getErrorFlags());
        m = process.memory().free;
        console.log("==========\r\nFREE MEMORY - POST", m, "\r\n==========\r\n");
      }});
    }
    
    function postJSON(params) {
      if(!sim.http){
        sim.http = require("http");
      }
      try{
        setTimeout(()=>{
          content = JSON.stringify(params.data);
          let options = url.parse(params.postURL);
          options.method = 'POST';
          options.headers = {
            "Content-Type":"application/json",
            "Content-Length":content.length
          };
          let req = sim.http.request(options, (res)=>{
            let content = "";
            res.on('data', (d)=>{ content+=d; });
            res.on('close', (d)=>{
              console.log("Got Response with statuscode:", res.statusCode);
              params.callback();
            });
          });
          req.on('error', (e)=>{ params.callback(e);});
          req.end(content);
        }, 5*1000);
      }catch(e){
        console.log(e);
      }
    }
    
About

Avatar for michael_101 @michael_101 started