Connect a GPS to the Raspberry PI

After seeing an article about the Raspberry PI in the atmosphere with the PI in the Sky Project, I’ve decided why not try it ? Since I don’t have access to a telemetry board, I’ve decided to go ahead with off the shelf components. Well, this is the first step

Portuguese version

First, how to track the Raspberry PI with the GPS

Harware

Raspberry PI B/B+ (I’ve used both)

1 GPS (I’ve tried with two GPS – both working perfectly):

Wiring

To connect the GPS to the Raspberry PI we’re going to use the GPIO. The following table shows the GPIO PINs for RPI version B where to connect the GPS Pins.

Raspberry PIN GPS PIN
2 – 5v VIN/VCC
6 – GND GND
8 – TX RX
10 – RX TX

Note: The  TX and RX switch when connecting to GPS:: RX -> TX e TX -> RX

Here’s RPI B pinout

GPS GY-NEO6Mv2

GPSv2-3

GPSv2-2

 

  GPS GY-NEO6Mv2 to PI wiring

GPS PI

Some images of the GPS wiring to  Raspberry PI version B

GPSv2-1 GPIO2 GPIO1

GPS EM-406A

To connect the GPS EM-406A I had to buy a  JST SH Jumper 6  cable – with 6 pins to fit in the GPS  – although we only need 4 pins.

JST-SH6

GPS EM-406A wiring

EM406

GPIOBplus3GPIOBplusEM401

I’ve used the RPI version B+ for this GPS. No explanation – just because at this time the B+ was the only one I had available.  Both PINs in RPI B or B+ are the same and the wiring is also the same. The only thing changing is the GPIO  of RPI  B+.

GPIOBplus2

Power on the PI

As soon you have a prompt available, it is necessary to change some configurations.

The UART port, by default, is used for logins. We need to free it for the GPS.

Edit the file /boot/cmdline.txt and change:

to

After that, change the /etc/inittab file and comment a line so it do not spawn a login console:

Comment the following line (It should be the last one):

After this changes, reboot the PI

After the reboot, install the GPS applications

Execute gpsd (The GPS daemon)

Now, let’s try the GPS (place it outside so it can work better)

And after some seconds you should get some signal

Now you should have GPS in your PI. But lets not stop here and create a map of the RPI locations.

Update a map with the current RPi location

It wont do us any good if the have a GPS in the RPI and not create some fancy map with it.

Create a map with the GPS coordinates

By installing the GPS applications, we also get some python modules. Let’s use those to get some info from the GPS.

My solution was with a Web server, the python app reporting the coordinates to a text file and a Web application in another computer (or in the PI – more on that later) getting them and creating a map with PHP and Ajax.

In the PI, install a Web server. I’m going for nginx

Configure nginx

This is the first time I’m working with nginx. I decided to use it because is much less resource hungry than apache.

For security concerns, lets change nginx listening port. Instead of 80, lets use 2121 (for example).

Change the line

to

Change document_root

In the line that says root, change to

Note: Don’t forget to create the directories gps/www

Since nginx runs with the  www-data group, lets add the PI (or your user) to the group because of permissions – we want nginx to be able to read and create files.

Note: You need to logout and login again

Change the permissions so that any file created by nginx can be read by the group – any created file by nginx will belong to the www-data group – (setgid)

Restart nginx

Note: Do not forget that, as configured, to connect to nginx, you must use the 2121 port:

Code

PI

Install Python module gpxpy

All the code is in my  bitbucket account (with Portuguese comments).

The directory contains a file named gpswww.py .This file is to keep in the RPI, wherever you want, but you must later create a www directory (accessible by nginx) – where the this script will put the file coords.txt that will contain the current coordinates.

To run it, just:

Before running it, please make sure the GPS is connected and working. You can use the above commands to test it: cgps -s

For informations about gpsd Python modules, visit the page.

Computer (the one that’s going to display the map)

In this computer is necessary to install a Web server (with PHP support) and Internet access so it can display the maps.

In the bitbuck account you can find all the necessary files.

It’s only necessary to change 2 lines, one on each of the following files::

– getcoords.php : change the line $rpi_host to your RPI IP

– pingPHP.php :  change the line $host again, to match your RPI IP

Update: With my last version, there’s no need to change any file. Just put your RPI IP in the input box and press “Track”

Auxiliary files:

pingPHP.php

getcoords.php

Everything is configured now.

IMPORTANT NOTE: It may seem that the map is not updating, but it is. Just zoom out with the mouse and center the map at your location. Since it is necessary to put some coordinates when creating the map, I’ve placed the map at my home town (Castelo Branco – Portugal). I will update this in a new version. (Thank you Nuno Oliveira)

In a browser, execute the file RPITracking.html and wait for the markers to start appearing with your RPI position.

In this version I used markers instead of a line. I get it that after a while it gets messy and confused… In a newer version I’ll use a line.

Here’s the result I’ve tried this morning (Image from the previous version)

GPS Track

Use the RPI to show the map (in update)

Instead of using another computer to get the RPI coordinates and show them on the map, we can use the RPI to do this job – and access it with a browser –

Change nginx

Install  PHP

Next, edit the ficheiro /etc/nginx/sites-available/default

and uncomment the following lines:

Save and restart nginx

To work, the files you would put in the computer, put them in the RPI and you should get the map working.  As you have configured the nginx document_root, that’s were you need  to put the files. A side note – and since everything is in the same place, there was no need to use CURL – on PHP – to get the coordinates, but it will do no warm and it will work.

PI Mobility

For all this to work, the RPI is needed to work on batteries.

In my case, I’m using LIPO batteries  – with 2h and 30m duration – which is not bad…for now is enough .

My material was (Portuguese stores):

Lithium Ion Polymer Battery – 3.7v 1200mAh

– PowerBoost 500 Basic – 5V USB Boost @ 500mA from 1.8V+ (increasing 3.7v from the battery to 5v for the PI)

– USB/DC Lithium Polymer battery charger 5-12V (3.7/4.2v cells) (to recharge the batteries)

With the mini-usb cable, you connect all to the RPI. I believe is not the most efficient way to power the RPI, but after a few days reading, it’s all I came up. I’m not an electronics expert and if there’s some one out there with a better idea, please come forward !

PILipo

Before I’ve settled with this solution, I’ve tried with AA batteries – 1.5v – a 6 AA batteries adapter and a Pololu 5v Step-up/Step-down voltage regulator 5v to control the voltage (6 * 1.5v = 9v). But, don’t know what went wrong, and only got 40m…

RPI_Pilhas
RPI alimentado com pilhas

Saving power

We can save about 20mA (more or less) turning off the PAL/HDMI output (assuming we’re not going to use it) .

To do so, just add the following line to the RPI boot (Usually, adding the following line to  /etc/rc.local)

tip via http://www.daveakerman.com/?page_id=1294

Conclusions

This project (for now) as the downside that, in the computer used to view the maps, an Internet connection is necessary – because of the maps. There’s another reason that, both computers must be in the same network. I’m exploring XBees. The models I’ve purchased have 1.5km range – updates after some testing.

Hope you like it. Errors or mistakes, please advise !

39 Replies to “Connect a GPS to the Raspberry PI

  1. Dear,

    Could we use a offline google map for this project ?

    Could you please tell me about the accurate rate of both GPS device you have mention ?

    1. Hi Le Toi !
      I’ve used OpenStreetMaps for this project to run away from Google Maps. I’ve tried hard to search for a solution to Offline maps, but I couldn’t find any that I could make it work. I’m due to review this project and try again for a offline maps solution.

      For the accuracy of these GPS, I believe they are in the order of 3-4m . Can’t be sure right now, but is within these figures.

      Cheers and thank you !

    1. Hi Sergio.

      Did you restart ngynx after you change the port ?

      Try the nginx default port:
      nc localhost 80

      Do you have a firewall active ? Please check

        1. Hi !

          What problems are those ?
          nginx runs as www-data. You need to add your user to that group:

          gpasswd -a www-data pi

          logout and login again.

          Those directories where you have the files need to belong (or at least the www-data group) to the group:

          chgrp www-data -R
          chmod g+w -R

          Hope it helps

    1. Hi Dhani !
      Can you get GPS fix ? You can check that with the small utility cgps .

      Where are you stuck ?

  2. hello.

    when i running code sudo python gpswww.py

    it’s tell that no such file ‘/coords.text’. how to fix this problem.

    tq

    1. Hi man4118.
      It’s a file that should be available in the directory, but it should have been created if it not exists.

      To fix it, just change the line:

      coords = open (“www/coords.txt”,”w”)

      to

      coords = open (“www/coords.txt”,”w+”)

      (just add the plus sign after the w)
      This is in the file gpswww.py (on the RPi).

      Hope it helps. Let me know if you need anything more !

      1. hi FEITICEIR0.

        thank you for your reply.

        i already add the plus sign and its worked.

        after i running the sudo python gpswww.py , its show an error
        ” Traceback (most recent call last):
        File “gpswww.py”, line 27, in
        coords.write(str(report.lat))
        AttributeError: dictwrapper instance has no attribute ‘lat’ ”

        help me to fix this problem.

        thank you.

        1. Hi MAN4118

          That appears to me is a problem with the Python libraries for the gpsd.
          Are you getting a lock for the gps ?

          Try another version of the gpsd daemon

  3. Hi FEITICEIR0

    Yes, gps already lock and i get gps data when i run code cgps -s.

    what do you mean another version of the gpsd daemon?

    tq

    1. Hi MAN4118.

      Like I said, after a few searches, that appears to me to be a problem with the gpsd python libraries, hence I recommended you to try another version of the gpsd, but I guess that’s the latest version.
      Have you manage to sort that problem ?

      Cheers

  4. when I run code sudo python gpswww.py
    it’s tell that no such file ‘/coords.text’

    I have tried: “To fix it, just change the line:
    coords = open (“www/coords.txt”,”w”)
    to
    coords = open (“www/coords.txt”,”w+”)”

    In addition: previously, chmod g+s www returned a result chmod: No such file or directory.

    Please help, thanks.

    1. Hi Piuser !

      Have you created the www directory ? In the post I wrote:

      “Note: Don’t forget to create the directories gps/www”

      and

      “The directory contains a file named gpswww.py .This file is to keep in the RPI, wherever you want, but you must later create a www directory (accessible by nginx) – where the this script will put the file coords.txt that will contain the current coordinates.”

      The code in the bitbucket account is changed – there is no more w, just w+ ! I’ll change the code in the post.

      Check if you have the www directory created

  5. Hi there, so I’ve got the gpswww.py file as well as the php and html scripts in the /home/pi/gps/www folder on the Raspberry Pi. My coords.txt file is getting updated with my co-ordinates of my location. However, when I navigate to the Pi IP address:2121, a map is displayed, however, the button “track” after entering the pi’s IP seems to be greyed out. In addition, no markers are appearing at my location.

    What could be the problem?
    Thanks

    1. Hi Bivek !

      As soon I have time, I’ll look into it and try to figure why it’s grayed out. Nonetheless, you have a developer console in any browser that you can invoke and see any errors that may be happening and if you want, post them here.
      In the code there’s nothing to gray . The colors are just like that – a pale blue

  6. Hello,
    I need some helps. I have followed the tutorial but I cant find the inittab file on /etc directory. What should I do ?

  7. hello dude ! thank you for sharing this . can i ask where will i put the “gpswww.py” ? i have my directory gps/www
    but i cant run the “sudo python gpswww.py” coords.txt no file or directory thank you for your help d

    1. hey i have an update its already running but it i dont see any markers indicating that im here or any line . but i have my map when i put my ip then press track nothing happen

      1. Hi kuchi !
        Thank you for the comments.
        There’s sort of a bug in the map that I need to fix – a long time it is to fix . Try to center the map at YOUR location. When starting, the map is at Castelo Branco@Portugal.

  8. Hi friend
    when i am open the /etc/inittab it’s created new file there is content on the file. Can you please send data to me
    what data inside the file.
    And i am not getting gps data when i am typing cgps -s

  9. Hi,
    is it possible to use this GPS also as a precise time source?
    Can you please also add this part in your tutorial?

  10. Hi feiticeir0
    I follow your tutorial but I get the trouble that when I open RPITracking.html on raspi I get the red button and cannot click Track
    The cgps OK
    I cat the file coords.txt have the “lat lon” value
    Realoading nginginx.server OK
    ping 8.8.8.8 OK too

    So why I cannot track my raspi

          1. In the box next to the red button, put the Raspberry PI IP address and press track (the box comment is in Portuguese… I need to change that !

          2. I try and put the Pi IP to that “box” already then click “track” but no luck It’s still red and I cant see the marker !_!

          3. I see you’re using the Raspberry PI to display the map. Can you ping the RPi IP ?
            i.e: ping 192.168.18.3 ?

          4. Yeah indeed I can ping 192.168.18.3 because I connect from my laptop to my RPi via VNC

  11. i did type in the ip addres and pressed track The light is green beside the ip address.
    But no markers visible in the map. I change the original position in portugal to my position, but no gps markers in the map.
    cgps – s is working
    coord.txt is updating with coordinates from the python file
    What else can this be?

    using raspberry pi 3

    1. Hello Samuel ! Thank you for the comment.

      Everything seems fine. Can you please, see the browser console if any errors are displayed ? Ctrl+Shift+j (or F12 and choose console) for Chrome or F12 in Firefox

      Thank you

  12. Hi,
    Thanks for this great tutorial. I am using the following GPS, similar to the one used in the tutorial.
    http://www.ebay.co.uk/itm/GY-NEO6MV2-NEO-6M-GPS-Module-Board-with-Antenna-for-Arduino-Raspberry-Pi-/162198415977

    How do I “change the /etc/inittab file” ?
    I have followed all the other steps in the tutorial and also installed the GPS applications.
    When I run the command “cgps -s”
    I get the table with all the rows, Time, Latitude, Longitude, Speed, Heading etc. But next to each heading it states n/a.

    Could you please tell me where I might be going wrong
    Thanks in advance.

    1. Hi Ikram!

      Thank you !
      Now, some questions so I can understand a little more of your problem

      First: are you wiring it correctly ? RX from GPS goes to TX from RPi and TX from GPS goes to RX from RPi.
      Second: are you outside so your GPS can pick up satellites ?
      Third: About the /etc/inittab file – can you locate the file ? You just need to comment a line, as explained in the tutorial. Is the file there? What distribution are you using ? This was done with Raspbian available in 2014 – Wheezy – have to check now !

Leave a Reply to feiticeir0 Cancel reply

Your email address will not be published. Required fields are marked *