def setup_rtc(): rtc = machine.RTC() rtc.ntp_sync("pool.ntp.org") while not rtc.synced(): utime.sleep_ms(100) utime.timezone(-25200) # GMT-7, Arizona (no DST) print('Clock synced, current time {}.\n'.format(utime.time()))
def setRTCLocalTime(): rtc = machine.RTC() rtc.ntp_sync("pool.ntp.org") utime.sleep_ms(750) print('\nRTC Set from NTP to UTC:', rtc.now()) utime.timezone(3600) print('Adjusted from UTC to EST timezone', utime.localtime(), '\n')
def setup_gps(): time.sleep(2) gc.enable() rtc = RTC() rtc.ntp_sync("pool.ntp.org") utime.sleep_ms(750) print('\nRTC Set from NTP to UTC:', rtc.now()) utime.timezone(7200) print('Adjusted from UTC to EST timezone', utime.localtime(), '\n') if rtc.now()[0] == 1970: print("Datetime could not be automatically set") date_str = (input( 'Enter datetime as list separated by commas (y, m, d, h, min, s): ' )).split(',') date_str = tuple([int(item) for item in date_str]) try: rtc.init(date_str) print('Time successfully set to ', rtc.now(), '\n') except Exception: print("Failed to set time...") py = Pytrack() l76 = L76GNSS(py, timeout=30) print("GPS Timeout is {} seconds".format(30)) chrono = Timer.Chrono() chrono.start() # while (True): # coord = l76.coordinates(debug=True) # print("{} - {} - {}".format(coord, rtc.now(), gc.mem_free())) return l76
def setRTCLocalTime(): rtc = machine.RTC() rtc.ntp_sync("pool.ntp.org") utime.sleep_ms(750) print('\nRTC establablecido desde NTP a UTC:', rtc.now()) utime.timezone(7200) print('Ajuste de zona horaria a GMT+2', utime.localtime(), '\n')
def setup_time(diff): rtc = machine.RTC() rtc.ntp_sync("pool.ntp.org") utime.sleep_ms(2000) print('\nRTC Set from NTP to UTC:', rtc.now()) utime.timezone(diff) print('Adjusted from UTC to local timezone', utime.localtime(), '\n')
def setRTCLocalTime(): rtc.ntp_sync("pool.ntp.org") utime.sleep_ms(750) print('\nRTC Set from NTP to UTC:', rtc.now()) utime.timezone(60*60) print('Adjusted from UTC to +1 timezone', utime.localtime(), '\n')
def setRTC(): print("Updating RTC from {} ".format(NTP_SERVER), end='') rtc.ntp_sync(NTP_SERVER) utime.timezone(-14400) while not rtc.synced(): print('.', end='') time.sleep(1) print(' OK')
def setRTCLocalTime(): rtc = machine.RTC() rtc.ntp_sync("pool.ntp.org") mytime = rtc.now() utime.sleep_ms(750) print('\nRTC Set from NTP to UTC:', mytime) utime.timezone(+3600) print(utime.localtime()) print(time.time()) mytime = rtc.now() myseconds = calcMicroseconds(time.time(), mytime[6]) print(myseconds)
def initialize_rtc(): rtc = machine.RTC() rtc.ntp_sync("pool.ntp.org") utime.sleep(5) if (3, 1, 2, 0, 0) <= rtc.now() < (11, 1, 2, 0, 0): tz_offset = -4 * 3600 else: tz_offset = -5 * 3600 if utime.timezone() != tz_offset: utime.timezone(tz_offset) gc.collect() utime.sleep(1)
def get_time(): """Sending of the Messages to the LoRa SERVER for getting the datetime """ pycom.heartbeat(False) # LED IN BLUE (TRYING TO SEND MESSAGE) pycom.rgbled(0x0000FF) s.setblocking(True) s.settimeout(20) print('Trying to send message') try: s.send(cbor.dumps("get_time")) print('Message sent!') except: print('Failed to send message!') rgbled(0xFF0000) #LED RED pycom.rgbled(0x00FF00) #LED GREEN #LED IN BLUE (WAITING FOR DATA) print('Waiting for Data...') pycom.rgbled(0x0000FF) msg_nb_try = 0 msg_send_ok = False while((msg_send_ok == False) and (msg_nb_try < 3)): # If retry, wait before retrying if(msg_nb_try > 1): time.sleep(10) msg_nb_try = msg_nb_try + 1 try: data = s.recv(64) print('Data Received:') print(cbor.loads(data)) if 'datetime' in data.keys(): utime.timezone((data['datetime'] - utime.time())) print('datetime updated : ' + str(data['datetime'])) msg_send_ok = True except: print ('timeout in receive') pycom.rgbled(0xFF0000) #LED IN GREEN (DATA RECEIVED) pycom.rgbled(0x00FF00) s.setblocking(False)
def getGPS(py, max_samples): """ Get GPS lng/lat by performing multiple GPS collections and then returning the most often seen results , after power on this can take a while but once coordinates stabilize the results will be returned """ early_end_count = 10 # Create a dictionary of coords and count coord_dict = {} gc.enable() # setup rtc rtc = machine.RTC() rtc.ntp_sync("pool.ntp.org") utime.sleep_ms(750) # print('\nRTC Set from NTP to UTC:', rtc.now()) utime.timezone(7200) # print('Adjusted from UTC to EST timezone', utime.localtime(), '\n') l76 = L76GNSS(py, timeout=30) valid_coord_count = 0 print("getGPS: ", end="") for sample_number in range(max_samples): # time.sleep(1) coord = l76.coordinates() if coord[0] is None or coord[1] is None: blink(1, 0xff0000) # red print(".", end='') else: blink(1, 0x00ff00) # green print("^", end='') valid_coord_count += 1 if coord in coord_dict: coord_dict[coord] += 1 else: coord_dict[coord] = 1 # print("coord_dict", coord_dict) # print("{} - {}".format(coord, sample_number)) # End early if we have enough coord samples if valid_coord_count > early_end_count: print(" ") return most_common_coord(coord_dict) print(" ") return most_common_coord(coord_dict)
def setup(self): time.sleep(2) gc.enable() # setup rtc rtc = machine.RTC() rtc.ntp_sync("pool.ntp.org") utime.sleep_ms(750) utime.timezone(7200) py = Pytrack() self.gnss = L76GNSS(py, timeout=30) print("Waiting for GNSS connection...") # Unfortunately, the tuple comparison method - cmp() - is not available in MicroPython while(self.gnss.coordinates()[0] == None and self.gnss.coordinates()[1] == None): pass print("GNSS connection acquired")
def setRTCLocalTime(): rtc = machine.RTC() rtc.ntp_sync("pool.ntp.org") utime.sleep_ms(750) # utime.timezone(+18000) utime.timezone(25200) time = utime.localtime() # print("SET MANUAL TIME : " + str(time[2]) + "-" + str(time[1]) + "-" + str(time[0]) + " " + str(time[3]+12)+":"+str(time[4])+":"+str(time[5])) if (time[1] < 10): set_time = str(time[2]) + "/" + "0" + str(time[1]) + "/" + str( time[0]) + " " + str(time[3]) + ":" + str(time[4]) + ":" + str( time[5]) elif (time[4] < 10): set_time = str(time[2]) + "/" + str(time[1]) + "/" + str( time[0]) + " " + str(time[3]) + ":0" + str(time[4]) + ":" + str( time[5]) elif (time[5] < 10): set_time = str(time[2]) + "/" + str(time[1]) + "/" + str( time[0]) + " " + str(time[3]) + ":" + str(time[4]) + ":0" + str( time[5]) elif (time[4] < 10 and time[5] < 10): set_time = str(time[2]) + "/" + str(time[1]) + "/" + str( time[0]) + " " + str(time[3]) + ":0" + str(time[4]) + ":0" + str( time[5]) elif (time[4] < 10 and time[5] < 10 and time[1] < 10): set_time = str(time[2]) + "/" + "0" + str(time[1]) + "/" + str( time[0]) + " " + str(time[3]) + ":0" + str(time[4]) + ":0" + str( time[5]) else: set_time = str(time[2]) + "/" + str(time[1]) + "/" + str( time[0]) + " " + str(time[3]) + ":" + str(time[4]) + ":" + str( time[5]) set_time = str(set_time) return set_time
from machine import SD from machine import Timer from L76GNSS import L76GNSS from pytrack import Pytrack # setup as a station import gc time.sleep(2) gc.enable() # setup rtc rtc = machine.RTC() rtc.ntp_sync("pool.ntp.org") utime.sleep_ms(750) print('\nRTC Set from NTP to UTC:', rtc.now()) utime.timezone(7200) print('Adjusted from UTC to EST timezone', utime.localtime(), '\n') py = Pytrack() l76 = L76GNSS(py, timeout=30) chrono = Timer.Chrono() chrono.start() #sd = SD() #os.mount(sd, '/sd') #f = open('/sd/gps-record.txt', 'w') while (True): coord = l76.coordinates() #f.write("{} - {}\n".format(coord, rtc.now())) print("{} - {} - {}".format(coord, rtc.now(), gc.mem_free()))
if use_WebServer: print("Starting Webserver") routes = WWW_routes() mws = MicroWebSrv(routeHandlers=routes, webPath=webFilePath) gc.collect() mws.Start() gc.collect() print('Starting Clocks') if network_OK: print(' Syncing RTC to ' + ntp_source) rtc.ntp_sync(ntp_source) utime.sleep_ms(1500) print(' RTC Time :', rtc.now()) utime.timezone(TZ_offset_secs) print(' Local Time:', utime.localtime()) print("Starting SD Card") sd = SD() os.mount(sd, '/sd') maxIndex = 0 # loop through all the files on the SD card for f in os.listdir('/sd'): #look for GPSlognnnn files if f[:6] == 'GPSlog': try: # extract the number from the GPSlognnnn filename index = int(f[6:].split(".")[0]) except ValueError: index = 0
import utime import machine from machine import RTC rtc = machine.RTC() rtc.ntp_sync("pool.ntp.org") #Setting time to UTC timezone utime.sleep_ms(750) utime.timezone(+3600) #Adjusting time to UTC+1 (for France)
def doSocketMessage(measurement_counter): color = "white" c = 0 light_measurement_counter = 0 shouldSend = True elapsedTime = 0 while True: try: measurement_counter += 1 light = str(lightsensor.light()[0]) #print(light) measurement_date = rtc.now() send_time_string = "{}-{}-{} {}:{}:{}".format( measurement_date[0], measurement_date[1], measurement_date[2], measurement_date[3], measurement_date[4], measurement_date[5]) s.sendall('{};{};{};{};{};{};{};{}'.format( measurement_counter, send_time_string, light, intensity, color, device, " ", "level").encode('utf-8')) #print(send_time_string) message = s.recv(4096) print(message) if (message != b''): received_data = message.decode("utf_8") elements = received_data.split(";") if elements[len(elements) - 1] == 'level': received_data = elements[0] print(received_data) pycom.rgbled(int(elements[0])) elif elements[len(elements) - 1] == 'light': color = elements[2] rtc.now() utime.timezone(7200) s.sendall('{};{};{};{};{};{}'.format( elements[0], send_time_string, elements[2], device, "recieved", "light").encode('utf-8')) utime.sleep(1) #Change how often we send if (elapsedTime >= 1800 and shouldSend): #MeasurementCounter, Send_time, color, device name, send/recieve, type color = newColor[c] s.sendall('{};{};{};{};{};{}'.format(light_measurement_counter, send_time_string, newColor[c], device, "send", "light").encode('utf-8')) elapsedTime = 0 c += 1 light_measurement_counter += 1 else: elapsedTime += 1 if (c >= 3): c = 0 except OSError as e: if e.args[0] not in [ERRORCONNECTIONRESET, ERRORHOSTUNREACHABLE]: raise print("Lost connection to host") renew_connection() break
def setup_rtc(): rtc = machine.RTC() rtc.ntp_sync("pool.ntp.org") while not rtc.synced(): utime.sleep_ms(100) utime.timezone(3600)
def rtc_setup(): # setup rtc rtc.ntp_sync("pool.ntp.org") utime.sleep_ms(750) utime.timezone(7200) print('Adjusted from UTC to EST timezone', utime.localtime(), '\n')
break else: print("Network not found") ################################################# # Get Current Date and Time # # Instructions from: MicroPython for the # # Internet of Things # ################################################# def setRTCLocalTime(): rtc = RTC() print("Time before sync: ", rtc.now()) rtc.ntp_sync("pool.ntp.org") while not rtc.synced(): utime.sleep(1) print("Waiting for NTP server...") print('\nTime after sync: ', rtc.now()) wifiConnect() setRTCLocalTime() utime.timezone(3600) #set the time zone to gmt+1 localtime = utime.localtime() print('Adjusted to GMT +1 timezone', localtime, '\n') todayDate = str(localtime[2]) + "/" + str(localtime[1]) + "/" + str(localtime[0]) nowTime = str(localtime[3]) + ":" + str(localtime[4]) print(todayDate) print(nowTime)
import utime import gc from machine import RTC from machine import SD from L76GNSS import L76GNSS from pytrack import Pytrack time.sleep(2) gc.enable() # setup rtc rtc = machine.RTC() rtc.ntp_sync("pool.ntp.org") utime.sleep_ms(750) print('\nRTC Set from NTP to UTC:', rtc.now()) utime.timezone(7200) print('Adjusted from UTC to EST timezone', utime.localtime(), '\n') py = Pytrack() l76 = L76GNSS(py, timeout=30) # sd = SD() # os.mount(sd, '/sd') # f = open('/sd/gps-record.txt', 'w') # 2019-0715 Peter added to re-run test def run(): while (True): coord = l76.coordinates() # f.write("{} - {}\n".format(coord, rtc.now()))
from network import LoRa from CayenneLPP import CayenneLPP import socket import ubinascii import struct print('Pytrack: ogosea version 0.1 by [email protected]') time.sleep(5) gc.enable() # setup rtc rtc = machine.RTC() rtc.ntp_sync("pool.ntp.org") utime.sleep_ms(1000) print('\nRTC Set from NTP to UTC:', rtc.now()) utime.timezone(25200) print('Adjusted from UTC to EST timezone', utime.localtime(), '\n') py = Pytrack() l76 = L76GNSS(py, timeout=30) time.sleep(2) # display the reset reason code and the sleep remaining in seconds # possible values of wakeup reason are: # WAKE_REASON_ACCELEROMETER = 1 # WAKE_REASON_PUSH_BUTTON = 2 # WAKE_REASON_TIMER = 4 # WAKE_REASON_INT_PIN = 8 print("Wakeup reason: " + str(py.get_wake_reason())) print("Approximate sleep remaining: " + str(py.get_sleep_remaining()) + " sec")