Reputation: 21
I'm using node.js to execute an external file (compiled cpp). If I execute the file on it's own, the program works ok. I should get the following output:
Got response 534, round-trip delay: 9569
I would like to use websokets to take the value and display on a webpage. Unfortunately, when running the program from node.js I have the following output:
stdout: stderr: exec error: null I don't understand what is happening and why if I run the application from command line I get the desired output?
Thank you and please forgive me for the dumb question!
After changing the way I execute the file I get the following output:
stdout: stderr: exec error: 139
the changes I have made are:
exec('sudo /home/pi/gitwork/RF24/RPi/RF24/examples/light -m 1',
function (error, stdout, stderr) {
socket.emit("response");
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error.code);
}
server.js
function sendMessage(socket){
console.log("execute app");
exec.execFile('/home/pi/gitwork/RF24/RPi/RF24/examples/light',
['-m', 1],
function (error, stdout, stderr) {
socket.emit("response");
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error.code);
}
});
}
io.sockets.on('connection', function (socket) {
socket.on('get', function (data) {
console.log("get received");
sendMessage(socket);
});
});
light.cpp
bool switchLight(int action){
radio.startListening();
bool timeout = false;
while ( ! radio.available() ) {
sleep(10);
}
if (radio.available()){
unsigned long got_time=0;
radio.read( &got_time, sizeof(unsigned long) );
printf("Got response %lu, round-trip delay: %lu\n\r",got_time,millis()-got_time);
return true;
}else{
printf("Failed, response timed out.\n\r");
return false;
}
}
int main( int argc, char ** argv){
char choice;
setup();
bool switched = false;
int counter = 0;
while(( choice = getopt( argc, argv, "m:")) != -1){
if (choice == 'm'){
printf("\nOpening the gates...\n");
while(switched == false && counter < 5){
// switched = true;
switched = switchLight(atoi(optarg));
counter ++;
}
}else{
// A little help:
return 4;
printf("\n\rIt's time to make some choices...\n");
}
//return 0 if everything went good, 2 otherwise
if (counter < 5 )
// printf("ok ok ok");
return 0;
else
return 2;
}
}
Upvotes: 2
Views: 1272
Reputation: 21
I noticed that if I comment below line, the script works ok. The example I'm trying to use is this one: http://hack.lenotta.com/arduino-raspberry-pi-switching-light-with-nrf24l01/
This made me to check the differences of RF24 libraries (the one I use and the one used in the example) and it seems that they are using a different library. I'll try to change it and see what it happens.
radio.read( &got_time, sizeof(unsigned long) );
Upvotes: 0