我们从Python开源项目中,提取了以下17个代码示例,用于说明如何使用machine.RTC。
def run(owner): from utime import sleep sleep(0.1) from gc import collect from machine import I2C, Pin, RTC bus = I2C(-1, Pin(4), Pin(5)) from urtc import DS3231 rtc = RTC() dl = [x for x in DS3231(bus).datetime() if x] rtc.datetime(dl + [0 for x in range(0, 8 - len(dl))]) del I2C, Pin, DS3231, rtc, dl collect() sleep(0.2) from gc import collect from states import init init_state = init() owner.core.load() collect() init_state.on_enter(owner)
def set_time(rtc_time): rtc = None try: import pyb rtc = pyb.RTC() rtc.datetime(rtc_time) except: try: import machine rtc = machine.RTC() rtc.datetime(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.
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()
def get_datestring(): from gc import collect from machine import RTC d = RTC().datetime() del RTC res = "{}-{}-{} {}:{}:{}".format(str(d[0])[2:], d[1], d[2], d[4], d[5], d[6]) collect() return res
def __init__(self, id, frequency, datarate, ssid, password, server, port, ntp_server='pool.ntp.org', ntp_period=3600): self.id = id self.server = server self.port = port self.frequency = frequency self.datarate = datarate self.ssid = ssid self.password = password self.ntp_server = ntp_server self.ntp_period = ntp_period self.server_ip = None self.rxnb = 0 self.rxok = 0 self.rxfw = 0 self.dwnb = 0 self.txnb = 0 self.sf = self._dr_to_sf(self.datarate) self.bw = self._dr_to_bw(self.datarate) self.stat_alarm = None self.pull_alarm = None self.uplink_alarm = None self.wlan = None self.sock = None self.udp_stop = False self.udp_lock = _thread.allocate_lock() self.lora = None self.lora_sock = None self.rtc = machine.RTC()
def set_time(self, dt=0): import ntptime t = ntptime.time() tm = time.localtime(t + dt) tm = tm[0:3] + (0,) + tm[3:6] + (0,) machine.RTC().datetime(tm)
def go_sleep(sleep_minutes): """Perform deepsleep to save energy.""" rtc = machine.RTC() rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP) # ticks_ms is used to make wake up period more consistent sleep_seconds = (sleep_minutes * 60) - (ticks_ms() // 1000) rtc.alarm(rtc.ALARM0, sleep_seconds * 1000) print(str(sleep_seconds // 60) + ":" + str(sleep_seconds % 60) + " minutes deep sleep NOW!") machine.deepsleep()
def deepsleep(uptime=0, calibrate=False): """Put to sleep for 60 seconds minus uptime.""" rtc = machine.RTC() rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP) sleeptime = max(MIN_INTERVAL, (INTERVAL_SEC * 1000) - uptime) rtc.alarm(rtc.ALARM0, sleeptime) machine.deepsleep()
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)
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)
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()
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))
def gotosleep(sleep_timeout): # Remember for this to work GPIO16 (D0) must be connected to RST # 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 a while (waking the device) sleep_timeout = sleep_timeout * 1000 # in microseconds rtc.alarm(rtc.ALARM0, sleep_timeout) # put the device to sleep print('Sleep for %d usec' % sleep_timeout) machine.deepsleep()
def __init__(self, hours): rtc = machine.RTC() self.memory = rtc.memory self.hours = int(hours)
def start(self): if c.DEBUG_LED: pycom.rgbled(c.RED) # Change WiFi to STA mode and connect self.wlan = WLAN(mode=WLAN.STA) self._connect_to_wifi() # Get a time Sync self.rtc = machine.RTC() self.rtc.ntp_sync(self.ntp, update_period=self.ntp_period) # Get the server IP and create an UDP socket self.server_ip = socket.getaddrinfo(self.server, self.port)[0][-1] self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.sock.setblocking(False) # Push the first time immediatelly self._push_data(self._make_stat_packet()) # Create the alarms self.stat_alarm = Timer.Alarm(handler=lambda t: self._push_data(self._make_stat_packet()), s=60, periodic=True) self.pull_alarm = Timer.Alarm(handler=lambda u: self._pull_data(), s=25, periodic=True) # Start the UDP receive thread _thread.start_new_thread(self._udp_thread, ()) # Initialize LoRa in LORA mode self.lora = LoRa(mode=LoRa.LORA, frequency=self.frequency, bandwidth=LoRa.BW_125KHZ, sf=self.sf, preamble=8, coding_rate=LoRa.CODING_4_5, tx_iq=True) # Create a raw LoRa socket self.lora_sock = socket.socket(socket.AF_LORA, socket.SOCK_RAW) self.lora_sock.setblocking(False) self.lora_tx_done = False self.lora.callback(trigger=(LoRa.RX_PACKET_EVENT | LoRa.TX_PACKET_EVENT), handler=self._lora_cb) if c.DEBUG_LED: pycom.rgbled(c.LED_OFF)
def start(self): """ Starts the LoRaWAN nano gateway. """ self._log('Starting LoRaWAN nano gateway with id: {}', self.id) # setup WiFi as a station and connect self.wlan = WLAN(mode=WLAN.STA) self._connect_to_wifi() # get a time sync self._log('Syncing time with {} ...', self.ntp_server) self.rtc.ntp_sync(self.ntp_server, update_period=self.ntp_period) while not self.rtc.synced(): utime.sleep_ms(50) self._log("RTC NTP sync complete") # get the server IP and create an UDP socket self.server_ip = usocket.getaddrinfo(self.server, self.port)[0][-1] self._log('Opening UDP socket to {} ({}) port {}...', self.server, self.server_ip[0], self.server_ip[1]) self.sock = usocket.socket(usocket.AF_INET, usocket.SOCK_DGRAM, usocket.IPPROTO_UDP) self.sock.setsockopt(usocket.SOL_SOCKET, usocket.SO_REUSEADDR, 1) self.sock.setblocking(False) # push the first time immediatelly self._push_data(self._make_stat_packet()) # create the alarms self.stat_alarm = Timer.Alarm(handler=lambda t: self._push_data(self._make_stat_packet()), s=60, periodic=True) self.pull_alarm = Timer.Alarm(handler=lambda u: self._pull_data(), s=25, periodic=True) # start the UDP receive thread self.udp_stop = False _thread.start_new_thread(self._udp_thread, ()) # initialize the LoRa radio in LORA mode self._log('Setting up the LoRa radio at {:.1f} Mhz using {}', self._freq_to_float(self.frequency), self.datarate) self.lora = LoRa( mode=LoRa.LORA, frequency=self.frequency, bandwidth=self.bw, sf=self.sf, preamble=8, coding_rate=LoRa.CODING_4_5, tx_iq=True ) # create a raw LoRa socket self.lora_sock = usocket.socket(usocket.AF_LORA, usocket.SOCK_RAW) self.lora_sock.setblocking(False) self.lora_tx_done = False self.lora.callback(trigger=(LoRa.RX_PACKET_EVENT | LoRa.TX_PACKET_EVENT), handler=self._lora_cb) self._log('LoRaWAN nano gateway online')
def now(self): """Rotina a ser chamada no boot.py. 0 - coleta temperatura e publica 1 - fica em loop ate que seja publicado que pode dormir 2 - faz a rotina de dormir. Dorme no intervalo definido em self.SLEEP_TIME 3 - quando der o timeout, acorda, coleta, envia e repete. """ size = 0 print("Estabelecendo conexao de rede...") while size < 11: try: size = len(self.sta_if.ifconfig()[0]) time.sleep_ms(80) except: size = 0 print("setting callback...") self.myMQTT.set_callback(self.sub_cb) print("connecting for...") self.myMQTT.connect() print("Subscribe for sleep...") self.myMQTT.subscribe(b'freezer/sleep') print("Publishing termometer status") self.myMQTT.publish(b'freezer/termometro',b'ON') # coleta e publica temperatura... print("Coletando temperatura e publicando...") t = self.tasks() # ...aguarda por ordem de dormir... print("Aguardando ordem para dormir...") self.waiting() if self.DO_SLEEP: self.myMQTT.publish("freezer/termometro", "OFF") print("Dormindo. Ate logo.") # ...inicia processo de deepsleep. self.DO_SLEEP = False rtc = machine.RTC() rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP) # wake up after... rtc.alarm(rtc.ALARM0, self.SLEEP_TIME) # sleep machine.deepsleep()