YitEarp
Posts: 45
Joined: Mon May 13, 2019 7:44 pm

Help adding pynmea to parse data in code

Sat May 25, 2019 11:08 pm

Hi i have succeded in getting this stupid gprs/gps hat working via AT commands. It seems the only way it will work anyway i digress here is my code.

Code: Select all

#!/usr/bin/python

import serial
import pynmea2
import time

# Enable Serial Communication
port = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=1)
# Transmitting AT Commands to the Modem
# '\r\n' indicates the Enter key

port.write('AT'+'\r\n')
rcv = port.read(100)
print rcv
time.sleep(.1)

port.write('AT+CGNSPWR=1'+'\r\n')             # to power the GPS
rcv = port.read(100)
print rcv
time.sleep(5)

port.write('AT+CGNSIPR=9600'+'\r\n') # Set the baud rate of GPS
rcv = port.read(100)
print rcv
time.sleep(.1)

port.write('AT+CGNSTST=1'+'\r\n')    # Send data received to UART
rcv = port.read(100)
print rcv
time.sleep(5)

port.write('AT+CGNSINF'+'\r\n')       # Print the GPS information
rcv = port.read(200)
print rcv
time.sleep(5)

ser = serial.Serial('/dev/ttyS0',9600)
while 1:
     data = ser.readline()
     if (data.startswith("$GPGGA")):
         msg = pynmea2.parse(data)
         print repr(msg)
I am trying to get pynmea2 to parse the $RMC sentence and output in human readable form.

Any tips?

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

Re: Help adding pynmea to parse data in code

Sat May 25, 2019 11:17 pm

Have you seen the example at https://github.com/Knio/pynmea2

Looks like it splits everything out and the you just reference the bit you need e.g. msg.altitude or msg.lat etc
Need Pi spray - these things are breeding in my house...

YitEarp
Posts: 45
Joined: Mon May 13, 2019 7:44 pm

Re: Help adding pynmea to parse data in code

Sun May 26, 2019 12:07 am

Andyroo wrote:
Sat May 25, 2019 11:17 pm
Have you seen the example at https://github.com/Knio/pynmea2

Looks like it splits everything out and the you just reference the bit you need e.g. msg.altitude or msg.lat etc
Yes i tried that again with this new code and nothing is parsing

Code: Select all

#!/usr/bin/python

import serial
import time
import pynmea2

# Enable Serial Communication
port = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=1)
# Transmitting AT Commands to the Modem
# '\r\n' indicates the Enter key

port.write('AT'+'\r\n')
rcv = port.read(100)
print rcv
time.sleep(.1)

port.write('AT+CGNSCMD=0,"$PMTK314,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29"') #Trying to parse only GGA and VTG sentences (this fails)
rcv = port.read(100)
print rcv
time.sleep(.1)

port.write('AT+CGNSPWR=1'+'\r\n')             # to power the GPS
rcv = port.read(100)
print rcv
time.sleep(5)

port.write('AT+CGNSIPR=9600'+'\r\n') # Set the baud rate of GPS
rcv = port.read(100)
print rcv
time.sleep(.1)

port.write('AT+CGNSTST=1'+'\r\n')    # Send data received to UART
rcv = port.read(100)
print rcv
time.sleep(5)

port.write('AT+CGNSINF'+'\r\n')       # Print the GPS information
rcv = port.read(200)
print rcv
time.sleep(5)

def read(filename):
    f = open(filename)
    reader = pynmea2.NMEAStreamReader(f)

    while 1:
        for msg in reader.next():
          print(msg)


def read_serial(filename):
    com = None
    reader = pynmea2.NMEAStreamReader()

    while 1:

        if com is None:
          try:
            com = serial.Serial(filename, timeout=5.0)
          except serial.SerialException:
            print('could not connect to %s' % filename)
            time.sleep(5.0)
            continue

        data = com.read(16)
        for msg in reader.next(data):
           print(msg)

User avatar
scruss
Posts: 2255
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Help adding pynmea to parse data in code

Sun May 26, 2019 12:30 am

Looks like pynmea2 is expecting to handle the serial device all itself. It's probably best to let it read all of the NMEA sentences too, as it'll want to populate its own data structures with all the information it can. You can then read only the bits you want.

So can you read the GPS with something like

Code: Select all

screen /dev/ttyS0 9600
? That should cause NMEA sentences to scroll up the screen. If you can't get that, then maybe you're using the wrong port? Have you got TX wired to RX, and vice versa?
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

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

Re: Help adding pynmea to parse data in code

Sun May 26, 2019 7:16 am

It just reads the stream, it can handle it itself iirc, but you can also feed pynmea2 the stream as well.

Can't look at the moment though.

What doe one of you lines look like, it does need to be NMEA data.

YitEarp
Posts: 45
Joined: Mon May 13, 2019 7:44 pm

Re: Help adding pynmea to parse data in code

Sun May 26, 2019 9:52 am

bensimmo wrote:
Sun May 26, 2019 7:16 am
It just reads the stream, it can handle it itself iirc, but you can also feed pynmea2 the stream as well.

Can't look at the moment though.

What doe one of you lines look like, it does need to be NMEA data.
I think you are correct it is not pushing out the $RMC statement. The data looks like this (it doesn't yet have a fix) AT+CGPSINF=32
+CGPSINF: 32,253967.000,V,0000.0000,N,00000.0000,E,0.000,0.00,0470480,,,N

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

Re: Help adding pynmea to parse data in code

Sun May 26, 2019 10:03 am

You may need to adapt the code, there is some specific device code in pynmea2, what and how it uses it I don't know. But I think that is just device added nmea sentences. You can see it in the GitHub code.

Just for reference, this is my main function for doing this, I've used it a long time in various ways.(pynmea2 & serial using UART pins)

Code: Select all

def gps_go():
    print("gps is go")
    serialPort = serial.Serial("/dev/serial0", 9600, timeout=0.5)
    while True:
        strip = serialPort.readline()
        #print(strip)
        if strip.find(b'GGA') > 0:
            msg = pynmea2.parse(strip.decode('utf-8'))
            print(msg.latitude,msg.longitude,msg.altitude)
        time.sleep(5)
This looks for GGA of course.

YitEarp
Posts: 45
Joined: Mon May 13, 2019 7:44 pm

Re: Help adding pynmea to parse data in code

Sun May 26, 2019 10:07 am

I think my problem is its not registering fully on the network since i am trying to use its GPRS function?

I am looking at how to set its APN and password for the T-Mobile network its proving quite tricky when a password is involved.

i can set APN no problem
AT+SAPBR=3,1,"APN","ee secure"
OK

I think if i get this setup then RMC sentences will be correct?

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

Re: Help adding pynmea to parse data in code

Sun May 26, 2019 10:19 am

That would be for the A-GPS side?

GNSS shouldn't need it.

I would guess you might have to ask for the parameters you want directly and store them in your own list, then use from there
I've not looked to see if it can be used as.a simple serial GNSS.

For reference to others
https://www.waveshare.com/wiki/File:SIM ... _V1.00.pdf

That's jus the GNSS side.

It's this device https://www.waveshare.com/gsm-gprs-gnss-hat.htm

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

Re: Help adding pynmea to parse data in code

Sun May 26, 2019 10:21 am

You might need to start again with setup...

But does this help at all?
https://www.zetecinside.com/xr2/rpi_project2.shtml

YitEarp
Posts: 45
Joined: Mon May 13, 2019 7:44 pm

Re: Help adding pynmea to parse data in code

Sun May 26, 2019 10:31 am

bensimmo wrote:
Sun May 26, 2019 10:21 am
You might need to start again with setup...

But does this help at all?
https://www.zetecinside.com/xr2/rpi_project2.shtml
I have used many guides that above is one of them i made some headway with it but got lost at the pynmea part since he then delved into sensors for something else :)

Proving to be a fruitless time with this device. I had expected to be able to simply run all the required steps to setup the port with gpsd then use the command cgps -s to retrieve the data.

But it looks as though this devices GPS power has to be turned on via an AT input first which had i known this i would never of purchased it.

On another terminal i have screen running /screen /dev/ttyS0 9600

I ran all the commands to get GPS started

Then switched to another terminal and straight away i got data

YitEarp
Posts: 45
Joined: Mon May 13, 2019 7:44 pm

Re: Help adding pynmea to parse data in code

Mon May 27, 2019 3:00 pm

Well i figured it out from the link you provided! Thank You.

Here is my code incase anybody else is searching out there.

Now i just want to be able to trigger this via a call or SMS and have it append to googlemaps and send back its location via SMS

Code: Select all

#!/usr/bin/python

import serial
import time
import pynmea2


#Enable Serial Communication
ser = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=1)
#Transmitting AT Commands to the Modem
#'r\n' indicates the Enter key



ser.write('AT'+'\r\n')
str = ser.readline()
print str
time.sleep(.1)

ser.write('AT+CGNSPWR=1'+'\r\n')
str = ser.readline()
print str
time.sleep(.1)

ser.write('AT+CGPSOUT=32'+'\r\n')
str = ser.readline()
print str
time.sleep(3)

ser.write('AT+CGNSTST=1'+'\r\n')
str = ser.readline()
print str
time.sleep(.1)

ser.flushInput()
ser.flushOutput()
def parseGPS(str):
    if str.find('GGA') > 0:
        msg = pynmea2.parse(str)
        print "Timestamp: %s -- Lat: %s %s -- Lon: %s %s -- Altitude: %s %s" % (msg.timestamp,msg.lat,msg.lat_dir,msg.lon,msg.lon_dir,msg.altitude,msg.altitude_units)


while True:
    str = ser.readline()
    parseGPS(str)

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

Re: Help adding pynmea to parse data in code

Mon May 27, 2019 3:12 pm

Good :-)

Just to note for anyone trying, that's Python2 above.
(It'll not work in Python3, though shouldn't be too hard to update it)

YitEarp
Posts: 45
Joined: Mon May 13, 2019 7:44 pm

Re: Help adding pynmea to parse data in code

Mon May 27, 2019 3:34 pm

I have just started learning python so i have guessed that because the while True: statement is a loop? What would i use to end?

would it be something like if str > 1:?

Also am not sure it is capturing how i want as this doesn't look like an GNMRC sentence as co-ords lead to nothing (changed values just incase)

Timestamp: 15:55:16 -- Lat: 8721.431854 N -- Lon: 00323.331247 W -- Altitude: 136.68 M
.

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

Re: Help adding pynmea to parse data in code

Mon May 27, 2019 4:33 pm

the co-ordinate will be correct, just not split up and maybe in the wrong format.
try msg.latitude and msg.longitude for a different format.
Came across that problem myself, you're either miles away or 40 miles in the wrong place.

You are grabbing the "GGA" sentence not the "RMC" sentence.
For what you are doing, I don't think it matters.

YitEarp
Posts: 45
Joined: Mon May 13, 2019 7:44 pm

Re: Help adding pynmea to parse data in code

Mon May 27, 2019 5:02 pm

bensimmo wrote:
Mon May 27, 2019 4:33 pm
the co-ordinate will be correct, just not split up and maybe in the wrong format.
try msg.latitude and msg.longitude for a different format.
Came across that problem myself, you're either miles away or 40 miles in the wrong place.

You are grabbing the "GGA" sentence not the "RMC" sentence.
For what you are doing, I don't think it matters.
I will try that and report back! I did try to change the parameter to "GRMC" but it resulted in errors still need to learn and yeah you are right i input the co ordinates to google maps and it was 45mins away :D

EDIT: No it didn't like me changing the longitude and latitude data in msg gave attribute error

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

Re: Help adding pynmea to parse data in code

Mon May 27, 2019 6:51 pm

Check spellings, they are there as attributes.
See the pynmea2 module page.

YitEarp
Posts: 45
Joined: Mon May 13, 2019 7:44 pm

Re: Help adding pynmea to parse data in code

Mon May 27, 2019 7:28 pm

Traceback (most recent call last):
File "/tmp/tmp/pycharm_project_357/gps.py", line 46, in <module>
parseGPS(str)
File "/tmp/tmp/pycharm_project_357/gps.py", line 41, in parseGPS
print "Timestamp: %s -- Latitude: %s %s -- Longitude: %s %s " % (data.timestamp,data.latitude,data.latitude_dir,data.longitude,data.longitude_dir)
File "/usr/local/lib/python2.7/dist-packages/pynmea2/nmea.py", line 155, in __getattr__
raise AttributeError(name)
AttributeError: latitude_dir

i swapped msg for data to help it make sense to me.

Changed my code too to make it easier to follow for myself as i add.

Code: Select all

#Transmitting AT Commands to the Modem
#'r\n' indicates the Enter key


ser.write('AT'+'\r\n') #Check to see if modem is responding
str = ser.readline()
print str
time.sleep(.1)

ser.write('AT+CGNSPWR=1'+'\r\n') #Turns on the GPS power
str = ser.readline()
print str
time.sleep(.1)

ser.write('AT+CGPSOUT=0'+'\r\n') #Outputs raw NMEA data
str = ser.readline()
print str
time.sleep(3)

ser.write('AT+CGNSTST=1'+'\r\n') #Starts sending data to UART
str = ser.readline()
print str
time.sleep(.1)

#Parsing Raw NMEA data through pynmea2

ser.flushInput()
ser.flushOutput()
def parseGPS(str):
    if str.find("GGA") > 0:
        data = pynmea2.parse(str)
        print "Timestamp: %s -- Lat: %s %s -- Lon: %s %s " % (data.timestamp,data.lat,data.lat_dir,data.lon,data.lon_dir)

#Loops the data need to figure out how to end the loop
while True:
    str = ser.readline()
    parseGPS(str)

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

Re: Help adding pynmea to parse data in code

Mon May 27, 2019 8:09 pm

Try

Code: Select all

print "Timestamp: %s -- Lat: %s %s -- Lon: %s %s " % (data.timestamp, data.latitude, data.lat_dir, data.longitude, data.lon_dir)
The East/West or North/South direction stays the same.
Depending where you live, you don't need them where I live, near the East/West changeover, I need to know.
Of there is no chance you can, then just see if you are east or west and append you coordinates with a - (minus) if needed.

'msg' was only used as it's short for 'message' and I think that is what the pynmea2 author used in the examples.

YitEarp
Posts: 45
Joined: Mon May 13, 2019 7:44 pm

Re: Help adding pynmea to parse data in code

Tue May 28, 2019 3:36 pm

bensimmo wrote:
Mon May 27, 2019 8:09 pm
Try

Code: Select all

print "Timestamp: %s -- Lat: %s %s -- Lon: %s %s " % (data.timestamp, data.latitude, data.lat_dir, data.longitude, data.lon_dir)
The East/West or North/South direction stays the same.
Depending where you live, you don't need them where I live, near the East/West changeover, I need to know.
Of there is no chance you can, then just see if you are east or west and append you coordinates with a - (minus) if needed.

'msg' was only used as it's short for 'message' and I think that is what the pynmea2 author used in the examples.
Tried that now i am getting even longer code :D

Doesn't seem to make any sense to googlemaps when i input it.

Timestamp: 15:36:40 -- Lat: xx.xxxxxxxxxx N -- Lon: -x.xxxxxxxxxxx W

Does google maps expect an 8 length lon and lat?

it seems if i take the first 8 digits inc the . then i get an accurate location fix.

Return to “Other projects”