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

GPS GY-NEO6Mv2

GPS GY-NEO6Mv2 to PI wiring

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.

GPS EM-406A wiring

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

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.

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):

Shell

1

#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

After this changes, reboot the PI

Shell

1

sudo reboot

After the reboot, install the GPS applications

Shell

1

sudo apt-getinstall gpsd gpsd-clients

Execute gpsd (The GPS daemon)

Shell

1

sudo gpsd/dev/ttyAMA0-F/var/run/gpsd.sock

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

Shell

1

cgps-s

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

Shell

1

sudo apt-getinstall 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.

Install Python module gpxpy

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:

Shell

1

sudo python gpswww.py

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

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)

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

1

sudo apt-get install php5-fpm

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

1

sudo vi/etc/nginx/sites-available/default

and uncomment the following lines:

1

2

3

4

5

6

7

8

location~.php${

fastcgi_split_path_info^(.+.php)(/.+)$;

# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

# fastcgi_pass 127.0.0.1:9000;

fastcgi_pass unix:/var/run/php5-fpm.sock;

fastcgi_index index.php;

include fastcgi_params;

}

Save and restart nginx

1

sudo/etc/init.d/nginx reload

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 .

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 !

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

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.

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.

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 ?

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

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.

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

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

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.

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

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

thank I fix the 403 error already and I now can connect to the map but I cant see the marker even when I put thi pi IP to the “RPI Encode IP” and It still a red button like thishttp://i.imgur.com/UsgEz2F.png
how can I fix this

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?

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.

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 !

Hi,
Sorry for the late reply. I have been following your tutorial.
I have connected the GPS module correctly. When you state “After that, change the /etc/inittab file and comment a line so it do not spawn a login console:”

How do I change this file or where is it located?
When I run the /etc/inittab command, I get a error message stating ” No such file or directory”.

Could you tell me what I may be doing wrong? I think since I am not able to change this file, the GPS is giving me no reading s for values. How else can I change or create this file and in which folder should it be located?

inittab is not a command, is a file. You have to edit it. The /etc/inittab is just the path. The file is located in etc directory.
The full command should be (without the quotes) “sudo vi /etc/inittab” or “sudo nano /etc/inittab” .

Reminder: This was for a older version of raspbian and using sysVinit. Newer versions (like sketch) use systemd, hence the missing of the file.
You can use raspi-config, go for option 5 (interfacing options) and then option P6 (Serial) and disable the login shell over serial and see if it works.

Hi sir!
I’m currently doing this command
” Execute gpsd (The GPS daemon)
gsudo gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock ”
but i seems to have problem said
” -bash: gsudo : command not found ”
can u help me solve the problem? it would be very helpful
ty.

First of all i like to congratulate for this wonderful project its great!

Now i like to share my problem here
1. gps coordinates are getting saved in file perfectly
2. map is getting loaded successfully
But when i am entering ip address of raspberry pi then there is no change in the status , I am not able to see any icon showing my current position

I am using raspbian jessie and same devices as mention in this tutorial. I know i am little away from achieving this. if you can help me .It would be great thank you

Everything should be fine.. This has a little bug (to revisit in the future) that is – the map, when displayed, it shows my home town. You need to zoom out and place it in your part of the world were you are trying the code.. You probably will see the marker there…

But, if you’re already at your position and don’t see nothing, please check the error logs for the websever and the javascript console – F12 on Chrome and Firefox – if there’s any errors… Please reply to me and I’ll try to help you ! Sorry for the delay in the answer.. This stopped sending emails and I don’t know why !