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

    I have some memory problems with a js code.

    I connect to wifi, resolve an ip addr, connect to onewire, get the ds18s20 sensor data, and every minutes it sends the data to thingspeak.

    For a while (what is sometimes 5-10 horus, sometimes less), I got this (it disconnect from espruino ide, and I need to reconnect):

    The response connection closed
    connection close
    { "free": 107, "usage": 916, "total": 1023, "history": 1 }
    >
    Disconnected
    >
    Connected
    >)ü
    =undefined
    >
    =undefined
    current temp:25.5
    sendTsStart
    { "free": 213, "usage": 810, "total": 1023, "history": 17 }
    ERROR: Out of Memory!
    WARNING: Unable to create string as not enough memory
    WARNING: Out of memory while appending to array
    Execution Interrupted
    current temp:25.5
    sendTsStart
    { "free": 212, "usage": 811, "total": 1023, "history": 1 }
    WARNING: Unable to create string as not enough memory
    WARNING: Out of memory while appending to array
    Execution Interrupted
    

    Here is the code:

    require("ESP8266").logDebug(false);
    
    var ssid = "ssid";
    var pass = "pass";
    
    var ApiKey = 'apikey';
    
    
    var peri = 60; //period of reading sensor in sec.
    
    var iotHostName="thingspeak.com";
    var iotIpAddr="";
    
    
    var GPIO0 = new Pin(12); // GPIO0 | ~D3 port
    var ow = new OneWire(GPIO0);
    var sensor = require("DS18S20").connect(ow);
    
    var GPIO2 = new Pin(2); // internal led
    var toggle=1;
    
    var startBlink = function()
    {
      print("startBlink");
      setInterval(function() {
        toggle=!toggle;
          digitalWrite(GPIO2, toggle);
        }, 500);
    };
    
    var connectToWifi = function() {
      console.log('connecting to wifi');
      var GPIO2 = new Pin(2);
    
      digitalWrite(GPIO2, 1);
      ESP8266WiFi.init();
      ESP8266WiFi.connect(ssid, pass, function() {
        digitalWrite(GPIO2, 0);
        console.log('connected to wifi');
        var ipInfo = ESP8266WiFi.getIPInfo();
        print("Current IP address is: " +
          ESP8266WiFi.getAddressAsString(ipInfo.ip­) + ':' +
          ESP8266WiFi.getAddressAsString(ipInfo.ne­tmask));
    
        resolveIotAddress(repeat);
    
      });
    };
    
    var resolveIotAddress= function(callback)
    {
      var ESP8266 = require("ESP8266");
      ESP8266.getHostByName(iotHostName, function(address) {
    
        iotIpAddr = ESP8266.getAddressAsString(address);
        console.log('resolved:' + iotIpAddr);
    
        startBlink();
    
        callback();
    
      });
    
    };
    
    var sendTs = function(temp) {
        console.log('sendTsStart');
        console.log(process.memory());
    
        var http = require("http");
        http.get({
            host: iotIpAddr,
            port: 80,
            headers: {
              'Host': 'tingspeak.com',
            },
            path: '/update?key=' + ApiKey + '&field1=' + temp
          },
          function(response) {
            print("get callback!");
            response.on('data', function(data) {
              console.log('connection getdata:');
              console.log(process.memory());
              print(data);
            });
            response.on('close', function() {
              print("The response connection closed");
              console.log('connection close');
              console.log(process.memory());
    
            });
          }
        );
    
    };
    
    var sendTemperature = function(temp)
    {
      console.log('current temp:' + temp);
    //  var field2 = 1 + Math.floor(Math.random() * 40);
      sendTs(temp);
    };
    
    repeat = function() {
      //var field1 = 1 + Math.floor(Math.random() * 40);
     // var field2 = 1 + Math.floor(Math.random() * 40);
    //  sendTs(field1, field2);
    
      sensor.getTemp(sendTemperature);
    
      setTimeout(repeat, peri * 1000);
    };
    
    
    E.on('init', function() {
      connectToWifi();
    });
    
    

    The smallest value what I get, is the response in the sendTs function in respons.on data part:

    { "free": 17, "usage": 1006, "total": 1023, "history": 1 }
    1331

    I used a changed ds18b20 library to support ds18s20. I send it in the next post.

    thanks,
    HyGy

About

Avatar for hygy @hygy started