Setting up GPS NEO 6 sensor with Raspberry PI and Node.js
The content here is under the Attribution 4.0 International (CC BY 4.0) license
Recently I have had the chance to set up a basic IoT project which consists of localization by GPS. The decision was to work with the Raspberry board over the arduino based on the number os shields that arduino would use.
To get start with we would need a Wi-Fi(or GSM) shield, which on raspberry is just an adapter. The second point was the MQTT protocol and the code maintainability. Whereas on arduino is just plain C, with Raspberry we have more options. One could argue that we can use the standard firmata to avoid using C, but just for clarity this wasn’t the crucial point.
After deciding which board to use the time was to gather the GPS. As you might have guesses the GPS NEO 6 was the chose one (most of NEO6 comes with an external antenna, but here I used one with a internal antenna).
In this post you will see the following:
- Wiring up the components
- How to configure the Raspberry Pi and read data from the GPS
- Enable the UART mode in a specific GPIO
- Gotchas, a few tricks and tips to help you mitigate problems
IMPORTANT - EDIT 05/01
The antenna that comes with this sensor is really cheap and takes time to receive the signal from the satellites. Make sure to be outside, and with a clear vision from the sky to improve your success of getting a signal.
The sensor has a PPS indicator (green light), when blinking it indicates that the signal is coming and therefore you are receiving the correct information. Otherwise the light will keep on, which means that it is trying to connect.
IMPORTANT: Before we start, the version of Raspberry Pi that I used is the model 2 B+, but it should be really simple to adapt for any Pi version.
The first thing to be in mind is the wiring, be careful while setting up the wires RX and TX. I just made a mistake that was plugin TX on TX and RX to RX. To avoid any further errors, the table below show the pin name (from the GPS NEO 6) and where it should go on the Raspberry Pi.
|GPS NEO 6||Raspberry Pi|
||TX - GPIO 15|
||RX - GPIO 16|
||3.3v or 5v|
The table below was build looking at the pi4j project, which has an image to better illustrate the pins. For now the figure below should do the work.
If you follow everything as described in the table and in the figure, you should see a green light on the GPS NEO 6. This means that at least the power was plugged correctly. The next step is to check the RX and TX connections which will send to use the data through the serial port.
The first test doesn’t need any special software, we are going to connect using the cat command directly to the serial port.
IMPORTANT: Before testing the signal from the sensor is needed to convert the GPIO from IN to UART in order to do that we need the GPIO numbers that we connected our GPS, in our case it is 15 and 16.
Next just type the following command to switch the GPIO mode:
shell gpio mode 15 ALT0; gpio mode 16 ALT0
Doing that will the work, but keep in mind that once you restart the Pi the default configuration will be restored. The ideal step here is to add this command into the
/etc/rc.local, so every time the Pi restarts it will run this command automatically.
Access your Raspberry Pi through SSH or plugin an keyboard and monitor to get to the shell. Once you are in, type the following command:
sudo cat /dev/serial0
If everything is correct you should see the following response:
The serial port
/dev/serial0 is an alias to the real one
/dev/ttyAMA0. If for some reason your serial
port is not the same as this post, first find it and replace the serial with the correct one.
Finally we have everything done to use the GPS daemon and client. If you have a look at the raw logs from the serial port, the text has an pattern but is difficult to understand and difficult to interact with (parse and use the data for an application). For that reason the project GPSD exists, which is a library to help to communicate with the GPS sensor ( not to mention the amazing interface between the sensor and the client).
To use it, just run the
apt-get command as the following:
sudo apt-get install gpsd gpsd-clients
gpsd is the daemon that runs in the background to fill up the GPS client, without the daemon
is not possible to fetch the data that comes from the GPS. Even if you try to access the client
with the command
cgps it will give you a error message.
pi@raspberrypi:~ $ cgps cgps: no gpsd running or network error: -6, can't connect to host/port pair
cgps is the client that comes from the package
gpsd-clients that we’ve installed.
To prevent the error we need to start the daemon passing as an argument the serial port in which our GPS is connected to.
cgps should work as a expected now. The picture below illustrates
the result after invoking the command.
obs: The response given by the
cgps is a JSON, to understand the content of each key
check out the official documentation.
In my experience the GPS NEO 6 takes a long time to start receiving signal from the satellites across the earth. It takes from 30 minutes to 2 hours, when it supposed to be less than 1 minute. Keep in mind that it can happen to you as well but don’t worry, just wait and keep an eye in the logs.