Espruino 保存代码并开始初始化

Espruino save code and start on initialization

我正在做一个很酷的项目。我或多或少已经完成了我的代码,它将在 NodeMCU 运行ning Espruino 上运行。我无法在 Espruino 上保存此代码。此代码应在每次启动时执行此操作:连接到 wifi,声明所有函数和变量。那么read()函数应该是连续运行。

正如我从 https://www.espruino.com/Saving 看到的那样,我有两个选择。我都试过了。

有谁知道如何在 Espruino 上正确保存代码,以便它连接到 wifi 并在每次开机时定义函数和变量?

我的代码:

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);
}

此代码的输出:

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,

您最好的解决方案是将 init 函数重命名为 onInit,然后在上传后键入 save(),它就会神奇地开始工作。

您找到的页面 https://www.espruino.com/Saving 提到 onInit 在启动时自动调用。

你用 E.setBootCode(init()); 做的事情不会起作用,因为它正在执行一个字符串。您正在执行的是执行 init() 函数,然后将 该函数的 return 值放入 setBootCode .

你需要 E.setBootCode("init();"); - 但在这种情况下你真的应该只做第一个选项 - 使用 onInit