The exponential number of devices come online day by day, synchronization becomes critical and with many emerging applications demanding far more accuracy, it’s important that precise timing can be ensured right across the network. In a Internet of Things (IoT) system, time is a critical factor for all the data collected. Time is recorded when sensors detected the targeted object and actuators respond based on the pre-scheduled time. Especially in data harvesting, time series data is important to extract more meaningful insight. This is the reason where many microcontrollers that are used in a IoT system are having Real Time Clock (RTC) in the chip. However, in this article, we like to share how to create real time awareness for a devices without RTC. This can be achieved when IoT devices are build upon Message Queuing Telemetry Transport (MQTT) network as shown in picture below:
Communication between devices is the basic element in a IoT system. Message Queuing Telemetry Transport (MQTT) is one of the favorite machine to machine communication protocol. It works on TCP/IP protocol. Basically, there is a central messenger which we identify it as a broker to convey all the published messages from any of connected clients to other devices that subscribed to a particular topic. You can refer to the article here if you wish to know more about MQTT. –https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/
To introduce time into this system, you need to have one main controller that can sync its clock from a NTP server from internet and perform as a timekeeper for the system. By utilizing the MQTT protocols, the time synced down can be shared among other subscribing devices that do not have RTC. The shared time should be in epoch format – It is the number of seconds that have elapsed since 00:00:00 Thursday, 1 January 1970.
Next, the clock can be continue ticking in the subscribing devices by utilizing the internal timer in the device. Once the device received the current time in epoch format, it can start its internal timer as well to count the seconds after that moment. Whenever the time need to be captured, the original received current time can be added with the number of seconds counted to become the actual current time.
This local time counting is useful whenever your device is disconnected from the MQTT network or the WIFI network, it can still record the time based data. However, the current time value need to be always updated by the timekeeper as the seconds counted by the timer might be deviated from time to time as it is highly depending on the accuracy of the quartz crystal which provide the clock signal. It is recommended that frequently update the time within the subscribing device will help to improve the time accuracy. Such technique is suitable for non-time critical application.
To further enhance its accuracy, the two-way exchange/synchronisation method can be considered where the time stamped exchange of packets between a client and a reference, where exchanges are typically initiated by the client.