Set RTC from PC datetime (no network connection)
-
I would like to set the RTC of a WiPy with the exact datetime on the computer it is connected to via USB. I know that
rtc.ntp_sync()
can be used to sync the RTC with a network but I will need to sync these devices in remote locations that don't have access to a wifi network (also no LoRa or LTE connection). Is there a way to query the datetime of the computer and set the WiPy RTC to that date time?
-
@alexpul Neat work around, Good job!
-
Here is a small script I wrote that sets the WiPy RTC from the PC datetime. The script is run through python on the computer and the device is connected via USB. (Note that it requires serial connection of UART0 to have a duplicated terminal - see
uos.dupterm()
in the pycom documentation for more details).import serial from datetime import datetime ## Get PC datetime pc_RTC = datetime.now() #get datetime of PC pc_datetime = pc_RTC.timetuple() #convert datetime to tuple datetime_tuple = pc_datetime[0:6] #get datetime entries in same format as WiPy RTC print(datetime_tuple) ## Set datetime over serial connection ser = serial.Serial(port='COM1', baudrate=9600, writeTimeout=1, timeout=1) # initialize serial connection input = 'from machine import RTC\r\nintRTC = RTC()\r\nintRTC.init(' + str(datetime_tuple) + ')\r\n' #WiPy commands to set RTC datetime ser.write(input) #send commands over USB serial connection
-
Given you can init the RTC with anytime you want:
# for 2nd of February 2017 at 10:30am (TZ 0) rtc.init((2017, 2, 28, 10, 30, 0, 0, 0))
I see no reasons you could not use the serial connection between a pc and the WiPy to sync time.
That said you'd need to write an application to run on said PC to write its current time out over serial.
And adjust your wipy code to read that time out of the serial and init the RTC clock.