While you upload the code, some initializations happen which do not when you run the saved state after those.
Try to put the 'connectsinto anonInit()` function and it should work:
var dht = null, dhtMod = require("DHT11");
var WebSocket = require("ws");
var wifi = require("Wifi");
var WIFI_NAME = "MY WIFI NAME";
var WIFI_OPTIONS = { password : "MY WIFI PASSWORD" };
function onInit() {
dht = dhtMod.connect(D4);
wifi.connect(WIFI_NAME, WIFI_OPTIONS, function(err) {
if (err) {
console.log("Connection error: "+err);
return;
}
console.log("Connected!");
var host = "MY IP";
var ws = new WebSocket(host,
{
path: '/',
port: 80, // default is 80
protocol : "echo-protocol", // websocket protocol name (default is none)
protocolVersion: 13, // websocket protocol version, default is 13
origin: 'Espruino',
keepAlive: 600,
(default is none)
}
);
ws.on('open', function() {
console.log("Connected to server");
setInterval(function(){
dht.read(function (a) {
ws.send('{"temp":"'+a.temp.toString()+'","rh":"'+a.rh.toString()+'"}');
});
},10000);
});
});
} // /onInit()
If you now 'just' upload, nothing happens...
To get it running, enter onInit() in the console - left side - of the Espruino IDE.
When the code works as you expect, upload again, and then enter save() in the console - left side - of the Espruino IDE. This will save the code and run the onInit() and you should be fine. Any power cycle after that will run onInit() and do what you expect.
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.
While you upload the code, some initializations happen which do not when you run the saved state after those.
Try to put the 'connects
into an
onInit()
` function and it should work:If you now 'just' upload, nothing happens...
To get it running, enter
onInit()
in the console - left side - of the Espruino IDE.When the code works as you expect, upload again, and then enter
save()
in the console - left side - of the Espruino IDE. This will save the code and run theonInit()
and you should be fine. Any power cycle after that will runonInit()
and do what you expect.