pzgps-server
v1.1.2
Published
Read GPS data from a PiZero in a single page web app via a WebSocket
Downloads
3
Readme
pzgps-server
The goal of this project is to collect data from the a GPS unit and stream that data out to a web front end via a WebSocket.
We'll use NodeJS and node-gpsd to read and process the data, make it available via ws, and render in the UI with the help of WebSockets.
That data, along with user defined information, can then be saved to a PouchDB database on your browser/device that syncs to a CouchDB database on your #pizero.
Assumptions
You...
- Have a #pizero with an ARM11 and a GPIO header soldered onto it.
- Have an Adafruit Ultimate GPS Breakout
- Have Raspbian Jessie installed.
- Have Connected your Adafruit GPS Breakout
Package Structure
- At the root of this package is an
index.js
file, which is a NodeJS application that reads the GPS data and provides it over a WebSocket (on port 9001). - Sample web apps are in the
/packages/
directory, each with it's ownpackage.json
file.- Currently the Preact version of the front end has the most code/features/effort.
- You'll run each part of the project by running
npm start
where thepackage.json
is located. - All front end web apps run on port 9001.
- If you want to persist data to a database, then install CouchDB on your pizero (see below) which will run on port 5984 (the Futon UI would then be at http://yourhostname:5984/_utils/index.html).
Installing NodeJS on the pizero
The version of NodeJS you get via apt-get install nodejs
is out of date (so you'd be missing some important security patches).
If you want to compile node from scratch on your #pizero and wait all night for it to complete, then check out this guide.
If you want to make things a bit easier, then download Node using wget
and install it directly. In this case we'll download the latest version (as of this writing) for ARM on the 6.x branch. Log in to your pi and remain in your home directory...
wget https://nodejs.org/dist/v6.9.5/node-v6.9.5-linux-armv6l.tar.xz
cd /usr/local
sudo tar --strip-components 1 -xvf ~/node-v6.9.5-linux-armv6l.tar.xz
cd && rm node-v6.9.5-linux-armv6l.tar.xz
Node is installed now, along with npm.
node -v
should yieldv6.9.5
npm -v
should yield3.10.10
Your /usr/local
dir has a few files left over from the install (ie, CHANGELOG.md, LICENSE, README.md). You can safely remove those.
Now consider installing n or nvm so you can easily install new versions of Node and NPM (and switch between them at will).
Installing CouchDB on the pizero.
The official PouchDB set up guide is excellent. The Preact based front end uses PouchDB to store data locally and to persist data to the CouchDB (see below) instance.
To install CouchDB on the pizero, log into it, then...
sudo apt-get install couchdb
Installing gpsd
Log into your pi...
sudo apt-get install gpsd gpsd-clients python-gps
Start gpsd
in verbose/debug mode...
sudo gpsd /dev/ttyAMA0 -D 2 -n -b -N -P /tmp/gpsd.pid -F /var/run/gpsd.sock
When starting gpsd
you might see an error like this...
gpsd:ERROR: can't bind to local socket /var/run/gpsd.sock
You can confirm data is coming to your #pizero with cat /dev/ttyAMA0
which should show a stream of data. If you see data coming thru but the commands to start gpsd
failed with an error about not being able to connect, then you might have to disable terminal over serial.
How disable terminal over serial
These didn't work, not sure why...
sudo systemctl stop [email protected]
sudo systemctl disable [email protected]
What did work was...
sudo raspi-config
- go to
Advanced Options
- then
serial
and turn it off sudo reboot
The Adafruit guide mentioned above says you can do this from /etc/inittab
but that file doesn't exist in Raspbian Jessie (it did in Wheezy). Raspbian Jessie has moved everything to services and there is no /etc/inittab
file at all, so it's best to use the raspi-config
command.
Configuring gpsd
To have gpsd
start up correctly, edit /etc/default/gpsd
# Default settings for the gpsd init script and the hotplug wrapper.
# Start the gpsd daemon automatically at boot time
START_DAEMON="true"
# Use USB hotplugging to add new USB devices automatically to the daemon
USBAUTO="false"
# Devices gpsd should collect to at boot time.
# They need to be read/writeable, either by user gpsd or the group dialout.
DEVICES="/dev/ttyAMA0"
# Other options you want to pass to gpsd
GPSD_OPTIONS
GPSD_SOCKET="/var/run/gpsd.sock"
Then restart: sudo /etc/init.d/gpsd restart
Then try cgps -s
and you should now see real data. If the GPS Breakout can't see the sky then you might see no fix
which means it can't see any satellites. Either go outside or put the #pizero on a window sill. 😀
More useful Commands for dealing with GPSD on the pizero
sudo killall gpsd
- To kill gpsdsudo /etc/init.d/gpsd restart
- To elegantly restart gpsdcgps -s
- to open a terminal UI for gps datacat /dev/ttyAMA0
- See raw data from the Adafruit Ultimate GPS Breakout
GPS data via NodeJS
Now that data is coming from the gps unit, thru gpsd
, we can read that data from node with the help of node-gpsd.
Run npm install
to install the deps which includes node-gpsd
This will handle the streaming of data from gpsd
for us and provide the data as JSON (it can also start and stop the daemon, you should read the docs).
Have a look at the index.js
in this repo and run npm start
in your terminal. If everything is set up correctly, you should see some basic info, then a bunch of TPV events streaming by. Now you have something you can write an application around.
Auto Start the WebSocket server at System Boot
After getting everything set up, you might want to have the WebSocket server auto-start when your pizero boots up.
- Look in the
package.json
file for thescripts
section - note the value for the
start
command - Add that command to
rc.local
with the correct path to theindex.js
file
Your entry in rc.local
will look something like this...
/usr/local/bin/node /home/pi/Projects/pzgps/index.js --port 9000 &
Change the port number as needed.