2017-12-17 48 views
1

멋진 프로젝트를 만들고 있습니다. Espruino를 실행하는 NodeMCU에서 진행될 내 코드를 더 많거나 적게 완성했습니다. Espruino에이 코드를 저장하는 데 어려움이 있습니다. 이 코드는 부팅 할 때마다 이렇게해야합니다. wifi에 연결하고 모든 함수 및 변수를 선언하십시오. 그런 다음 read() 기능을 계속 실행해야합니다.Espruino가 코드를 저장하고 초기화시 시작합니다.

나는 https://www.espruino.com/Saving에서 볼 수 있듯이 두 가지 옵션이 있습니다. 나는 둘 다 시도했다.

  • 은 내가 NodeMCU를 다시 시작한 후 코드는 중지 된 위치에서 계속 실행하지만이 NodeMCU가 무선 랜에 연결되어 있지 않은 것을 의미하는 코드의 끝 부분에 save()을 넣어합니다.
  • 코드 끝에 E.setBootCode(init());을 넣고 NodeMCU를 다시 시작하면 코드가 더 이상 실행되지 않습니다.

Espruino에 코드를 저장하는 방법을 아는 사람이 있습니까? Wi-Fi에 연결하여 전원을 켤 때마다 함수와 변수를 정의 할 수 있습니까?

내 코드 :

이 코드의
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

가장 좋은 해결책은 onInitinit 기능의 이름을 바꾼 다음 업로드 후 save()을 입력하는 것입니다 그것은 마술 시작합니다 일.

찾은 페이지는 https://www.espruino.com/Saving에 대해 자동으로 부팅시 약 onInit이 언급됩니다.

문자열을 실행 중이므로 E.setBootCode(init());으로 무엇을 할 수 없습니다. 당신이하는 일은 init() 함수를 실행 한 다음 함수의 반환 값을 setBootCode에 넣는 것입니다.

당신은 E.setBootCode("init();"); 필요 했어 -하지만이 경우 당신은 정말 그냥 첫 번째 옵션해야 - onInit

를 사용하여