Python源码示例:machine.RTC
示例1
def await_transition(self): # Wait until DS3231 seconds value changes
self.ds3231.readfrom_mem_into(DS3231_I2C_ADDR, 0, self.timebuf)
ss = self.timebuf[0]
while ss == self.timebuf[0]:
self.ds3231.readfrom_mem_into(DS3231_I2C_ADDR, 0, self.timebuf)
return self.timebuf
# Get calibration factor for Pyboard RTC. Note that the DS3231 doesn't have millisecond resolution so we
# wait for a seconds transition to emulate it.
# This function returns the required calibration factor for the RTC (approximately the no. of ppm the
# RTC lags the DS3231).
# Delay(min) Outcome (successive runs). Note 1min/yr ~= 2ppm
# 5 173 169 173 173 173
# 10 171 173 171
# 20 172 172 174
# 40 173 172 173 Mean: 172.3
# Note calibration factor is not saved on power down unless an RTC backup battery is used. An option is
# to store the calibration factor on disk and issue rtc.calibration(factor) on boot.
示例2
def get_time(self, set_rtc = False):
if set_rtc:
data = self.await_transition() # For accuracy set RTC immediately after a seconds transition
else:
self.ds3231.readfrom_mem_into(DS3231_I2C_ADDR, 0, self.timebuf)
data = self.timebuf
ss = bcd2dec(data[0])
mm = bcd2dec(data[1])
if data[2] & 0x40:
hh = bcd2dec(data[2] & 0x1f)
if data[2] & 0x20:
hh += 12
else:
hh = bcd2dec(data[2])
wday = data[3]
DD = bcd2dec(data[4])
MM = bcd2dec(data[5] & 0x1f)
YY = bcd2dec(data[6])
if data[5] & 0x80:
YY += 2000
else:
YY += 1900
if set_rtc:
rtc.init((YY, MM, DD, hh, mm, ss, 0))
return (YY, MM, DD, hh, mm, ss, 0, 0) # Time from DS3231 in time.time() format (less yday)
示例3
def run(appname='home'):
rtc = machine.RTC()
rtc.memory(appname)
print('Restrt to run app {}'.format(appname))
restart()
示例4
def settime():
t = time()
import machine
import utime
tm = utime.localtime(t)
tm = tm[0:3] + (0,) + tm[3:6] + (0,)
machine.RTC().datetime(tm)
print(utime.localtime())
示例5
def settime():
t = time()
import machine
import utime
tm = utime.localtime(t)
tm = tm[0:3] + (0,) + tm[3:6] + (0,)
machine.RTC().datetime(tm)
print(utime.localtime())
示例6
def settime():
t = time()
import machine
import utime
tm = utime.localtime(t)
tm = tm[0:3] + (0,) + tm[3:6] + (0,)
machine.RTC().datetime(tm)
print(utime.localtime())
示例7
def settime():
t = time()
import machine
import utime
tm = utime.localtime(t)
tm = tm[0:3] + (0,) + tm[3:6] + (0,)
machine.RTC().datetime(tm)
print(utime.localtime())
示例8
def set_time(rtc_time):
rtc = None
try:
# Pyboard (pyboard doesn't have machine.RTC()).
# The pyb.RTC.datetime function takes the arguments in the order:
# (year, month, day, weekday, hour, minute, second, subseconds)
# http://docs.micropython.org/en/latest/library/pyb.RTC.html#pyb.RTC.datetime
import pyb
rtc = pyb.RTC()
rtc.datetime(rtc_time)
except:
try:
import pycom
# PyCom's machine.RTC takes its arguments in a slightly different order
# than the official machine.RTC.
# (year, month, day, hour, minute, second[, microsecond[, tzinfo]])
# https://docs.pycom.io/firmwareapi/pycom/machine/rtc/#rtc-init-datetime-none-source-rtc-internal-rc
rtc_time2 = (rtc_time[0], rtc_time[1], rtc_time[2], rtc_time[4], rtc_time[5], rtc_time[6])
import machine
rtc = machine.RTC()
rtc.init(rtc_time2)
except:
try:
# The machine.RTC documentation was incorrect and doesn't agree with the code, so no link
# is presented here. The order of the arguments is the same as the pyboard.
import machine
rtc = machine.RTC()
try:
# ESP8266 uses rtc.datetime() rather than rtc.init()
rtc.datetime(rtc_time)
except:
# ESP32 (at least Loboris port) uses rtc.init()
rtc.init(rtc_time)
except:
pass
# 0x0D's sent from the host get transformed into 0x0A's, and 0x0A sent to the
# host get converted into 0x0D0A when using sys.stdin. sys.tsin.buffer does
# no transformations, so if that's available, we use it, otherwise we need
# to use hexlify in order to get unaltered data.
示例9
def get_time(self, set_rtc=False):
if set_rtc:
self.await_transition() # For accuracy set RTC immediately after a seconds transition
else:
self.ds3231.readfrom_mem_into(DS3231_I2C_ADDR, 0, self.timebuf) # don't wait
return self.convert(set_rtc)
示例10
def save_time(self):
(YY, MM, mday, hh, mm, ss, wday, yday) = utime.localtime() # Based on RTC
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 0, tobytes(dec2bcd(ss)))
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 1, tobytes(dec2bcd(mm)))
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 2, tobytes(dec2bcd(hh))) # Sets to 24hr mode
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 3, tobytes(dec2bcd(wday + 1))) # 1 == Monday, 7 == Sunday
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 4, tobytes(dec2bcd(mday))) # Day of month
if YY >= 2000:
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 5, tobytes(dec2bcd(MM) | 0b10000000)) # Century bit
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 6, tobytes(dec2bcd(YY-2000)))
else:
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 5, tobytes(dec2bcd(MM)))
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 6, tobytes(dec2bcd(YY-1900)))
# Wait until DS3231 seconds value changes before reading and returning data
示例11
def await_transition(self):
self.ds3231.readfrom_mem_into(DS3231_I2C_ADDR, 0, self.timebuf)
ss = self.timebuf[0]
while ss == self.timebuf[0]:
self.ds3231.readfrom_mem_into(DS3231_I2C_ADDR, 0, self.timebuf)
return self.timebuf
# Test hardware RTC against DS3231. Default runtime 10 min. Return amount
# by which DS3231 clock leads RTC in PPM or seconds per year.
# Precision is achieved by starting and ending the measurement on DS3231
# one-seond boundaries and using ticks_ms() to time the RTC.
# For a 10 minute measurement +-1ms corresponds to 1.7ppm or 53s/yr. Longer
# runtimes improve this, but the DS3231 is "only" good for +-2ppm over 0-40C.
示例12
def rtc_test(self, runtime=600, ppm=False, verbose=True):
if rtc is None:
raise RuntimeError('machine.RTC does not exist')
verbose and print('Waiting {} minutes for result'.format(runtime//60))
factor = 1_000_000 if ppm else 114_155_200 # seconds per year
self.await_transition() # Start on transition of DS3231. Record time in .timebuf
t = utime.ticks_ms() # Get system time now
ss = rtc.datetime()[6] # Seconds from system RTC
while ss == rtc.datetime()[6]:
pass
ds = utime.ticks_diff(utime.ticks_ms(), t) # ms to transition of RTC
ds3231_start = utime.mktime(self.convert()) # Time when transition occurred
t = rtc.datetime()
rtc_start = utime.mktime((t[0], t[1], t[2], t[4], t[5], t[6], t[3] - 1, 0)) # y m d h m s wday 0
utime.sleep(runtime) # Wait a while (precision doesn't matter)
self.await_transition() # of DS3231 and record the time
t = utime.ticks_ms() # and get system time now
ss = rtc.datetime()[6] # Seconds from system RTC
while ss == rtc.datetime()[6]:
pass
de = utime.ticks_diff(utime.ticks_ms(), t) # ms to transition of RTC
ds3231_end = utime.mktime(self.convert()) # Time when transition occurred
t = rtc.datetime()
rtc_end = utime.mktime((t[0], t[1], t[2], t[4], t[5], t[6], t[3] - 1, 0)) # y m d h m s wday 0
d_rtc = 1000 * (rtc_end - rtc_start) + de - ds # ms recorded by RTC
d_ds3231 = 1000 * (ds3231_end - ds3231_start) # ms recorded by DS3231
ratio = (d_ds3231 - d_rtc) / d_ds3231
ppm = ratio * 1_000_000
verbose and print('DS3231 leads RTC by {:4.1f}ppm {:4.1f}mins/yr'.format(ppm, ppm*1.903))
return ratio * factor
示例13
def get_time(self, set_rtc=False):
if set_rtc:
self.await_transition() # For accuracy set RTC immediately after a seconds transition
else:
self.ds3231.readfrom_mem_into(DS3231_I2C_ADDR, 0, self.timebuf) # don't wait
return self.convert(set_rtc)
示例14
def getcal(self, minutes=5, cal=0, verbose=True):
if d_series:
return self._getcal_d(minutes, cal, verbose)
verbose and print('Pyboard 1.x. Waiting {} minutes for calibration factor.'.format(minutes))
rtc.calibration(cal) # Clear existing cal
self.save_time() # Set DS3231 from RTC
self.await_transition() # Wait for DS3231 to change: on a 1 second boundary
tus = utime.ticks_us()
st = rtc.datetime()[7]
while rtc.datetime()[7] == st: # Wait for RTC to change
pass
t1 = utime.ticks_diff(utime.ticks_us(), tus) # t1 is duration (μs) between DS and RTC change (start)
rtcstart = get_ms(rtc.datetime()) # RTC start time in mS
dsstart = utime.mktime(self.convert()) # DS start time in secs as recorded by await_transition
utime.sleep(minutes * 60)
self.await_transition() # DS second boundary
tus = utime.ticks_us()
st = rtc.datetime()[7]
while rtc.datetime()[7] == st:
pass
t2 = utime.ticks_diff(utime.ticks_us(), tus) # t2 is duration (μs) between DS and RTC change (end)
rtcend = get_ms(rtc.datetime())
dsend = utime.mktime(self.convert())
dsdelta = (dsend - dsstart) * 1000000 # Duration (μs) between DS edges as measured by DS3231
if rtcend < rtcstart: # It's run past midnight. Assumption: run time < 1 day!
rtcend += 24 * 3_600_000
rtcdelta = (rtcend - rtcstart) * 1000 + t1 - t2 # Duration (μs) between DS edges as measured by RTC and corrected
ppm = (1000000* (rtcdelta - dsdelta))/dsdelta
if cal:
verbose and print('Error {:4.1f}ppm {:4.1f}mins/year.'.format(ppm, ppm * 1.903))
return 0
cal = int(-ppm / 0.954)
verbose and print('Error {:4.1f}ppm {:4.1f}mins/year. Cal factor {}'.format(ppm, ppm * 1.903, cal))
return cal
# Version for Pyboard D. This has μs resolution.
示例15
def _getcal_d(self, minutes, cal, verbose):
verbose and print('Pyboard D. Waiting {} minutes for calibration factor.'.format(minutes))
rtc.calibration(cal) # Clear existing cal
self.save_time() # Set DS3231 from RTC
self.await_transition() # Wait for DS3231 to change: on a 1 second boundary
t = rtc.datetime() # Get RTC time
# Time of DS3231 transition measured by RTC in μs since start of day
rtc_start_us = get_us(t)
dsstart = utime.mktime(self.convert()) # DS start time in secs
utime.sleep(minutes * 60)
self.await_transition() # Wait for DS second boundary
t = rtc.datetime()
# Time of DS3231 transition measured by RTC in μs since start of day
rtc_end_us = get_us(t)
dsend = utime.mktime(self.convert()) # DS end time in secs
if rtc_end_us < rtc_start_us: # It's run past midnight. Assumption: run time < 1 day!
rtc_end_us += 24 * 3_600_000_000
dsdelta = (dsend - dsstart) * 1_000_000 # Duration (μs) between DS3231 edges as measured by DS3231
rtcdelta = rtc_end_us - rtc_start_us # Duration (μs) between DS edges as measured by RTC
ppm = (1_000_000 * (rtcdelta - dsdelta)) / dsdelta
if cal: # We've already calibrated. Just report results.
verbose and print('Error {:4.1f}ppm {:4.1f}mins/year.'.format(ppm, ppm * 1.903))
return 0
cal = int(-ppm / 0.954)
verbose and print('Error {:4.1f}ppm {:4.1f}mins/year. Cal factor {}'.format(ppm, ppm * 1.903, cal))
return cal
示例16
def calibrate(self, minutes=5):
cal = self.getcal(minutes)
rtc.calibration(cal)
print('Pyboard RTC is calibrated. Factor is {}.'.format(cal))
return cal
示例17
def setup_rtc():
rtc = machine.RTC()
rtc.ntp_sync("pool.ntp.org")
while not rtc.synced():
utime.sleep_ms(100)
utime.timezone(3600)
示例18
def now(): # Return the current time from the RTC in millisecs from year 2000
secs = utime.time()
ms = int(rtc.now()[6]/1000)
if ms < 50: # Might have just rolled over
secs = utime.time()
return 1000 * secs + ms
示例19
def delta(self): # Return no. of mS RTC leads DS3231
self.await_transition()
rtc_ms = now()
t_ds3231 = utime.mktime(self.get_time()) # To second precision, still in same sec as transition
return rtc_ms - 1000 * t_ds3231
示例20
def set_time():
ntptime.settime()
tm = utime.localtime()
tm = tm[0:3] + (0,) + tm[3:6] + (0,)
machine.RTC().datetime(tm)
print('current time: {}'.format(utime.localtime()))
示例21
def set_datetime(year, month, day, hour=0, minute=0, second=0) :
rtc = machine.RTC()
rtc.datetime((year, month, day, hour, minute, second, 0, 0))
示例22
def deep_sleep(secs) :
import machine
# configure RTC.ALARM0 to be able to wake the device
rtc = machine.RTC()
rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
# set RTC.ALARM0 to fire after 10 seconds (waking the device)
rtc.alarm(rtc.ALARM0, secs)
# put the device to sleep
machine.deepsleep()
示例23
def set_datetime(secs) :
import utime
import machine
tm = utime.localtime(secs)
tm = tm[0:3] + (0,) + tm[3:6] + (0,)
machine.RTC().datetime(tm)
示例24
def set_datetime(secs):
import utime
import machine
tm = utime.localtime(secs)
tm = tm[0:3] + (0,) + tm[3:6] + (0,)
machine.RTC().datetime(tm)
示例25
def _sync():
s = 1
while True:
print("Synchronize time from NTP server ...")
try:
ntptime.settime()
gc.collect()
tm = time.localtime()
hour = tm[3] + config.RTC_TIMEZONE_OFFSET
day = tm[2]
if hour > 24:
hour -= 24
day += 1
elif hour < 0:
hour += 24
day -= 1
tm = tm[0:2] + (day,) + (0,) + (hour,) + tm[4:6] + (0,)
machine.RTC().datetime(tm)
print("Set time to", time.localtime())
s = 1
await asyncio.sleep(18000) # every 5h
except Exception as e:
await logging.getLogger("wifi").asyncLog("error",
"Error syncing time: {!s}, retry in {!s}s".format(
e, s))
await asyncio.sleep(s)
s += 5
# should prevent crashes because previous request was not finished and
# sockets still open (Errno 98 EADDRINUSE). Got killed by WDT after a few minutes.
示例26
def _sync():
s = 1
while True:
print("Synchronize time from NTP server ...")
try:
ntptime.settime()
gc.collect()
tm = time.localtime()
hour = tm[3] + config.RTC_TIMEZONE_OFFSET
day = tm[2]
if hour > 24:
hour -= 24
day += 1
elif hour < 0:
hour += 24
day -= 1
tm = tm[0:2] + (day,) + (0,) + (hour,) + tm[4:6] + (0,)
machine.RTC().datetime(tm)
print("Set time to", time.localtime())
s = 1
await asyncio.sleep(18000) # every 5h
except Exception as e:
await logging.getLogger("wifi").asyncLog("error",
"Error syncing time: {!s}, retry in {!s}s".format(
e, s))
await asyncio.sleep(s)
s += 5
# should prevent crashes because previous request was not finished and
# sockets still open (Errno 98 EADDRINUSE). Got killed by WDT after a few minutes.
示例27
def deepsleep(sleeping_time, wait_before_sleep=None, event=None):
if wait_before_sleep is not None:
await asyncio.sleep(wait_before_sleep)
if event is not None:
await event
if platform == "esp32_LoBo":
machine.deepsleep(int(sleeping_time * 1000))
else:
rtc = machine.RTC()
rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
rtc.alarm(rtc.ALARM0, int(sleeping_time * 1000))
machine.deepsleep()
示例28
def __init__(self, id=0, timeout=120, use_rtc_memory=True):
self._timeout = timeout / 10
self._counter = 0
self._timer = machine.Timer(id)
self._use_rtc_memory = use_rtc_memory
self._has_filesystem = False
self.init()
asyncio.get_event_loop().create_task(self._resetCounter())
""" Done in pysmartnode.main
if use_rtc_memory and platform == "esp8266":
rtc = machine.RTC()
if rtc.memory() == b"WDT reset":
logging.getLogger("WDT").critical("Reset reason: Watchdog")
rtc.memory(b"")
elif sys_vars.hasFilesystem():
self._has_filesystem = True
try:
with open("reset_reason.txt", "r") as f:
if f.read() == "True":
logging.getLogger("WDT").warn("Reset reason: Watchdog")
except Exception as e:
print(e) # file probably just does not exist
try:
os.remove("reset_reason.txt")
except Exception as e:
logging.getLogger("WDT").error("Error saving to file: {!s}".format(e))
"""
示例29
def _wdt(self, t):
self._counter += self._timeout
if self._counter >= self._timeout * 10:
if self._use_rtc_memory and platform == "esp8266":
rtc = machine.RTC()
rtc.memory(b"WDT reset")
elif self._has_filesystem:
try:
with open("reset_reason.txt", "w") as f:
f.write("WDT reset")
except Exception as e:
print("Error saving to file: {!s}".format(e))
machine.reset()
示例30
def sleep(self, milliseconds):
# To be able to use this fea
import machine
# configure RTC.ALARM0 to be able to wake the device
rtc = machine.RTC()
rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
# set RTC.ALARM0 to fire after some milliseconds
rtc.alarm(rtc.ALARM0, milliseconds)
# put the device to sleep
machine.deepsleep()