You are reading a single comment by @Planer and its replies. Click here to read the full conversation.
  • 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;
      }
    }
    
    
About

Avatar for Planer @Planer started