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.
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()
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.
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....
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" (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....
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.