For my CarPC, I don't have any RTC module on Raspberry PI, but I do have a gps always connected, which provide accurate date and UTC time.
I have found some tutorials on how I can set up ntp to update the system clock based on gpsd but they didn't worked with any of my gps devices:
ST22 SkyTraq GPS receiver
Columbus V-800
I have followed some links with no luck. I got:
"gpsd:WARN: can't use GGA time until after ZDA or RMC has supplied a year."
or
"gps data is no good"
or
"unrecognized ... sentence"
I have decided to make my own time synchronization based on parsing raw gps data.
You can download an archive containing the scripts from here.
How does it work?
First connect your gps module:
gpsd /dev/ttyAMA0
Then, to get the raw data I used:
gpspipe -R -n10
This command will get the first 10 lines from gps raw data. I got this:
pi@raspberrypi ~ $ gpspipe -R -n10The Shell part.
{"class":"VERSION","release":"3.6","rev":"3.6","proto_major":3,"proto_minor":7}
{"class":"DEVICES","devices":[{"class":"DEVICE","path":"/dev/ttyAMA0","activated":"2013-10-06T09:42:18.793Z","flags":1,"driver":"Generic NMEA","native":0,"bps":9600,"parity":"N","stopbits":1,"cycle":1.00}]}
{"class":"WATCH","enable":true,"json":false,"nmea":false,"raw":2,"scaled":false,"timing":false}
$GPGGA,094220.784,4425.1141,N,02602.8254,E,1,05,1.6,96.1,M,37.0,M,,0000*6D
$GPGSA,A,3,25,05,29,31,21,,,,,,,,3.1,1.6,2.7*3A
$GPGSV,3,1,09,29,61,061,29,21,58,214,37,25,41,146,38,31,33,245,35*72
$GPGSV,3,2,09,05,25,056,30,16,14,314,,18,09,170,19,12,04,138,20*76
$GPGSV,3,3,09,06,02,278,*49
$GPRMC,094220.784,A,4425.1141,N,02602.8254,E,000.0,191.5,061013,,,A*6E
$GPVTG,191.5,T,,M,000.0,N,000.0,K,A*01
To set UTC time for our unix system we have to issue a command like this:
date -u -s "2013/10/05 12:48:00"From the raw gps output, we see that GPRMC gives all the needed information about the date and time(see here what the fields mean).
My idea was to capture just GPRMC data from this output and send it as a parameter to a C program which will parse the string and create a new string as needed to set time.
To get the GPRMC string from the raw gps output I have did the following bash command:
gpspipe -R -n10 | sed -n "/GPRMC/,/*/p"Decomposition of the command:
gpspipe -R -n10 - this outputs the first 10 lines from the gps raw output.
sed -n "/GPRMC/,/*/p" - extracts the line starting with the string GPRMC
I have used unix pipes(| character) to pass the output from gpspipe -R -n10 to the sed command.
The output from this command will be like this:
pi@raspberrypi ~ $ gpspipe -R -n10 | sed -n "/GPRMC/,/*/p"Now, to pass this as program arguments(assuming the program's name is set_date) we have to do the following:
$GPRMC,100201.786,A,4425.1179,N,02602.8192,E,000.0,191.5,061013,,,A*61
$GPVTG,191.5,T,,M,000.0,N,000.0,K,A*01
./set_date 21 $(gpspipe -R -n10 | sed -n "/GPRMC/,/*/p")The C program part.
In this example, argc will be 4 and argv will be as follows:
argv[0] - "./set_date"
argv[1] - "21"
argv[2] - "$GPRMC,100201.786,A,4425.1179,N,02602.8192,E,000.0,191.5,061013,,,A*61"
argv[3] - "$GPVTG,191.5,T,,M,000.0,N,000.0,K,A*01"
The GPRMC output gives 100201 for time and 061013 for date. This means:
UTC time is 10:02:01 and date is 06 October 2013. GPRMC does not provide the full year, so we have to provide the century as an argument to the C program to compute the correct date.
We are only interested in argv[1] and argv[2], so, in the C program we will convert argv[1] to int using atoi(argv[1]) and we will have the century and after this we have to parse argv[2] using sscanf to get the two numbers for time: 100201 and for date 061013. Let's assume we got these numbers in two uint32_t variables:
rawDate = 61013
rawTime = 100201
To get useful data from here we have to do this:
hour = timeRaw / 10000;After this, to create the command we can use sprintf to put everything in an outputBuffer and then call system(outputBuffer) to execute the command.
minute = (timeRaw % 10000) / 100;
second = (timeRaw % 10000) % 100;
century = atoi(argv[1]);
day = dateRaw / 10000;
month = (dateRaw % 10000) / 100;
year = (century - 1) * 100 + (dateRaw % 10000) % 100;