Sorry, I need to make it a little more clear. It actually doesn't start without errors on the next boot. In fact, most of the time, a power-cycle will not cause things to work. Every now and then however, IT DOES. This happens so unpredictably that I have not been able to nail down any repeatable behavior, such as a minimum amount of down time.
Now, originally, I did have the wifi.on('connected' event inside the onInit() function. Then in an attempt to simplify things, I moved it out. I just put that event handler back in the onInit() on the assumption that it needed to be registered again after a reboot.
No such luck... sometimes things work, sometime not after a boot. But the one thing I have not ever seen fail is proper operation after typing save() on the left, even if there have been absolutely no changes to the code.
If it will help, here is the entirety of the code in question:
var wifi = require("Wifi");
var bme;
var mqtt;
var temp;
var pressure;
var humidity;
function onInit() {
wifi.startAP('AgilatechESP', { password: 'xxxxxxxx', authMode: 'wpa2' }, (err) => {
if (err) throw err;
else wifi.connect("HeartJS", {password : "xxxxxxxxxx"}, (err) => {
if (err) throw err;
});
});
SPI1.setup({mosi:A7, miso:A6, sck:A5});
bme = require("BME280").connectSPI(SPI1, B1);
wifi.on('connected', () => {
require("http").createServer(pageReq).listen(8080);
mqtt = require("MQTT").connect({
host: "xxx.xxx.xxx.xxx"
});
loadData();
dataInterval();
});
}
function pageReq(req, res) {
const reqdat = url.parse(req.url, true);
var ret = "";
if (reqdat.path === '/') {
ret = `Temperature: ${tempF}, Pressure: ${pressure}, Humidity: ${humidity}`;
//ret = "Hello World";
}
res.writeHead(200);
res.end(ret);
}
function seaLevelPressure(pressure_mb, elevation, temp) {
return pressure_mb * Math.pow((1 - ((0.0065 * elevation) / (temp + 0.0065 * elevation + 273.15))), -5.257);
}
function dataInterval() {
var timer = setInterval( () => {
loadData();
}, 600000);
}
function loadData() {
let data = bme.getData();
data.pressure = seaLevelPressure(data.pressure, 1750, data.temp);
pressure = Math.round(data.pressure * 100) / 100;
temp = Math.round(data.temp * 10) / 10;
tempF = Math.round(((data.temp * 9/5) + 32) * 10) / 10;
humidity = Math.round(data.humidity * 10) / 10;
}
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.
Sorry, I need to make it a little more clear. It actually doesn't start without errors on the next boot. In fact, most of the time, a power-cycle will not cause things to work. Every now and then however, IT DOES. This happens so unpredictably that I have not been able to nail down any repeatable behavior, such as a minimum amount of down time.
Now, originally, I did have the wifi.on('connected' event inside the onInit() function. Then in an attempt to simplify things, I moved it out. I just put that event handler back in the onInit() on the assumption that it needed to be registered again after a reboot.
No such luck... sometimes things work, sometime not after a boot. But the one thing I have not ever seen fail is proper operation after typing save() on the left, even if there have been absolutely no changes to the code.
If it will help, here is the entirety of the code in question: