def __init__(self, default_timezone=EUROPE_LONDON): if utime.time() < 1585235437: # RTC is unset. wifi.ntp() self._rtc = machine.RTC() timezone = machine.nvs_getstr('system', 'timezone') if not timezone: timezone = default_timezone self._rtc.timezone(timezone)
def wifiTask(): global wifi_status_prev, wifi_status_curr, gui_redraw, ota_available wifi_status_prev = wifi_status_curr wifi_status_curr = wifi.status() if wifi_status_curr: wifi.ntp(True) if wifi_status_curr != wifi_status_prev: #pm.feed() wifi_status_prev = wifi_status_curr gui_redraw = True if wifi_status_curr: ota_available = otacheck.available(True) return 1000
def wifiTask(): global wifi_status_prev, wifi_status_curr, gui_redraw, ota_available wifi_status_prev = wifi_status_curr wifi_status_curr = wifi.status() if wifi_status_curr: wifi.ntp(True) if wifi_status_curr != wifi_status_prev: print("WiFi status changed", wifi_status_curr) pm.feed() wifi_status_prev = wifi_status_curr gui_redraw = True if wifi_status_curr: ota_available = otacheck.available(True) print("Check OTA", ota_available) return 1000
def update_cache(): # Check if RTC has been synced over NTP if not rtc.isSet(): _show_progress("Connecting to WiFi...", False) wifi.connect() if not wifi.wait(): _show_progress("Failed to connect to WiFi.", True) return False while wifi.status() and (time.time() < 1482192000): wifi.ntp() last_update = _get_last_updated() if last_update > 0 and time.time() < last_update + (600): return True if not wifi.status(): _show_progress("Connecting to WiFi...", False) wifi.connect() if not wifi.wait(): _show_progress("Failed to connect to WiFi.", True) return False print('Updating woezel cache..') #os.remove(cache_path + '/categories.json') _show_progress("Downloading package list...") packages = get_pkg_list() categories = set(item['category'] for item in packages) with open(cache_path + '/categories.json', 'w') as categories_file: categories_file.write(json.dumps([{'name': _slug_to_name(cat), 'slug': cat} for cat in categories])) _show_progress("Saving lists...") for cat in categories: gc.collect() with open(cache_path + '/' + cat + '.json', 'w') as category_file: category_file.write(json.dumps([app for app in packages if app['category'] == cat])) gc.collect() _set_last_updated() _show_progress("Done!") gc.collect() return True
try: while True: try: wifi.init() while not wifi.sta_if.isconnected(): sleep(0.1) except: wifi.connect() print("Connecting to WiFi") if not wifi.wait(): time.sleep(2) print("Connection Error, Rebooting") machine.reset() wifi.ntp(False) rtc = machine.RTC() print(rtc.now()) #url="http://ip-api.com/line/?fields=city" #url = "http://travelplanner.mobiliteit.lu/restproxy/departureBoard?accessId=cdt&id=A=1@O=Lintgen,%20Kräizung@X=6,125115@Y=49,720344@U=82@L=160702003@B=1@p=1594365298&format=json" url = 'http://wttr.in/?format="%S:+%m+%s"' while True: r = requests.get(url) print("API HTTP request status Code:", r.status_code) #print(r.status_code) if r.status_code == 200: break else: # Hope it won't 500 a little later
def configure(): import wifi return wifi.ntp()
co2_history = [] co2_history_add_counter = 0 co2_history_max_len = display.size()[0] co2 = None co2_show = None co2_show_accum = [] co2_show_accum_max_len = 4 climate = (0, 0, 0) ui = UI() while True: time_synced = utime.localtime()[0] >= 2020 if not wifi.status(): _ = wifi.connect() if not time_synced and wifi.status(): _ = wifi.ntp() if wifi.status(): try: mqtt.ping() except: mqtt.connect() if mhz19 is None: mhz19 = MHZ19(rx_pin=17, tx_pin=16) try: co2 = mhz19.gas_concentration() print('co2: %d' % co2) except Exception as err: print(err) mhz19.close() mhz19 = None
def update_cache(): # Check if RTC has been synced over NTP if not rtc.isSet(): _show_progress("Connecting to WiFi...", False) wifi.connect() if not wifi.wait(): _show_progress("Failed to connect to WiFi.", True) return False while wifi.status() and (time.time() < 1482192000): wifi.ntp() last_update = _get_last_updated() if last_update > 0 and time.time() < last_update + (600): return True if not wifi.status(): _show_progress("Connecting to WiFi...", False) wifi.connect() if not wifi.wait(): _show_progress("Failed to connect to WiFi.", True) return False print('Updating woezel cache..') _show_progress("Downloading categories...") try: request = urequests.get("https://%s/eggs/categories/json" % woezel_domain, timeout=30) _show_progress("Saving categories...") with open(cache_path + '/categories.json', 'w') as categories_file: categories_file.write(request.text) _show_progress("Parsing categories...") categories = request.json() request.close() del request for index, category in enumerate(categories): gc.collect() cat_slug = category["slug"] _show_progress("Downloading '" + category["name"] + "' (%d/%d)..." % (index, len(categories))) f = urequests.get("https://%s/basket/%s/category/%s/json" % (woezel_domain, device_name, cat_slug), timeout=30) with open(cache_path + '/' + cat_slug + '.json', 'w') as f_file: f_file.write(f.text) f.close() del f gc.collect() _set_last_updated() _show_progress("Done!") gc.collect() return True except BaseException as e: sys.print_exception(e) _show_progress("Failed!", True) gc.collect() return False
'doy': 1 }] print("Error parsing activities") ##### Main program ###### days = [] ## Check if RTC is initialized if not rtc.isSet(): if not wifi.status(): print("Updating time") uinterface.connect_wifi() wifi.ntp() wifi.disconnect() ## Cache updates cache_path = '/cache/activities' cache_file = '/activities.json' url = "http://cz19.lanergy.eu/api/cz_app.php" ## Update cache update_cache() ## Get current day of year today = utime.localtime()[7] counter = 0 daysindex = 0