Memory out trouble

Posted on
  • Hi gyes. Recently write code of internet online clock. But my happiness was not long, because i had unresolved trouble for me. All of operativ memory for some time is went out, and code is stopping. Where could I make a mistake and how to fix it?
    p.s Intervals of lost all memory every time is different.
    all code:

    I2C1.setup({ scl: 5, sda: 4 });
    var wifi = require("Wifi");
    var esp8266 = require("ESP8266");
    
    var lcd;
    var masterPass = "q1w2e3r4";
    var serverStarted = false;
    var allDate = "";
    var yr = 999;
    var oldYr = 99;
    var mns = 999;
    var oldMns = 99;
    var dy = 999;
    var oldDy = 99;
    var hr = 999;
    var oldHr = 99;
    var min = 999;
    var oldMin = 99;
    var sec = 999;
    var oldSec = 99;
    
    //setup part--------------------------------------------------------------------------
    wifi.on('sta_joined', function (details) {
      if (!serverStarted) {
        serverStart();
        serverStarted = true;
      }
    });
    
    function getPage(text) {
      text = typeof text !== 'undefined' ? text : wifi.getStatus().station;
      return `<!doctype html>
      <html>
      <head>
          <title>System setup</title>
      </head>
      <body>
      <p style="text-align: center;">Status: `+ text + `</p>
      <form action="" method="post">
      <h2 style="text-align: center;">Setup configuration:</h2>
      <hr />
      <p style="text-align: center;"><span style="color:#FF0000;">Access pass:&nbsp;<input name="accpsName" required type="password" /></span></p>
      <p style="text-align: center;"><span style="color:#000000;">SSID name:&nbsp;<input name="ssidName" required type="text" /></span></p>
      <p style="text-align: center;"><span style="color:#000000;">SSID pass:&nbsp;<input name="passName" type="password" /></span></p>
      <p style="text-align: center;"><input type="submit" value="SEND" /></p>
      </form>
      <p style="text-align: center;">&nbsp;</p>
      </body>
      </html>`;
    }
    
    function parseData(data) {
      var arr = data.split("&");
      var result = {};
    
      arr.forEach(function (pair) {
        var elem = pair.split("=");
        result[elem[0]] = elem[1];
      });
      return result;
    }
    
    function serverStart() {
      function onPageRequest(req, res) {
        var reqData;
        if (req.method == "GET") {
          res.writeHead(200, { 'Content-Type': 'text/html' });
          res.end(getPage());
        } else {
          req.on("close", function () {
            reqData = req.read();
            reqData = parseData(reqData);
            if (reqData.accpsName == masterPass) {
              wifi.connect(reqData.ssidName, { password: reqData.passName }, function (ap) { });
              wifi.save();
              setTimeout(function () {
                esp8266.reboot();
              }, 5000);
            }
          });
          res.writeHead(200, { 'Content-Type': 'text/html' });
          res.end(getPage());
        }
      }
      require("http").createServer(onPageRequest).listen(8080);
    }
    
    //Inet time part-------------------------------------------------------------------------
    wifi.on('connected', function (details) {
      lcd = require("HD44780").connectI2C(I2C1, 0x27);
      if (!serverStarted) {
        serverStart();
        serverStarted = true;
      }
      /*if (wifi.getStatus().station != "connected") {
        lcd.clear();
        lcd.setCursor(0, 3);
        lcd.print("setup wifi connection");
      }*/
      setInterval(function () {
        try { getTimeOnline(); } catch (error) { }
        updateDisplay();
      }, 1000);
    });
    
    function getTimeOnline() {
      allDate = "";
      var options = {
        host: 'api.timezonedb.com',
        port: '80',
        path: '/v2/get-time-zone?format=json&by=zone&zone=Europe/Minsk&key=5CZHRPQ83HOC',
        method: 'GET',
        headers: { "Content-Type": "application/json" }
      };
      require("http").request(options, function (res) {
        res.on('data', function (partOfData) {
          allDate += partOfData;
        });
        res.on('close', function (data) {
          try {
            allDate = allDate.split('{');
            allDate = allDate[1].split('}');
            allDate = '{' + allDate[0] + '}';
            allDate = JSON.parse(allDate);
            displayTextParse(allDate.formatted);
          } catch (error) { }
        });
      }).end();
    }
    
    function displayTextParse(params) {
      var date = params.split("-");
      var temp = date[2].split(" ");
      date[2] = temp[0];
      var tme = temp[1];
      tme = tme.split(":");
    
      yr = date[0];
      mns = date[1];
      dy = date[2];
      hr = tme[0];
      min = tme[1];
      sec = tme[2];
    }
    
    function updateDisplay() {
      if (oldYr != yr) {
        lcd.setCursor(0, 1);
        lcd.print(yr);
        oldYr = yr;
        lcd.print("-");
      }
      if (oldMns != mns) {
        lcd.setCursor(5, 1);
        lcd.print(mns);
        oldMns = mns;
        lcd.print("-");
      }
      if (oldDy != dy) {
        lcd.setCursor(8, 1);
        lcd.print(dy);
        oldDy = dy;
        lcd.print(" ");
      }
      if (oldHr != hr) {
        lcd.setCursor(12, 1);
        lcd.print(hr);
        oldHr = hr;
        lcd.print(":");
      }
      if (oldMin != min) {
        lcd.setCursor(15, 1);
        lcd.print(min);
        oldMin = min;
        lcd.print(":");
      }
      if (oldSec != sec) {
        lcd.setCursor(18, 1);
        lcd.print(sec);
        oldSec = sec;
      }
    }
    
    
  • What is the error output your are seeing?

    What is the output of process.env ?

    The will show the free jsvars after the load.

    If you use the save on send option in the ide settings, the code will be stored in flash rather than ram and you might not run out of space then.

  • in error output i see:
    sometime this:
    ERROR: Error processing Serial data handler - removing it.
    ERROR: Error processing Serial data handler - removing it.
    Execution Interrupted during event processing.
    New interpreter error: CALLBACK,MEMORY

    sometime this:
    ERROR: Ctrl-C while processing interval - removing it.
    Execution Interrupted during event processing.
    Uncaught InternalError: Unable to create socket
    at line 130 col 10
    }).end();

         ^
    

    in function "getTimeOnline" called from line 103 col 21
    try { getTimeOnline(); } catch (error) { }

    process.env:
    "VERSION": "1v99",
    "GIT_COMMIT": "f0d66ba",
    "BOARD": "ESP8266_4MB",
    "FLASH": 0, "RAM": 81920,
    "SERIAL": "5ccf7fed-7f68",
    "CONSOLE": "Telnet",
    "MODULES": "Flash,Storage,net" ... "r,crypto,neopixel",
    "EXPTR": 1073643636 }

  • ohh -0nce a second you are going to the internet to update the time?

    Instead do that a lot less frequently - say once an hour - and then set the internal clock to the time, and use the internal clock as your time reference.

    What is probably happening is a new call to get the time is made, when the last has not finished.

  • I had similar problem:

    Execution Interrupted during event processing.
    New interpreter error: MEMORY
    ERROR: Ctrl-C while processing interval - removing it.
    Execution Interrupted during event processing.
    New interpreter error: CALLBACK
    

    I send DHT22 sensor data to backend endpoint every second using http post requests.
    I think I needed 7+-20 of them to show up to cause it this to happen.

    Extending setInterval() from 1000 to 5000 did work.

    I think the memory stores the http request until it receives callback or expires at some point, and sending too many in the same time causes CALLBACK MEMORY to run out of storage space :)

    You need to find the right interval time so that the "callback memory" stack had data flow instead eventually run out of space.

    http request connects to server, closes connection, waits for the response, and when it receives one, the whole thing ends and memory stack is being free from this http request, so in the meantime memory stores the request.

    If you sent 50 requests and none gets response, you have 50 requests waiting for responses which causes memory run out of space to store more.

    If you need the data immediately, please consider web sockets instead of http requests.

    web socket opens connection and never closes it, in the meantime data flows non stop. it is used for chats but any other use when data flow is needed and considerably web socket would use less data transfer than http request is a good choice.

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

Memory out trouble

Posted by Avatar for Planer @Planer

Actions