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
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."
"gps data is no good"
"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:
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.
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:
./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 - "./set_date"
argv - "21"
argv - "$GPRMC,100201.786,A,4425.1179,N,02602.8192,E,000.0,191.5,061013,,,A*61"
argv - "$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 and argv, so, in the C program we will convert argv to int using atoi(argv) and we will have the century and after this we have to parse argv 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);
day = dateRaw / 10000;
month = (dateRaw % 10000) / 100;
year = (century - 1) * 100 + (dateRaw % 10000) % 100;