npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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

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