I have a gps tracking script using node-gpsd module. When the automobile pass over a checkpoint the script plays a sound like "you are in XXXXX stop" and then waits 30 second to start tracking for the automobile ubication. All this tracking system is running on a raspberry pi zero in a bus.
Here is my code:
var turf = require('turf');
var fs = require('fs');
var gpsd = require('node-gpsd');
var listener = new gpsd.Listener();
var exec = require('child_process').exec;
var sleep = require('sleep');
function estaDentro(long,lat,coords){
var posicion = turf.point([long, lat]);
var pol = turf.polygon([coords]);
return turf.inside(posicion, pol);
};
function operacion(tpv) {
var lat = tpv.lat;
var lng = tpv.lon;
var zonasJson = fs.readFileSync("audio/jsons/zonas.json");
var zonas = JSON.parse(zonasJson);
for (i=0; i<zonas.zonas.length; i++){
if(estaDentro(lng,lat,zonas.zonas[i].coords)===true){
var zona = i;
var paradaJson = fs.readFileSync("audio/jsons/"+zona+"paradas.json");
var parada = JSON.parse(paradaJson);
for(x=0; x<parada.paradas.length; x++){
if(estaDentro(lng,lat,parada.paradas[x].coords)===true){
exec('mpg123 audio/arribando.mp3 audio/paradas/'+parada.paradas[x].nombre+'.mp3')
listener.unwatch()
sleep.sleep(30)
setTimeout(function(){ listener.watch(); }, 30000);
}
}
}
}
};
function correr() {
listener.on('TPV', function (tpv) {
operacion(tpv);
});
listener.connect(function() {
listener.watch();
});
}
correr();
I need to run this script at boot, but I cant, really, I tried in /etc/rc.local writing this on that file:
/usr/local/bin/node /home/pi/trak3.js &
Then I reboot and nothing happens, in the log it appears that the command its run but for some reason it closes.
Another approach was inserting in crontab like this
@reboot /usr/local/bin/node /home/pi/trak3.js &
This works, but the script runs with an error, it play the sound repeatedly 3 times in a row, but if I run the script myself writing in the command line it play the sound once.
I dont know why I cant run node via the /etc/rc.local and why that happen when it run via crontab. This is making me crazy haha.
Thank you
=============Update 05 November 2016 ==============
I got the conclusion that the problem is caused by the paths of the modules, because I made a test script without dependencies and it executed fine at boot, I can get the path from modules downloaded from npm, but I cant get the paths from the integrated modules.
var turf = require('/home/pi/node_modules/turf');
var fs = require('fs');
var gpsd = require('/home/pi/node_modules/node-gpsd');
var listener = new gpsd.Listener();
var exec = require('child_process').exec;
var sleep = require('/home/pi/node_modules/sleep');
As you can see I need the "fs" and "child_process" path modules, do anybody knows how to solve this problem? thank you.
sudo
here? Whoever you are copying does not know what they are doing. – goldilocks Nov 03 '16 at 08:20