I have making a cool project. I have more or less finalized my code, which will go on a NodeMCU running Espruino. I have trouble to save this code on the Espruino. This code should do this on each boot: connect to wifi, declare all functions and variables. Then the read()
function should be run continuously.
As I see from https://www.espruino.com/Saving I have two options. I tried both.
- If I put
save()
at the end of the code, after I restart NodeMCU code continues to run from where it stopped, but this mean that NodeMCU is not connected to wifi. - If I put
E.setBootCode(init());
at the end of code and restart the NodeMCU code doesn't run anymore.
Does anybody know how to properly save code on Espruino so it connects to wifi and defines functions and variables each time it is powered on?
My code:
function init() {
var wifi = require("Wifi");
var http = require("http");
wifi.connect("ALHN-096D", {password:"7381491319"}, function(err){
console.log("connected? err=", err, "info=", wifi.getIP());
});
wifi.stopAP();
//NodeMCU.xx is converter to Espruino pins
I2C1.setup({ 'scl': NodeMCU.D2, // pin D4 (in Espruino firmware, different physical pin)
'sda': NodeMCU.D1, // pin D5 (in Espruino firmware, different physical pin)
bitrate: 100000
}); // set bitrate just in case Arduino is talking in a different bitrate
//function to sort and arrange data in normal order
function sort(data) {
//position cursor, points to position in data array
var position = 0;
//creates empty array, later strings will be appended
var string_arr = [];
//first while loop exits when pointer reads 255
while(data[position] != 255) {
//create empty string; important to have "" not just empty!
var string = "";
//second while loop stops when pointer reaches 44 -> in ASCII ","
while(data[position] != 44) {
//inserts last digit first, function converts decimal to string
string = String.fromCharCode(data[position]) + string;
//increments pointer
position++;
}
//increments pointer to position after the "," (44)
position++;
//pushes newly created string in to the array
string_arr.push(string);
}
return string_arr;
}
function sendToServer(sensor) {
http.get("https://xxxxxx.com/send?temp0="+ sensor[0] +"&temp1=" + sensor[1], function(res) {
res.on('data', function(serverData) {
console.log(serverData);
});
});
}
function read() {
//writes data received from Arduino
//I2C1.readFrom(<ID of device>, <number of bytes to receive>);
//ID of device is set in Arduino firmware
//ID in official docs is represented in hex 0x but works as decimal, need to be identical
var rawData = I2C1.readFrom(8, 20);
var sortedData = sort(rawData);
//console logs data
//sort function returns sorted string array with numbers in right order
console.log("Received ... " + rawData);
console.log("Reversing and sorting ... ");
console.log("Received sorted ... " + sortedData);
console.log("Reading5...");
sendToServer(sortedData);
}
//function calls anonymous function each second
setInterval(function() {
console.log("Reading...");
read();
}, 10000);
}
Output of this code:
Reading...
Received ... 49,56,49,44,49,49,57,44,44,255,255,255,255,255,255,255,255,255,255,255
Reversing and sorting ...
Received sorted ... 181,911,