Chat freely about the open source Javascript projects for ESP8266

User avatar
By dgustavus
#32464 I am using an Adafruit Huzzah (esp-12) and the 10_25 version of espruino. The simple code below works from the console, but if I run the setInterval command which is commented out at the bottom, it will fail after several hundred iterations running out of memory:

Code: Select all// esp8266 test net connection

var ssid = "<SSID>";
var pass = "<PASSWORD>";
var serverip = "192.168.xxx.xxx";

// network connection
var wifi = require("wifi");
var net = require("net");
var ipInfo = ESP8266WiFi.getIPInfo();
print("IP addr: " + ESP8266WiFi.getAddressAsString(ipInfo.ip));

// update sample and send new data
function sample() {
    var skt = net.connect({host: serverip, port: 1234}, function() {
    skt.write(getTime().toString()+"\n");
    skt.end();
    console.log(process.memory());
  });
}

//setInterval('sample();', 5000);


The process.memory() function returns the same values hundreds of times, then begins to increment used memory:

---results from console.log()---
--several hundred lines like the two immediately below:
{ "free": 821, "usage": 202, "total": 1023, "history": 141 }
{ "free": 821, "usage": 202, "total": 1023, "history": 141 }
Uncaught Error: Function "write" not found!
at line 3 col 14
skt.write(getTime().toString()+"\n");
^
in function called from system
WARNING: Prototype is not an Object, so setting it to {}
{ "free": 803, "usage": 220, "total": 1023, "history": 141 }
{ "free": 753, "usage": 270, "total": 1023, "history": 141 }
{ "free": 703, "usage": 320, "total": 1023, "history": 141 }
{ "free": 653, "usage": 370, "total": 1023, "history": 141 }
{ "free": 603, "usage": 420, "total": 1023, "history": 141 }
{ "free": 553, "usage": 470, "total": 1023, "history": 141 }
{ "free": 503, "usage": 520, "total": 1023, "history": 141 }
{ "free": 453, "usage": 570, "total": 1023, "history": 141 }
{ "free": 403, "usage": 620, "total": 1023, "history": 141 }
ERROR: Unable to create socket

I am learning javascript and this is very possibly due to a programming or conceptual error. I cannot find any information about a useful return code from the net.connect() function, but it seems that at some point it cannot create a socket. Any thoughts of what is happening would surely be instructive.

Thanks, Duane
User avatar
By Solid
#33537 Try something like this, defining a variable several hundred times with limited resources will lead to memory problems.

Code: Select all// esp8266 test net connection

var ssid = "<SSID>";
var pass = "<PASSWORD>";
var serverip = "192.168.xxx.xxx";

// network connection
var wifi = require("wifi");
var net = require("net");
var ipInfo = ESP8266WiFi.getIPInfo();
print("IP addr: " + ESP8266WiFi.getAddressAsString(ipInfo.ip));

var skt;

// update sample and send new data
function sample() {
  skt = net.connect({ host: serverip, port: 1234 }, function() {
    skt.write(getTime().toString()+"\n");
    skt.end();
    console.log(process.memory());
  });
}

setInterval(sample, 5000);
User avatar
By dgustavus
#33781 Thanks Solid for the reply! Being new to javascript I had assumed that the scope of the skt variable was the sample() function, and it would be "cleaner" to do it that way. After reading a bit more, it appears the the setInterval() function keeps any variable declared in the sample() function around until it exits (never!) or runs out of memory. I decided to declare and open the socket in global space, then reduce sample function to nothing more than writing the current clock time to the socket. I left that running all night, and it was fine this morning.

I appreciate you taking the time to point me in the right direction!
Duane