2017-12-17 16 views
1

私はクールなプロジェクトを作っています。私は多かれ少なかれ、Espruinoを実行しているNodeMCUに行く私のコードを確定しました。私はこのコードをEspruinoに保存するのが難しいです。このコードは、起動時にこれを行う必要があります:wifiに接続し、すべての関数と変数を宣言します。その後、read()関数を連続して実行する必要があります。Espruinoはコードを保存して初期化を開始します

私がhttps://www.espruino.com/Savingから見る通り、私には2つの選択肢があります。私は両方を試みた。

  • 私は、コードの末尾にsave()を置く場合、私はNodeMCU再起動した後、コードは、それが停止した場所から実行し続けますが、これはNodeMCUが無線LANに接続されていないことを意味します。
  • 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

あなたの最善の解決策は、onInitにごinit機能の名前を変更して、アップロード後save()を入力することであり、それは魔法のように起動しますワーキング。

あなたが見つけたページhttps://www.espruino.com/Savingには約onInitが自動的に呼び出されています。

E.setBootCode(init());で行っていることは、文字列を実行しているため機能しません。あなたがしているのは、init()関数を実行してから、その関数の戻り値をsetBootCodeに入れることです。

あなたはE.setBootCode("init();");を必要とするだろう - が、この場合には、あなたが本当にただ最初のオプションを行う必要があります - onInit

を使用して
関連する問題