What's new

USB GPS instead of NTP

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

Fitz Mutch

Senior Member
I found this little USB GPS on eBay for $10 and it works perfectly to synchronize the router's clock, instead of NTP.

https://www.ebay.com/sch/i.html?_from=R40&_sacat=0&_nkw=usb gps u-blox&_sop=15&_clu=2&_fcid=1
s-l1600.jpg

Here's a sample script to synchronize the router clock with a GPS receiver. I verified this works on my RT-N16 router w/stock firmware. So it will likely work for all Asuswrt-Merlin routers too.

/jffs/scripts/setclock-ublox7.sh
Code:
#!/bin/sh
GPS_DEVICE="/dev/ttyACM0"
GPS_PRODUCT="1546:01a7"  # u-blox GPS receiver
NMEA_TYPE="GPZDA"
NMEA_DATA_LEN=36

# wait for device to become ready
modprobe cdc-acm
while : ; do
  lsusb | grep -qi "$GPS_PRODUCT"
  if [ $? -eq 0 ]; then
    break
  else
    sleep 5
  fi
done

# configure the u-blox7 GPS to report only $GPZDA
stty -F $GPS_DEVICE 9600 raw -clocal -echo icrnl
ubx_write() {
  local cmdline=" $@"
  local fmtline="${cmdline// /\\x}"
  printf $fmtline >$GPS_DEVICE
}
ubx_write "B5 62 06 02 0A 00 01 00 00 00 00 00 00 00 00 87 9A 77" # Turn off info messages
ubx_write "B5 62 06 01 08 00 F0 00 00 00 00 00 00 00 FF 23" # Disable: NMEA GxGGA
ubx_write "B5 62 06 01 08 00 F0 01 00 00 00 00 00 00 00 2A" # Disable: NMEA GxGLL
ubx_write "B5 62 06 01 08 00 F0 02 00 00 00 00 00 00 01 31" # Disable: NMEA GxGSA
ubx_write "B5 62 06 01 08 00 F0 03 00 00 00 00 00 00 02 38" # Disable: NMEA GxGSV
ubx_write "B5 62 06 01 08 00 F0 04 00 00 00 00 00 00 03 3F" # Disable: NMEA GxRMC
ubx_write "B5 62 06 01 08 00 F0 05 00 00 00 00 00 00 04 46" # Disable: NMEA GxVTG
ubx_write "B5 62 06 01 08 00 F0 06 00 00 00 00 00 00 05 4D" # Disable: NMEA GxGRS
ubx_write "B5 62 06 01 08 00 F0 07 00 00 00 00 00 00 06 54" # Disable: NMEA GxGST
ubx_write "B5 62 06 01 08 00 F0 08 01 01 01 01 01 00 0C 6F" # Enable: NMEA GxZDA
ubx_write "B5 62 06 01 08 00 F0 09 00 00 00 00 00 00 08 62" # Disable: NMEA GxGBS
ubx_write "B5 62 06 01 08 00 F0 0A 00 00 00 00 00 00 09 69" # Disable: NMEA GxDTM
ubx_write "B5 62 06 01 08 00 F0 0D 00 00 00 00 00 00 0C 7E" # Disable: NMEA GxGNS
ubx_write "B5 62 06 01 08 00 F0 0E 00 00 00 00 00 00 0D 85" # Disable: NMEA GxTHS
ubx_write "B5 62 06 01 08 00 F0 0F 00 00 00 00 00 00 0E 8C" # Disable: NMEA GxVLW
#ubx_write "B5 62 06 09 0D 00 00 00 00 00 FF FF 00 00 00 00 00 00 01 1B A9" # Save settings to BBR (battery-backed RAM)
ubx_write "B5 62 06 09 0D 00 00 00 00 00 FF FF 00 00 00 00 00 00 03 1D AB" # Save settings to BBR,Flash


# loop until successful clock synchronization
while : ; do

  # wait for NMEA sentence of type $GPZDA
  READ_COUNT=0
  read NMEA_DATA <$GPS_DEVICE
  while [ $? -eq 0 ] && [ ${#NMEA_DATA} -ne $NMEA_DATA_LEN -o "${NMEA_DATA:0:6}" != "\$${NMEA_TYPE}" ]; do
    let READ_COUNT++
    if [ $READ_COUNT -gt 50 ]; then
      READ_COUNT=0
      sleep 5
    fi
    read NMEA_DATA <$GPS_DEVICE
  done

  # found a NMEA sentence of type $GPZDA with correct length
  if [ ${#NMEA_DATA} -eq $NMEA_DATA_LEN ] && [ "${NMEA_DATA:0:6}" == "\$${NMEA_TYPE}" ]; then

    # compute the NMEA checksum
    L1=${#NMEA_DATA}
    let L2=$L1-3
    CHECKSUMHEX=${NMEA_DATA##*\*}
    CHECKSUM=$(printf "%d" 0x${CHECKSUMHEX})
    P=1; CHECKSUM_CALC=0
    while [ $P -lt $L2 ]; do
      let CHECKSUM_CALC^=$(printf '%d' "'${NMEA_DATA:$P:1}")
      let P++
    done

    # validate the NMEA checksum
    if [ $CHECKSUM_CALC -eq $CHECKSUM ]; then

      # parse current date/time from NMEA sentence of type $GPZDA
      if [ "$NMEA_TYPE" == "GPZDA" ]; then
        #           11111111112222222222333333
        # 012345678901234567890123456789012345
        # $GPZDA,132727.00,07,06,2017,00,00*61
        UTC="${NMEA_DATA:23:4}${NMEA_DATA:20:2}${NMEA_DATA:17:2}${NMEA_DATA:7:2}${NMEA_DATA:9:2}.${NMEA_DATA:11:2}"
      else
        UTC=""
      fi

      # set the system clock
      if [ -n "$UTC" ]; then
#        echo $UTC
        date -u -s $UTC
        if [ $? -eq 0 ]; then
          nvram set ntp_ready=1
          break
        else
          sleep 1
        fi
      fi

    fi
  fi

done


Example usage on startup.

/jffs/scripts/init-start
Code:
#!/bin/sh

# runs in background and waits indefinitely for successful clock synchronization then exits; you could do it once per day with an additional cron job
/jffs/scripts/setclock-ublox7.sh &


I suppress all NMEA sentences except $GPZDA, so it outputs only the current date/time and hides my geographic location. These settings may be "locked down" to the flash memory of the u-blox GPS, then you don't need to set the UBX codes every time. My u-blox7 USB GPS was very inexpensive. However, there's no flash memory to "lock down" my custom settings. When left unplugged for 4 hours, it resets back to factory defaults. That's why the script must write the UBX codes to the GPS every time.


This information originally appeared in the following post.
https://www.snbforums.com/threads/ntp-daemon-for-asuswrt-merlin.28041/page-10#post-328607
 
Last edited:
Out of curiosity, whats the hype of having such an accurate clock on your router, or is it just one of those "because I can" situations which I totally understand :p
 
Out of curiosity, whats the hype of having such an accurate clock on your router, or is it just one of those "because I can" situations which I totally understand :p
It's for reliability of services that do certificate validation on startup. Certificate validation requires that the correct date/time be set. Tor and Dnscrypt are examples of services that require the current date/time to operate successfully. Sometimes it can be a chicken/egg problem.
 
Last edited:
the router has to been placed under the sky?
You don't need a strong fix on multiple GPS satellites to read only the current date/time. You don't need the positional information.

My building has a metal roof, I'm three feet from the nearest window, there is barely any satellites around and I'm reading the current date/time from a weak satellite. I suppose it has a good side effect... if by chance, the GPS may not know my geographical location due to the weak satellite signal.

You could also use up to 12 foot USB extension cable, if you really want to get a stronger fix.
 
Last edited:
There's another thread here where GPS is used as a seed for local NTP...

Can't find it at the moment, but the heavy lifting for scripts, code, etc was completed and tested successfully.
 
There's another thread here where GPS is used as a seed for local NTP...
If we're thinking of the same thread, it was my brother Jon who implemented the GPS+PPS+NTP on the router. The new technique, as I've described here, should run on any Linux system because it is 100% script implementation. AND, it is proof that NTP is not needed to set the router's clock.
 
Last edited:
If we're thinking of the same thread, it was my brother Jon who implemented the GPS+PPS+NTP on the router. The new technique, as I've described here, should run on any Linux system because it is 100% script implementation. AND, it is proof that NTP is not needed to set the router's clock.

Might be...

The upside to having a very accurate local NTP is that one can use this as a time resource across the LAN
 
Out of curiosity, whats the hype of having such an accurate clock on your router, or is it just one of those "because I can" situations which I totally understand :p
Because its how my brain works :) I first got interested in time keeping at a local clock repairer (you'll need to Google some history to understand that profession) nearly 60 years ago, bought a very expensive "flipping digits" clock with my first pay and all downhill since then. Actually, perhaps I don't comply here as I have my ntp as a separate Raspberry Pi box, running a still-being-developed ntpsec but bleeding edge anyway.

Nah, you are right - because I can.
 
the router has to been placed under the sky?

To receive signal from the bird at midnight every day. Same with GPS. All the time base is atomic, they're all synchronized world wide. All wall clocks in my are atomic. Only time to touch the clock is to put in new battery. My daily casual watch is 3 band atomic G-Shock solar powered. I used to wear auto winding Swiss made very high quality watch. Anything electronic watch will emit RF signature in enemy territory.
 
To receive signal from the bird at midnight every day. Same with GPS. All the time base is atomic, they're all synchronized world wide. All wall clocks in my are atomic. Only time to touch the clock is to put in new battery. My daily casual watch is 3 band atomic G-Shock solar powered. I used to wear auto winding Swiss made very high quality watch. Anything electronic watch will emit RF signature in enemy territory.
In Europe, many clocks use the longwave time signal from a transmitter near Frankfurt, Germany.
https://en.wikipedia.org/wiki/DCF77

If I count correctly, I have about 5 of them around my home. When I moved to Australia years ago, I broght one with me in the container. I left it there after I moved back to Switzerland because the battery fluid desteoyed the electeonics.
And of course, it never received a single signal from the sender and since it had no manual setting, left it in a cubboard all the time.
 
In Europe, many clocks use the longwave time signal from a transmitter near Frankfurt, Germany.
Just in case North Korea make all the GPS satellites fall out of the sky?
 
Just in case North Korea make all the GPS satellites fall out of the sky?
This has been up and running since almost hefore North Korea existed, much less the GPS Satellites...
 
This has been up and running since almost hefore North Korea existed, much less the GPS Satellites...
I wish China makes a USB DCF77 receiver, it would be my plan B. Now, a North Korea DoS attack on a constellation of GPS satellites, that's something to bragg about. It will never happen, it's impossible.
 
I wish China makes a USB DCF77 receiver, it would be my plan B. Now, a North Korea DoS attack on a constellation of GPS satellites, that's something to bragg about. It will never happen, it's impossible.
Haha, just wanted to brag about an old, outdated but widely used and highly functional and reliable 'Atomic' time signal that kept the Germans and us Swiss on time and extremely punctual. All the other countries in Europe seem not to care from our perspective...
 
I would never say impossible - EMP is what it is...
The GPS satellites are possibly radiation-hardened? Instead, maybe a swarm of tiny drones could follow those GPS signals, into outer space, back to their respective satellites and do something really wicked to the satellites? It could get them in big trouble. However, I think we're safe for now.
 

Similar threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top