def set_clock_via_internet(): c = untplib.NTPClient() resp = c.request('0.de.pool.ntp.org', version=3, port=123) print("Offset is ", resp.offset) from machine import RTC import time rtc = RTC() print("Adjusting clock by ", resp.offset, "seconds") rtc.init(time.localtime(time.time() + resp.offset))
def get_time_offset(): """Get the time offset between internal clock and timeserver""" import untplib # Re-try in case of connectivity problems for i in range(3): try: c = untplib.NTPClient() resp = c.request('0.uk.pool.ntp.org', version=3, port=123) return resp.offset except: time.sleep_us(500000)
# main.py -- put your code here! import untplib # get time from NTP server c = untplib.NTPClient() resp = c.request('0.uk.pool.ntp.org', version=3, port=123) print("Offset is ", resp.offset) # adjust WiPy time from machine import RTC import time rtc = RTC() print("Adjusting clock by ", resp.offset, "seconds") rtc.init(time.localtime(time.time() + resp.offset + 3600)) #+3600 for BST print(rtc.now()) wipy_time = str(time.localtime()) print("Current time", wipy_time) # send e-mail print("Sending e-mail...") import smtplib # Please replace with your e-mail credentials: to = 'YOUR_EMAIL_ADDRESS' gmail_user = '******' gmail_pwd = 'EMAIL_ACCOUNT_PASSWORD' smtpserver = smtplib.SMTP("smtp.gmail.com", 465) smtpserver.helo()
import untplib import time import wifi_connect as wlan wlan.connect() c = untplib.NTPClient() resp = c.request('pool.ntp.org', version=3, port=123) print("Offset is ", resp.offset) i = 0 while True: try: tm = time.localtime(time.time() + resp.offset) print('DATE TIME: {2:02d}/{1:02d}/{0} {3:02d}:{4:02d}:{5:02d}'.format( tm[0], tm[1], tm[2], tm[3] + 7, tm[4], tm[5])) time.sleep(1) i = i + 1 if i == 300: print('sync ntp time server...') resp = c.request('pool.ntp.org', version=3, port=123) i = 0 except untplib.NTPException as e: print(e)