Due to high demand expect some shipping delays at this time, orders may not ship for 3-4 business days. On MLK Day no orders will be shipped.
0

How to pull battery-backup time from GPS when no fix
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

How to pull battery-backup time from GPS when no fix

by GlitchBoy on Fri Dec 03, 2021 4:23 pm

I'm trying to use the Ultimate GPS featherwing in a clock project so it always has the correct time. My research has indicated that it has a RTC and the battery backup will keep it going even if it looses its fix.

I used the adafruit example as a starting point for my code. When the GPS has a fix, I can get the current hour and minute using:

current_hour = gps.timestamp_utc.tm_hour
current_minute = gps.timestamp_utc.tm_min

and all is well.

However, if the GPS has not yet gotten a fix, or if it had a fix and then looses it, that same exact line of code causes the execution to halt with an error:

" AttributeError: 'NoneType' object has no attribute 'tm_hour' "

I don't get why that happens, but then again I'm a newb at Python and I'm sure there's a logical reason for it. So something tells me that when the GPS does not have a fix, I may need to use a different variable to pull the time off of it's RTC, but after much searching I have not been able to determine how. I'd be grateful for any advice.

Thank you

GlitchBoy
 
Posts: 18
Joined: Tue Nov 30, 2021 12:46 pm

Re: How to pull battery-backup time from GPS when no fix

by adafruit_support_mike on Fri Dec 03, 2021 5:01 pm

IIRC, the NMEA sentence parser only returns a data structure when the sentences say the device has a fix.

The raw NMEA sentences should have a timestamp whether the GPS module has a fix or not. When there’s no fix, the time comes from the internal RTC.

adafruit_support_mike
 
Posts: 63937
Joined: Thu Feb 11, 2010 2:51 pm

Re: How to pull battery-backup time from GPS when no fix

by blakebr on Mon Dec 06, 2021 12:37 am

Write a simple script that takes the raw GPS data from the RX and TX pins on the GPS module. The data most likely will be 9600 bps. The AdaFruit library probably try’s to protect the user from inaccurate data.

blakebr
 
Posts: 248
Joined: Tue Apr 17, 2012 6:23 pm

Re: How to pull battery-backup time from GPS when no fix

by GlitchBoy on Tue Dec 07, 2021 3:13 pm

Yep, I'm sure it can be done, but it sounds outside my skill zone right now. I was hoping (assuming) that the adafruit module would provide access to the battery backed up RTC. Seems only logical to me, else what is the point of having a battery back-up?

GlitchBoy
 
Posts: 18
Joined: Tue Nov 30, 2021 12:46 pm

Re: How to pull battery-backup time from GPS when no fix

by blakebr on Tue Dec 07, 2021 6:53 pm

Hello,

Here is some code I am using. It takes the output of two GPS receivers on two USB ports and displays it. HTH
Code: Select all | TOGGLE FULL SIZE
#!/usr/bin/python3
#### #!/usr/bin/env python3

##########################
# Start of Program setup #
##########################

import time
import board
import math
import random
import serial
import os
import os.path
from os import path

import RPi.GPIO as GPIO
import sys

import smbus
import logging
import getopt

ser = "noGPS"
# with serial.Serial('/dev/ttyS1', 19200, timeout=1) as ser:
# NOTE:  Only one GPS receiver can be used at a time
if   (path.exists("/dev/ttyACM0")):
        ser0 = serial.Serial('/dev/ttyACM0', 9600, timeout=1)  # open serial port
        print ("/dev/ttyACM0  exists")
if (path.exists("/dev/ttyACM1")):
        ser1 = serial.Serial('/dev/ttyACM1', 9600, timeout=1)  # open serial port
        print ("/dev/ttyACM1  exists")
# End If
print (ser)
while True:
    Line  = ser0.readline()        # series of bytes      (char) not a string
    Line2 = Line.decode("utf-8")   # convert char to      a string
    Line2 = Line2.replace("*",",") # replace (*) wit     h (,)
    Lin   = Line2.split(',')       # split line into      a srring array
    NMEA  = Lin[0] # Line headder
    if NMEA == "$GPTXT":
           print (Line, end='')
    elif NMEA == "$GPRMC":  # Lin[1]
           print("\f")
           print (Line2, end='')
#           times    = Lin[1]
#           hours    = times[0:2]
#           minutes  = times[2:4]
#           seconds  = times[4:6]
#           fraction = times[7:10]
#           min = int(minutes)
#           sec = int(seconds)
    if NMEA == "$GPVTG":
           print (Line2, end='')
    if NMEA == "$GPGGA":  # Lin[1]
           print (Line2, end='')
    if NMEA == "$GPGSA":
           print (Line2, end='')
    if NMEA == "$GPGSV":
           print (Line2, end='')
    if NMEA == "$GPGLL":  # Lin[4]
           print (Line2, end='')

    Line  = ser1.readline()         # series of bytes      (char) not a string
    Line2 = Line.decode("utf-8")   # convert char to      a string
    Line2 = Line2.replace("*",",") # replace (*) wit     h (,)
    Lin   = Line2.split(',')       # split line into      a srring array
    NMEA  = Lin[0] # Line headder
    if NMEA == "$GPTXT":
           print (Line, end='')
    elif NMEA == "$GPRMC":  # Lin[1]
           print (Line2, end='')
#           times    = Lin[1]
#           hours    = times[0:2]
#           minutes  = times[2:4]
#           seconds  = times[4:6]
#           fraction = times[7:10]
#           min = int(minutes)
#           sec = int(seconds)
    if NMEA == "$GPVTG":
           print (Line2, end='')
    if NMEA == "$GPGGA":  # Lin[1]
           print (Line2, end='')
    if NMEA == "$GPGSA":
           print (Line2, end='')
    if NMEA == "$GPGSV":
           print (Line2, end='')
    if NMEA == "$GPGLL":  # Lin[4]
           print (Line2, end='')

blakebr
 
Posts: 248
Joined: Tue Apr 17, 2012 6:23 pm

Re: How to pull battery-backup time from GPS when no fix

by adafruit_support_mike on Tue Dec 07, 2021 11:21 pm

If you're using our CircuitPython module, the .nmea_sentence() method will give you the information you need.

adafruit_support_mike
 
Posts: 63937
Joined: Thu Feb 11, 2010 2:51 pm

Re: How to pull battery-backup time from GPS when no fix

by GlitchBoy on Wed Dec 08, 2021 5:25 pm

Yes I'm using the Adafruit ultimate GPS featherwing. Mike, can you point me to the documentation on the .nmea_sentence() method? I've done searches but I don't think I'm finding what I need. (or it's possible I don't know what I'm looking for). Or could provide me a brief example of how to include it in my code after my gps.update() call?

blakebr, thank you for the code. I think it will be useful to me.

thank you
GB

GlitchBoy
 
Posts: 18
Joined: Tue Nov 30, 2021 12:46 pm

Re: How to pull battery-backup time from GPS when no fix

by blakebr on Wed Dec 08, 2021 7:07 pm

Null to fix double post
Last edited by blakebr on Wed Dec 08, 2021 7:20 pm, edited 3 times in total.

blakebr
 
Posts: 248
Joined: Tue Apr 17, 2012 6:23 pm

Re: How to pull battery-backup time from GPS when no fix

by blakebr on Wed Dec 08, 2021 7:07 pm

GB,

I got this chunk of code to work. I hope it gives you a good place to start.
Code: Select all | TOGGLE FULL SIZE
import time
import board
import busio
import adafruit_gps

uart = busio.UART(board.TX, board.RX, baudrate=9600, timeout=10)
gps = adafruit_gps.GPS(uart, debug=False)  # Use UART/pyserial
gps.send_command(b"PMTK314,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0")
gps.send_command(b"PMTK220,1000")

# Main loop runs forever printing the GPS data.
while True:
    gps.update()
    print(gps.nmea_sentence)
    time.sleep(.33)
    pass

blakebr
 
Posts: 248
Joined: Tue Apr 17, 2012 6:23 pm

Re: How to pull battery-backup time from GPS when no fix

by adafruit_support_mike on Wed Dec 08, 2021 8:38 pm

The documentation lives here:

https://circuitpython.readthedocs.io/pr ... en/latest/

It's linked from the top page of the module's Github repo.

The .nmea_sentence() method is specifically mentioned here:

https://circuitpython.readthedocs.io/pr ... a_sentence

adafruit_support_mike
 
Posts: 63937
Joined: Thu Feb 11, 2010 2:51 pm

Re: How to pull battery-backup time from GPS when no fix

by blakebr on Wed Dec 08, 2021 9:38 pm

Mike,

You may need to drop the () off of the end of .nmea_sentence.
It is not shown in the documentation and my chunk of code works without it.

Bruce

blakebr
 
Posts: 248
Joined: Tue Apr 17, 2012 6:23 pm

Re: How to pull battery-backup time from GPS when no fix

by adafruit_support_mike on Fri Dec 10, 2021 4:45 pm

Yeah, that’s a property, not a method. Good catch!

adafruit_support_mike
 
Posts: 63937
Joined: Thu Feb 11, 2010 2:51 pm

Re: How to pull battery-backup time from GPS when no fix

by blakebr on Fri Dec 10, 2021 7:29 pm

Mike,

After beating my head against the wall for 30 minutes on this one, I reread the documentation.
RTFM and It works better if you turn it on/plug it in will eventually dawn on me. ;-)

Bruce

blakebr
 
Posts: 248
Joined: Tue Apr 17, 2012 6:23 pm

Re: How to pull battery-backup time from GPS when no fix

by adafruit_support_mike on Fri Dec 10, 2021 8:08 pm

That happens more than any of us want to admit. ;-)

adafruit_support_mike
 
Posts: 63937
Joined: Thu Feb 11, 2010 2:51 pm

Please be positive and constructive with your questions and comments.