node-gpsd
Interface to gpsd.
Installation
With package manager npm:
npm install node-gpsd
Code instructions
Require node-gpsd
by calling:
var gpsd = require('node-gpsd');
node-gpsd
has 2 classes: Daemon
and Listener
.
The Daemon
is a wrapper to start and stop gpsd
from your program. The Listener
interfaces with a running gpsd
(not necessarily instantiated via the Daemon
class).
Deamon
A Daemon
is instantiated by calling:
var daemon = new gpsd.Daemon({
program: 'gpsd',
device: '/dev/ttyUSB0',
port: 2947,
pid: '/tmp/gpsd.pid',
verbose: false
});
The options that are listed above are the default values so calling new gpsd.Daemon()
will have the same effect. Change the options according your own setup.
The Daemon
can be started and stopped by calling the appropriate methods:
daemon.start(function() {
console.log('Started');
});
or:
daemon.stop(function() {
console.log('Stopped');
});
The Daemon
can log to the console if needed. Logging can be controlled by adding verbose: true
to the options when creating the Daemon
or by calling:
daemon.setVerbose(true /* or false */);
The Daemon
is an EventEmitter and will emit the following events:
died
: when theDaemon
is killed.
Listener
A Listener
is instantiated by calling:
var listener = new gpsd.Listener({
port: 2947,
hostname: 'localhost',
verbose: false
});
The options that are listed above are the default values so calling new gpsd.Listener()
will have the same effect. Change the options according your own setup.
The Listener
can be connected to the gpsd
by calling:
listener.connect(function() {
console.log('Connected');
});
and disconnected by calling:
listener.disconnect(function() {
console.log('Disconnected');
});
The connection state can be queries by calling:
listener.isConnected();
To control watching gps events call the methods:
listener.watch();
listener.unwatch();
This will put the Listener
in and out-of watching mode. The Listener
is an EventEmitter and will emit the following events:
gpsd
events like described in the gpsd documentation. Allgpsd
events like:TPV
,SKY
,INFO
andDEVICE
can be emitted. To receive allTPV
events just addlistener.on('TPV', function(tpvData))
to your code.error
when data in a bad format is received fromgpsd
.disconnected
when the connection withgpsd
is lost.connected
when the connection withgpsd
is established.error.connection
when the connection is refused.error.socket
on other connection errors.
It is possible to query the gps device by calling:
listener.version(); /* a INFO event will be emitted */
listener.devices(); /* a DEVICES event will be emitted */
listener.device(); /* a DEVICE event will be emitted */
The Listener
can log to the console if needed. Logging can be controlled by adding verbose: true
to the options when creating the Listener
or by calling:
listener.setVerbose(true /* or false */);
Shout outs
Shout outs go to Pascal Deschenes for creating the Bancroft project that formed the basis for node-gpsd
.