Free heap started out at abut 9600... went down to 7200, then backup and circling around 8800, and then, on the 50th - server side - cycle and on the 33rd - client/browser side cycle (pull data) - it crashed... with heap still 7720... and after crashing - without touching - it keeps crashing.... and crashing... and crashing,... about every minute once... with same message.
The code as below can be run in any environment with isEspruino = true / false; set accordingly for Espruino-Wifi (or Espruino with a serially connected ESP8266 for Wifi0) and for ESP8266 (Espruino on ESP8266). The sensor (isChirp) is set to false and thus is not talked to... only the counting of cycles happens and all Espruino/ESP8266/server-side and data-pulling/browser/client-side mechanics. Browser url: <ipAddress>/info.html, and setting on page: Pause unchecked. Eventhough IP address is not available (anymore) when asked for after connect w/ hostname set, a bit later it can be asked for in the console with wifi.getIP().
The code I'm running now is this (with the anomaly that after setting the hostname, I get weird result w/ wifi.getIP(...): ip: 0.0.0.0 - and mentioned ips specific to my wireless LAN):
// SoilWifiPullTest.js
// (c)20181029 allObjects
// for ip, turn log on and check.
// http://192.168.0.119/info.html EonESP
// http://192.168.0.121/info.html EWifi
var lg = true;
var sid = "StaWestRow3";
var isChirp = false;
var isEspruino = false;
var hostname = "westrow3";
var webServerId = "2"; // "1"=module | "2"=RYO
function log() { if (lg) console.log.apply(console,arguments); }
var wifiCreds = require("wifiCreds");
var wifi = require("Wifi"); // EWifi, EonE
function initWifi(cb) {
wifi.setHostname(hostname,function(err){
if (err) {
log(`error on setHostname: ${err}`);
} else {
log(`Hostname ${hostname} set.`);
log(`Connecting ${hostname} to wifi...`);
wifi.connect(wifiCreds.ssid
, {password:wifiCreds.pw}, function(err) {
if (err) {
log(`error on connecting ${hostname}: ${err}`);
wifi.disconnect();
} else {
log(`Connected ${hostname} to wifi.`);
wifi.getIP(function(err,ip){
if (err) { // ...ever in ESP8266?
log(`error on getIP for ${hostname}: ${err}`);
wifi.disconnect();
} else {
if (ip.ip == "0.0.0.0") {
log("Espruino on ESP8266");
wifi.stopAP();
ip = wifi.getIP();
}
log(ip);
log(process.env);
log(process.memory());
cb();
}
});
}
});
}
});
}
var webServer, WebServer;
function initWebServer1() {
WebServer = require("WebServer");
webServer = new WebServer(
{ port: 80
, default_type: 'text/plain'
, default_index: 'index.html'
, memory:
{ 'info.html': { 'content': info_html, 'type': 'text/html' }
, 'info.txt': { 'cowestrow3ntent': 'Hello World!' }
, 'info.njs': { 'content': info_njs } // content from function return
}
});
webServer.on('start', function (WebServer) {
// log('WebServer listening on port ' + WebServer.port);
});
webServer.on('request', function (request, response, parsedUrl, WebServer) {
// log('WebServer requested', parsedUrl);
});
webServer.on('error', function (error, WebServer) {
// log('WebServer error', error);
});
webServer.createServer();
}
var webServer2created, webServer2listening, httpMod;
function onPageRequest(req, res) {
var urlObj = url.parse(req.url, true), resObj;
log(urlObj);
switch (urlObj.pathname) {
case "/info.html": res.writeHead(200, {'Content-Type': 'text/html'});
res.end(info_html); break;
case "/info.njs" : resObj = info_njs();
res.writeHead(200, {'Content-Type': resObj.type });
res.end(resObj.content); break;
default: res.writeHead(404, {'Content-Type': 'text/html'});
res.end("");
}
}
function initWebServer2() {
httpMod = require("http");
webServer2created = httpMod.createServer(onPageRequest);
webServer2listening = webServer2created.listen(80);
}
var iidM, recM = { sid:sid, mid:0, tim:0, lite:0, temp:0, moist: 0};
var i2cM, sclM, sdaM;
if (isEspruino) {
i2cM = I2C2; sclM = B10; sdaM = B3; // hw I2C EwE
} else {
i2cM = null; sclM = D13; sdaM = D12; // sw I2C EonE
}
function initSensorM() {
i2cM = i2cM || new I2C();
i2cM.setup({scl:sclM, sda:sdaM});
}
function getLite(){ // takes ...3[s] to acquire
i2cM.writeTo(0x20,0x03);
setTimeout(function(){
i2cM.writeTo(0x20,0x04);
var d = i2cM.readFrom(0x20,2);
recM.lite = d[0]*256 + d[1];
},3000);
}
function getTemp(){
i2cM.writeTo(0x20,0x05);
var d = i2cM.readFrom(0x20,2);
recM.temp = d[0]*256 + d[1];
}
function getMoist(){
i2cM.writeTo(0x20,0x00);
var d = i2cM.readFrom(0x20,2);
recM.moist = d[0]*256 + d[1];
}
function getMSensorVals() { // done at ~:
recM.mid++; recM.tim = Math.floor(getTime());
if (isChirp) {
i2cM.writeTo(0x20,0x06); // H+00[s] reset the sensor
setTimeout(getLite ,1000); // H+01..04 (timed read 3[s] after cmd)
setTimeout(getTemp ,6000); // H+06..
setTimeout(getMoist,8000); // H+08..
} // H+09 log will be issued
}
function cycle() {
log(require('ESP8266').getState());
getMSensorVals();
setTimeout(function(){ log(recM); },9000);
}
function startSensing() {
if (isChirp) initSensorM();
cycle();
iidM = setInterval(cycle,10000);
}
function onInit() {
initWifi(function(){
if (webServerId == "1") {
initWebServer1();
} else {
initWebServer2();
}
startSensing();
});
}
// static and dynamic Web resources:
var info_html = `
<html>
<!-- info.html -->
<head>
<title>Info</title>
<script>
var baseUrl = "";
var reloadInt = 15; // reload interval in seconds
var tid, datTim = "...", cnt = 0, paused = true;
function wait(remaining) {
tid = null;
if (remaining > 0) {
document.getElementById("countDown").innerHTML
= ""+cnt+". "+datTim+" : Reloading in "+remaining+" seconds.";
tid = setTimeout(wait,1000,remaining - 1);
} else {
if (paused) {
pause();
} else {
cnt++
document.getElementById("countDown").innerHTML
= ""+cnt+". Loading...";
document.getElementById("data").src = baseUrl+"/info.njs";
wait(reloadInt);
}
}
}
function pause() {
if (tid) { clearInterval(tid); tid = false; }
paused = true;
document.getElementById("pauseChkBox").checked = true;
document.getElementById("countDown").innerHTML = "Paused";
}
function resume() {
paused = false;
document.getElementById("pauseChkBox").checked = false;
wait(0);
}
function setIframeDocument(iframeDocument) {
top.iframeDocument = iframeDocument;
var info = iframeDocument.getElementById("info").innerHTML;
var recM = JSON.parse(info);
datTim = new Date(recM.tim * 1000).toISOString()
datTim = datTim.substr(0,19).replace("T"," ");
// ...and some other code using it
}
</script>
</head>
<body onload="wait(0);">
<h3>Info</h3>
<div><p>
<input id="pauseChkBox" type="checkbox"
onclick="if (this.checked) { pause(); } else { resume(); }"
> Pause
</p></div>
<div><p><span id="countDown"></span><p></div>
<div><iframe id="data" style="width:100%;"></iframe></div>
</body>
</html>
`;
function info_njs() {
return {'type': 'text/html', 'content': `
<html><body><div id="info">${JSON.stringify(recM)}</div>
<script>if (top.setIframeDocument) top.setIframeDocument(document);
</script></body></html>
` };
}
setTimeout(onInit,1000);
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Free heap started out at abut 9600... went down to 7200, then backup and circling around 8800, and then, on the 50th - server side - cycle and on the 33rd - client/browser side cycle (pull data) - it crashed... with heap still 7720... and after crashing - without touching - it keeps crashing.... and crashing... and crashing,... about every minute once... with same message.
The code as below can be run in any environment with
isEspruino = true / false;
set accordingly for Espruino-Wifi (or Espruino with a serially connected ESP8266 for Wifi0) and for ESP8266 (Espruino on ESP8266). The sensor (isChirp) is set to false and thus is not talked to... only the counting of cycles happens and all Espruino/ESP8266/server-side and data-pulling/browser/client-side mechanics. Browser url:<ipAddress>/info.html
, and setting on page: Pauseunchecked
. Eventhough IP address is not available (anymore) when asked for after connect w/ hostname set, a bit later it can be asked for in the console withwifi.getIP()
.The code I'm running now is this (with the anomaly that after setting the hostname, I get weird result w/
wifi.getIP(...)
:ip: 0.0.0.0
- and mentioned ips specific to my wireless LAN):