【问题标题】:Espruino save code and start on initializationEspruino 保存代码并开始初始化
【发布时间】:2017-12-18 17:28:16
【问题描述】:

我正在制作一个很酷的项目。我或多或少地完成了我的代码,它将在运行 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】:

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

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

    您对E.setBootCode(init()); 所做的操作将不起作用,因为它正在执行一个字符串。您正在做的是执行 init() 函数,然后将 该函数的返回值放入 setBootCode

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

    【讨论】:

      猜你喜欢
      • 2011-12-19
      • 2011-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 2010-12-24
      相关资源
      最近更新 更多