HasnainMoavia
Posts: 13
Joined: Sun Feb 24, 2019 12:04 am

How to save GPS Data to a text file?

Wed Apr 17, 2019 2:38 am

Hello everyone!

I am new with Raspberry Pi and GPS.
I need some help that how can I save the data of the GPS sensor to .txt file on the Raspberry Pi using Python code?
Help will be appreciated.

Thanks

User avatar
topguy
Posts: 5317
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: How to save GPS Data to a text file?

Wed Apr 17, 2019 9:25 am

Do you have a GPS ? Do you know how it connects to the Pi ?
What is the purpose of the text file ? Who or what should read the file later ? Should it contain raw GPS data or only serve as a log of positions ?
How well do you know Python ?

User avatar
Mortimer
Posts: 918
Joined: Sun Jun 10, 2012 3:57 pm

Re: How to save GPS Data to a text file?

Wed Apr 17, 2019 9:35 am

It probably relates to the OP's first port to the forum...
viewtopic.php?f=32&t=234357&p=1433628#p1433628

They are probably still using an Adafruit Ultimate GPS Breakout.

I assume by this question they have managed to get the GPS board working, if so it would have been nice to mention this in the other topic.
--------------
The purpose of a little toe is to ensure you keep your furniture in the right place.

User avatar
bensimmo
Posts: 3793
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: How to save GPS Data to a text file?

Wed Apr 17, 2019 10:01 am

Learn a bit of Python3 and a good way to do this and work out what you need to do is work your way through this project tutorial.
https://projects.raspberrypi.org/en/pro ... ata-logger
It's a datalogger (which is what you are doing ) but for the SenseHAT. Best part is you don't need a SenseHAT and you'll learn some techniques :-)

Then write or adapt for what you need with the GPS.

HasnainMoavia
Posts: 13
Joined: Sun Feb 24, 2019 12:04 am

Re: How to save GPS Data to a text file?

Wed Apr 17, 2019 1:27 pm

Actually, I wants to save every gps (latitude and longitude) value which it changes when I start moving from one place to another. I wants to save it to .txt file which I can further plot on graph and can check the accuracy of the GPS sensor.
Please note, I am using Raspberry Pi 3.

HasnainMoavia
Posts: 13
Joined: Sun Feb 24, 2019 12:04 am

Re: How to save GPS Data to a text file?

Wed Apr 17, 2019 1:31 pm

Mortimer wrote:
Wed Apr 17, 2019 9:35 am
It probably relates to the OP's first port to the forum...
viewtopic.php?f=32&t=234357&p=1433628#p1433628

They are probably still using an Adafruit Ultimate GPS Breakout.

I assume by this question they have managed to get the GPS board working, if so it would have been nice to mention this in the other topic.
I have already solved that issue, which I had in the beginning. What I want now is to save the data (Lat and Lon) of the GPS.

HasnainMoavia
Posts: 13
Joined: Sun Feb 24, 2019 12:04 am

Re: How to save GPS Data to a text file?

Wed Apr 17, 2019 1:34 pm

topguy wrote:
Wed Apr 17, 2019 9:25 am
Do you have a GPS ? Do you know how it connects to the Pi ?
What is the purpose of the text file ? Who or what should read the file later ? Should it contain raw GPS data or only serve as a log of positions ?
How well do you know Python ?
Yes I am using the GPS sensor of Adafruit. It is connected to the Raspberry Pi through /dev/ttyS0. I want to save the Latitude and Longitude of the gps to a .txt file for further processing.
Thanks

HasnainMoavia
Posts: 13
Joined: Sun Feb 24, 2019 12:04 am

Re: How to save GPS Data to a text file?

Wed Apr 17, 2019 1:35 pm

bensimmo wrote:
Wed Apr 17, 2019 10:01 am
Learn a bit of Python3 and a good way to do this and work out what you need to do is work your way through this project tutorial.
https://projects.raspberrypi.org/en/pro ... ata-logger
It's a datalogger (which is what you are doing ) but for the SenseHAT. Best part is you don't need a SenseHAT and you'll learn some techniques :-)

Then write or adapt for what you need with the GPS.
Thanks for the sincere advice :)

User avatar
topguy
Posts: 5317
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: How to save GPS Data to a text file?

Wed Apr 17, 2019 2:04 pm

Actually, I wants to save every gps (latitude and longitude)
First you need to parse the raw GPS data into longitude and latitude values. There are some NMEA parsing libraries you can use for that.( maybe even some examples from adafruit how to use them too )
I wants to save it to .txt file which I can further plot on graph
How do you plan to plot it ?
Maybe you then want to choose a format of the textfile so that it can be easily imported into a map-application or google maps. that way you don't have to write a second program just to plot it.

----

I know this might be beside what you want to do if you want to use this as an exercise for Python but there is a program called "gpsbabel" which should be able to read the GPS directly from ttyS0 and output to file in many different formats.
https://www.gpsbabel.org/htmldoc-1.6.0/Invocation.html

HasnainMoavia
Posts: 13
Joined: Sun Feb 24, 2019 12:04 am

Re: How to save GPS Data to a text file?

Wed Apr 17, 2019 2:28 pm

First you need to parse the raw GPS data into longitude and latitude values. There are some NMEA parsing libraries you can use for that.( maybe even some examples from adafruit how to use them too )
Can you please specify it with a link or some other further detail?
The link to the code I am using from Adafruit is: https://github.com/adafruit/Adafruit_Ci ... pletest.py
I had some changes, I commented out RX, TX and uart in the code and I am using Import Serial, uart = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=3000). The rest is the same. What do you suggest? Shall I make changes in this code or shall I use some of the stuff from code and bring the data to .txt file?
I wants to save it to .txt file which I can further plot on graph
How do you plan to plot it ?
Maybe you then want to choose a format of the textfile so that it can be easily imported into a map-application or google maps. that way you don't have to write a second program just to plot it.[/quote]
At the moment I don't wants to plot it on google map. I simply wants to take the values of the gps which it changes every second when I start moving from one place to another.
What I meant with plotting is that I wants to save the data to a .txt file and copy that data on to a laptop where I can plot that using Microsoft Excel and looks for it's variance every second on the graph.

Andyroo
Posts: 1967
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: How to save GPS Data to a text file?

Wed Apr 17, 2019 5:15 pm

If you are going to read the data into Excel then look at writing the data out as a CSV file rather than plain text.

Python has a simple module that will help https://www.pythonforbeginners.com/syst ... in-python/
Need Pi spray - these things are breeding in my house...

User avatar
bensimmo
Posts: 3793
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: How to save GPS Data to a text file?

Wed Apr 17, 2019 6:44 pm

That's all covered in the SenseHAT data logger.

I would recommend using
pynmea2 to parse the serial stream
(Also use /dev/serial0 then it'll always point to the UART pins even if you change to a none WiFi Pi or disable Bluetooth etc).

You then just loop, parse, save to CSV, on the serial read
That's the basics.
Have fun working it out.

Return to “Python”