Espruino保存代码并开始初始化

时间:2017-12-17 12:14:01

标签: save nodemcu espruino

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

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

  • 如果我将save()放在代码的末尾,在我重新启动后,NodeMCU代码将继续从停止的位置运行,但这意味着NodeMCU未连接到wifi。
  • 如果我将E.setBootCode(init());放在代码末尾并重新启动NodeMCU代码,则不再运行。

有没有人知道如何在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,

1 个答案:

答案 0 :(得分:1)

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

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

您使用E.setBootCode(init());执行的操作无效,因为它正在执行字符串。你正在做的是执行init()函数,然后将该函数的返回值放入setBootCode

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