1.1.2 • Published 7 years ago

pzgps-server v1.1.2

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
7 years ago

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...

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 own package.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 the package.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 yield v6.9.5
  • npm -v should yield 3.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 serial-getty@ttyAMA0.service
sudo systemctl disable serial-getty@ttyAMA0.service

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 gpsd
  • sudo /etc/init.d/gpsd restart - To elegantly restart gpsd
  • cgps -s - to open a terminal UI for gps data
  • cat /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 the scripts section
  • note the value for the start command
  • Add that command to rc.local with the correct path to the index.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.

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago