Espruino保存代码并开始初始化

问题描述 投票:1回答:1

我做了一个很酷的项目。我或多或少地完成了我的代码,它将运行在运行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,
save nodemcu espruino
1个回答
1
投票

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

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

你用E.setBootCode(init());做什么是行不通的,因为它正在执行一个字符串。你正在做的是执行init()函数,然后将该函数的返回值放入setBootCode。

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

© www.soinside.com 2019 - 2024. All rights reserved.