In this post i will show you guys how we can automatically synchronize the RTC of our GSM module with the network time. This method is very useful if we are using a board that doesnt have battery backup present on it. Once the GSM module RTC is synchronized, we can also sync the MCU RTC and also any external RTC if we are using one.
Since i had started designing the VALTRACK GPS trackers, I have always had trouble with maintaining the correct time on the tracking device because once i remove the battery, the STM32 RTC lost its synchronization. I didnt have the space or luxury to maintain a separate backup battery for the STM32 RTC.
So, i was only using the server time for the incoming packets where the time field was auto populated by the server side script.
This method was ok for certain applications but we didnt have the method to know the delayed packets time (Which get re-sent later on failure). This is when i was looking for a method to calibrate and sync the MCU RTC to the right time on power on reset.
I considered a few options,
- Sync/set RTC clock with a SMS command
- Sync RTC by bluetooth
- Sync RTC using a network time server
- Sync using network time.
Sync/set RTC clock with a SMS command :
We implemented a command to set the RTC date and time by SMS. Its working fine. But this has to be done everytime the power is removed.
Sync RTC by bluetooth :
We implemented a command on the VALTRACK-V2 setup application and added a field to set date and time. This can be only done when you have physical access to the device and are nearby it.
Sync RTC using a network time server:
We tried using a Network Time server using NTP protocol but this method is a waste of data and time and not reliable. We cannot send packets until there is a response from server.
Sync using network time :
We then implemented this method where in the GSM module synchronizes its internal RTC and then we take the clock value of the GSM module and set the clock of our STM32 RTC.
This method has been working fine so far as it doesn't need any manual intervention for calibrating the time.
Here are the commands i used:
Command to read the GSM RTC clock :
Command to enable GSM RTC network time sync :
Command to save the command settings to memory so that the time is synchronized on every boot up automatically.
Read current time (You can see that the time is not right):
Enable auto network time sync :
Check if value is set :
Save the setting to permanent memory so that module enables sync on restart also :
Restart the module and check time :
As you can see the RTC of the GSM module is always set to network time using this method and you have to do this only once.
For live demo watch the video below,