It is quite common to want your Arduino to keep actual time allowing you to timestamp data in a way that humans can understand. These dates and times can easily be interpreted by tools like Microsoft Excel and Matlab where they can be used for more in-depth analysis.

This example shows how you can use MegunoLinks MessageLibrary and MLPUIMessageProcessor controls inside an interface panel to detect and respond to a time sync message sent by the Arduino.

Time is kept using a library written by Michael Margolis and Paul Stoffregen. This time library can be downloaded from here. Install the library as you would any other Arduino library. As well as the time library we are also using our CommandHandler and ArduinoTimer libraries for receiving the time and triggering the synchronization.

The MegunoLink Interface

The first step is to configure MegunoLink (or download my example interface to set the Arduino time). Once you have MegunoLink open create a basic project with connection manager, monitor, and interface panel. The interface panel handles the command processing and time synchronizing.

MegunoLink interface showing interface panel editor button

Open MegunoLink and create a basic project with connection manager, monitor and interface panel.

Go into the interface panel designer by clicking the designer button (circled in red). The image below shows the designer. From the Toolbox on the left add a MessageLibrary and then a MLPUIMessageProcessor. The order is important so make sure the MessageLibrary is added first.

MLPUIMessageProcessor responds to serial commands it receives and the MessageLibrary object provides a way to design responses which can be executed whenever the target command is received.

Interface panel designer showing objects added to interface

Interface panel designer with MLPUIMessageProcessor and MessageLibrary objects added to interface.

In this example, we will stick with the default names and we will not specify a channel for the MLPUIMessageProcessor.

Now we need to add the command to the MessageLibrary1 object. To do that click on MessageLibrary1 and in the properties box click on the Messages property.

Red circle showing messages property

Accessing the Messages property of the message library.

The Messages property box will appear. Here you enter the name of the message, this name is what the Arduino will use in its serial message to trigger the command. In this case, I called it “TimeMessage”. Then simply add the command you want to execute, the result of which will be sent back over the serial channel. In this example I want to send the time in seconds since the 1/1/1970 (Unix time).

Editing a message in the message library dialog

Adding the time message to the message library.

Close the message library once your message has been added.

Now we need to connect the MessageLibrary1 object to the MLPUIMessageProcessor1 object. Click on MLPUIMessageProcessor1 and in the properties section, you will see a library option. Click on that and select MessageLibrary1.

How to connect the message library with message processor

Connecting the message processor and message libraries together.

Once that connection is made you can click apply (top left) in the designer so that the interface is transferred to MegunoLink. Now you can close the designer and its a good idea save your MegunoLink project.

Now whenever the Arduino sends this {UI|CMD|TimeMessage} over the serial connection the MLPUIMessageProcessor1 object will trigger and respond to the Arduino with the Unix time. The Arduino program will process this message and synchronise its clock to that time.

Build an interface panel for your Arduino sketch

The Arduino Program

The below code block shows the Arduino program. The program is fairly simple, your Arduino should attempt to sync its time every 10 seconds and it will print the time to the monitor every second.

Program your Arduino with this and then connect it to MegunoLink using the connection manager.

What you should see in MegunoLink

Once your Arduino is programmed connect it to MegunoLink and look at the monitor to see what messages are going back and forth. The screenshot below shows an example of what should occur. At startup and every 10 seconds, the Arduino sends MegunoLink a request for the time ({UI|CMD|TimeMessage}). This message is processed and the time sync message is sent back to the Arduino (!SetTime 1491919844\r\n). The Arduino used this message to sync up its clock and then it prints the time every second which you can also see in the monitor window.

Example of time being synchronized.

Serial monitor showing the Arduino time synchronize request (black) and the message sent back to the microcontroller (red).

Summary

This example demonstrates that MegunoLink can be used as a time synchronization service when coupled with a time-keeping library and our command handling and timer libraries.

Being able to provide your own timestamp is useful when wanting to log and plot data with your own timestamps rather than relying on MegunoLinks internal timestamping.

If you enjoyed this article give us a like on Facebook.

And don’t forget to subscribe if you’d like to hear about more snazzy Arduino projects.

MegunoLink_Time_Sync.mlx
MegunoLink project to send current time to an Arduino program.
Subscribe for MegunoLink and Arduino Tips, Tricks and Projects
Showing 2 comments
  • Olivier
    Reply

    Is it possible to set the time by Meguno and by Xbee for a RTC_DS1307 shield on an arduino uno ?

    • Philip Rowe
      Reply

      Don’t see why not. The same approach should work.

Leave a Comment

Start typing and press Enter to search