Last active
July 7, 2023 06:59
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# @2021 Create by Tawatchai Insree | |
import smbus | |
import time | |
import datetime | |
import os, subprocess | |
address = 0x68 | |
register = 0x00 | |
#sec min hour week day month year | |
w = ["Mon","Tues","Wed","Thur","Fri","Sat","Sun"] | |
m = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'] | |
#/dev/i2c-1 | |
bus = smbus.SMBus(1) | |
def ds1307SetTime(now_time): | |
bus.write_i2c_block_data(address,register, now_time) | |
def ds3231ReadTime(): | |
return bus.read_i2c_block_data(address,register,7) | |
def ping(host): | |
command = ['ping', '-c', '1', host, "-w 1"] | |
status = subprocess.call(command) == 0 | |
return status | |
i = 0 | |
if __name__ == "__main__": | |
while i!=3: | |
t = ds3231ReadTime() | |
t[0] = t[0]&0x7F #sec | |
t[1] = t[1]&0x7F #min | |
t[2] = t[2]&0x3F #hour | |
t[3] = t[3]&0x07 #week | |
t[4] = t[4]&0x3F #day | |
t[5] = t[5]&0x1F #month | |
# print("20%x/%x/%x %x:%x:%x %s" %(t[6],t[5],t[4],t[2],t[1],t[0],w[t[3]-1])) | |
rtc_time = [hex(t[6]),hex(t[5]),hex(t[4]),hex(t[2]),hex(t[1]),hex(t[0])] | |
# print(rtc_time) | |
now = datetime.datetime.now() | |
machine_time = now.strftime("%y:%m:%d:%H:%M:%S").split(':') | |
machine_time[0] = hex(int('0x'+machine_time[0], 16)) | |
machine_time[1] = hex(int('0x'+machine_time[1], 16)) | |
machine_time[2] = hex(int('0x'+machine_time[2], 16)) | |
machine_time[3] = hex(int('0x'+machine_time[3], 16)) | |
machine_time[4] = hex(int('0x'+machine_time[4], 16)) | |
machine_time[5] = hex(int('0x'+machine_time[5], 16)) | |
# print(machine_time) | |
try: | |
if ping('google.com') == True: | |
print("Connected to the Internet") | |
os.system('hwclock -w') | |
ds1307SetTime([int(machine_time[5], 16), int(machine_time[4], 16), int(machine_time[3], 16), int(hex(int('0x'+ str(datetime.datetime.today().weekday()), 16)),16), int(machine_time[2], 16), int(machine_time[1], 16), int(machine_time[0], 16)]) | |
except Exception as identifier: | |
print("No internet connection.") | |
if machine_time > rtc_time: | |
ds1307SetTime([int(machine_time[5], 16), int(machine_time[4], 16), int(machine_time[3], 16), int(hex(int('0x'+ str(datetime.datetime.today().weekday()), 16)),16), int(machine_time[2], 16), int(machine_time[1], 16), int(machine_time[0], 16)]) | |
pass | |
elif machine_time < rtc_time: | |
# print('date -s "' + str(rtc_time[2]).replace('0x',''), m[int(str(rtc_time[1]).replace('0x',''))], str(rtc_time[0]).replace('0x','20'), str(rtc_time[3]).replace('0x','')+':'+str(rtc_time[4]).replace('0x','')+':'+str(rtc_time[5]).replace('0x','')+'"') | |
os.system('date -s "' + str(rtc_time[2]).replace('0x','') + ' ' + m[int(str(rtc_time[1]).replace('0x',''))] + ' ' + str(rtc_time[0]).replace('0x','20') + ' ' + str(rtc_time[3]).replace('0x','')+':'+str(rtc_time[4]).replace('0x','')+':'+str(rtc_time[5]).replace('0x','')+'"') | |
os.system('hwclock -w') | |
else: | |
print('ok') | |
pass | |
i+=1 | |
time.sleep(1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment