Arduino UNIX Time - Syncing Computer UNIX Time to Arduino Time with Python

Just finished a quick Python script to send the current unix time over to the Arduino from macOS, so in the absence of GPS or some other way to get the unix timestamp (epoch time) to the Arduino, I can get my macOS and Arduino UNO synced to within a second.

Normally, when the Arduino starts up, Arduino run time starts at 0; so I created a Python script, which I will share in a future post (as well as the Arduino sketch), to send the macOS unix time over to the Arduino.

1 Like

Here is a quick sketch I put together for the Arduino UNO to get the unix time from the usb serial port and sync the Arduino time to the computer (in my case macos).

This sketch works but needs more refinement (trap errors, disconnects, add options, etc) but since I will not be doing much with this for the next few weeks, I post the first working draft below and will also post the python script I use on macos soon:

/*
  Sync UNIX Time with Computer and
  LCD Display with I2C on Arduino UN0
  Rough Draft 0.1  (needs improvements)
  Neo December 2019
  https://www.unix.com
*/

// Include Wire Library for I2C
#include <Wire.h>
// Include NewLiquidCrystal Library for I2C
#include <LiquidCrystal_I2C.h>
#include <Time.h>
#include <TimeLib.h>

#define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by Unix time_t as ten ASCII digits
#define TIME_HEADER 'T' // Header tag for serial time sync message
#define TIME_REQUEST 7  // ASCII bell character requests a time sync message
#define DEBUG_SKETCH false
// Define LCD pinout
const int en = 2, rw = 1, rs = 0, d4 = 4, d5 = 5, d6 = 6, d7 = 7, bl = 3;
// can be any integer greater than zero and less than recent unix time
const int time_int = 1000;
// Define I2C Address - change if reqiuired
const int i2c_addr = 0x27;
String s;
bool flag = true;
time_t t = now();
LiquidCrystal_I2C lcd(i2c_addr, en, rw, rs, d4, d5, d6, d7, bl, POSITIVE);
void setup()
{
    Serial.begin(9600);
    Wire.begin();
    time_t t = now();
    lcd.begin(16, 2);
    lcd.clear();
}

void loop()
{
    lcd.clear();
    int count = 0;
    if (Serial.available() and flag)
    {
        lcd.clear();
        lcd.setCursor(0, 1);
        lcd.print("Serial OK");
        count = processSyncMessage();
        if (DEBUG_SKETCH)
        {
               Serial.println("Serial Available: " + count);
        }
        if (count > time_int)
            flag = false;
    }

    lcd.setCursor(0, 0);
    if (timeStatus() == timeNotSet)
    {
        if (flag)
        {
            Serial.println("waiting for sync message");
            lcd.print("Arduino Time:");
        }
    }
    else
    {

        lcd.print("Unix Time:");
        if (DEBUG_SKETCH)
        {
            Serial.println("Time Synced");
        }
    }
    t = now();
    s = String(t);
    if (s != '0')
    {
        lcd.setCursor(0, 1);
        lcd.print(t);
        lcd.print(" secs");
    }
    Serial.println(s);
    delay(1000);
    lcd.clear();
}

int processSyncMessage()
{
    while (Serial.available() >= TIME_MSG_LEN)
    { // time message consists of header & 10 ASCII digits
        char c = Serial.read();
        int count = 0;
        Serial.print(c);
        time_t pctime = 0;
        if (c == TIME_HEADER)
        {
            for (int i = 0; i < TIME_MSG_LEN - 1; i++)
            {
                c = Serial.read();
                if (c >= '0' && c <= '9')
                {
                    Serial.print(c);
                    count++;
                    pctime = (10 * pctime) + (c - '0'); // convert digits to a number
                }
            }
            Serial.print("\n");
            if (DEBUG_SKETCH)
            {
                Serial.write(pctime + "\n");
                Serial.println(pctime);
            }
            setTime(pctime); // Sync Arduino clock to the time received on the serial port
        }
        return count;
    }
}

Here is the Python 2.7.16 code I quickly put together to send unix time to the Arduino UNO.

I assume it also works on Python3, but I have not tested it because I have not completely upgrade python on my mac pro yet, sorry about that.

# -----------------------------------------------------------
# Sync Unix Time with Arduino Initial Draft 0.1
# Written on macOS, tested with Python 2.7.16
# Neo, December 2019
# This code needs refinement to be more general
# I will improve this code later when I need to use it in an Arduino project
# For now, it works "OK" and serves it purpose
# Feel free to improve, modify as you like.
# -----------------------------------------------------------
import serial
import time

ser = serial.Serial(
    port='/dev/cu.usbserial-40',  #change this for your device
    baudrate=9600,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout=0)

print("connected to: " + ser.portstr)

# this will store the line
line = []
ux = 0
a = False
debugScript = False
offset = 2 .  #adjust this time offset (in seconds) to account for various time delays in script and transferring via the serial port, etc. to insure the Arduino is in closer sync to the computer
time_int = 1000  # this can be any number greater than zero and less than recent unix time
count = 0
while True:
    if debugScript:
        print(str(int(time.time())))
    for c in ser.read():
        if not a:
            if ux < time_int:
                ser.write("T")
                ux = int(time.time())+offset
                ser.write(str(ux))
                ser.write("\n")
                ser.flush()
                if debugScript:
                    print("unixtime: " + str(ux))
        line.append(c)
        count += 1
        if c == '\n':
            print("Line("+str(count)+"): " + ''.join(line))
            x = int(time.time())+offset
            print("time("+str(count)+"): " + str(x) + "\n")
            line = []
            a = True
            break
ser.close()

Maybe I'll make a video and show this in more detail at some point in time (if I get the free time to make videos)

When I revise this code, I'll add some feature to detect when the usb serial port is available and sync when detected, and I'll add more error checking, trapping code and some options.

Detecting and automatic syncing was trivial to do with cron, crude and rude but it works (but it can be slow, based on the 1 minute cron):

macos# crontab -l
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.6hwbckCLEy installed on Tue Dec 24 14:09:49 2019)
# (Cron version -- $FreeBSD: src/usr.sbin/cron/crontab/crontab.c,v 1.24 2006/09/03 17:52:19 ru Exp $)
* * * * * /usr/bin/python /usr/local/bin/neo/checkArduino.py
macos# cat /usr/local/bin/neo/checkArduino.py
import os.path
from os import path

device = '/dev/cu.usbserial-40'
syncscript = '/usr/local/bin/neo/sync_unixtime.py'
if path.exists(device):
   execfile(syncscript)

The Arduino sketch will not sync after the unixtime is set the first time (in my current draft sketch above), so this will work OK for now.

I could set a PID or some flag in the filesystem to check if already synced, don't run the sync script again; but it's OK for now.

So I think this simple crontab is adequate for my current tests and experiments with the Arduino.

It even works when I press the reset button on the Arduino UNO, which is cool.

Also, on macOS, I added this to launchd so it would sync the unix time with the Arduino UNO on startup:

macOS:LaunchDaemons# ls -l *neo*
-rw-r--r--  1 root  wheel  453 Dec 24 15:30 com.neo.startup.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>com.neo.startup</string>
        <key>ProgramArguments</key>
        <array>
                <string>/usr/bin/python</string>
                <string>/usr/local/bin/neo/checkArduino.py</string>
        </array>
        <key>StartInterval</key>
        <integer>1</integer>
</dict>
</plist>
macOS:LaunchDaemons# launchctl load -w /Library/LaunchDaemons/com.neo.startup.plist

Unfortunately, because this python script is not a "proper" macOS launch daemon, it will not "keep alive", so I still use cron for that. Maybe I'll find a way to fix this later. Maybe I need to move it to LaunchScripts versus LaunchDaemons ? Anyway.... think I'll "call it good" on the unix time sync experiment for now... and as we all know....

It's only rock and roll, and I like it.

As a side note:

I notice that when I use launchd and cron on my mac to keep an Arduino UNO in sync, that I cannot update the Arduino code though the IDE unless I unload the launchd file, stop the cron entry (actually, if fast do not need to modify cron) and power cycle the Arduino.

This takes more time than I like (unloading and loading launchd , editing and restarting cron twice, hard resetting the Arduino), so as a work around, I can update the Arduino on a different mac and then connect the updated Arduino back to the mac running launchd and cron on the required serial port. Note: If I'm fast, can get by not editing and restarting the crontab.

Today, I am experimenting with a 650nm (red) laser and was going to use this little laser to transmit and sync unix time to a remote receiver; but I don't have an optical receiver in my parts box yet.

I do have a long range RF transmitter and receiver in the mail from China, as well as a 3G module, so I maybe I'll transmit and sync unix time to my Ninja, for fun.

Actually, I need to think of some very cool, "science-project-like" Arduino project which is not already on YT or in the Arduino forums. I am thinking to do something with my motorcycle, time, position, and speed via 3G back to my home and store the results (tracking) in a database; but that sounds "too easy" :slight_smile: (waiting on parts). Then I thought, I would create a RF network and transmit unix time as a beacon, but we have GPS for that. Haha....

Any crazy ideas (not already on YT)?

Quote by Neo..............

Yes, loads!!! How many "Crazy Ideas" would you like?

And if you want to go smaller take a look at the ATtiny85's which you can use an Arduno to program.

It just goes on and on.

1 Like

Oh, and I forgot about this heavy stuff:

Top 100+ Arduino Projects for Engineering Students - Pantech Blog

And this is the website of the author whose series of books taught me most:

SimonMonk.org - books by Simon Monk

(I won't post anymore links; there's plenty out there to find.)

2 Likes

Thanks Dennis,

I'll scan all those lists and see if anything looks interesting to build and modify.

I may just wait for my robot kit and modify it by adding a Elecrow GSM/GPRS/EDGE SIM5360E 3G Shield (both on the way from AliExpress) and play around with networking robots across the Internet.

https://www.aliexpress.com/item/32814966264.html

Right now, I've decided to work on an NB-IoT / LoRa project.

Cheers.