From b220ba0ddade378f88fe60644bde2e1dbb5dafd6 Mon Sep 17 00:00:00 2001 From: Hel Gibbons Date: Thu, 5 Oct 2023 16:01:19 +0100 Subject: [PATCH] adjust paths and tweak wi-fi code --- examples/magic_mirror/magic_mirror.py | 31 ++++++++++--------- .../magic_mirror/magic_mirror_adafruit_io.py | 31 ++++++++++--------- .../magic_mirror_home_assistant..py | 21 +++++++------ 3 files changed, 43 insertions(+), 40 deletions(-) diff --git a/examples/magic_mirror/magic_mirror.py b/examples/magic_mirror/magic_mirror.py index ce2e5cf..5b44a8e 100644 --- a/examples/magic_mirror/magic_mirror.py +++ b/examples/magic_mirror/magic_mirror.py @@ -11,7 +11,6 @@ from modes import VGA from pimoroni import Button import pngdec -import network import urequests import ntptime import time @@ -28,9 +27,6 @@ # how often to poll the APIs for data, in minutes UPDATE_INTERVAL = 15 -# How many times to retry connecting to WiFi before giving up -MAX_RETRIES = 5 - # Weather settings: # Weather from OpenMeteo - find out more at https://open-meteo.com/ @@ -89,11 +85,13 @@ last_second = 0 temperature = None quote = None +wifi_problem = False # Connect to wifi def connect_to_wifi(): global WIFI_SSID, WIFI_PASSWORD + import network try: from secrets import WIFI_SSID, WIFI_PASSWORD except ImportError: @@ -108,6 +106,8 @@ def connect_to_wifi(): wlan.config(pm=0xa11140) # Turn WiFi power saving off for some slow APs retries = 0 + MAX_RETRIES = 5 # How many times to retry connecting to WiFi before giving up + while retries < MAX_RETRIES: print(f"Attempt {retries + 1} to connect to WiFi...") wlan.connect(WIFI_SSID, WIFI_PASSWORD) @@ -133,8 +133,12 @@ def connect_to_wifi(): # Synchronize the RTC time from NTP and download data from our APIs def get_data(): + global wifi_problem if connect_to_wifi() is False: + wifi_problem = True return + else: + wifi_problem = False try: print("Setting time from NTP") @@ -231,15 +235,15 @@ def redraw_display_if_reqd(): # Weather codes from https://open-meteo.com/en/docs # Weather icons from https://icons8.com/ if weathercode in [71, 73, 75, 77, 85, 86]: # codes for snow - png.open_file("/icons/snow.png") + png.open_file("/magic_mirror/icons/snow.png") elif weathercode in [51, 53, 55, 56, 57, 61, 63, 65, 66, 67, 80, 81, 82]: # codes for rain - png.open_file("/icons/rain.png") + png.open_file("/magic_mirror/icons/rain.png") elif weathercode in [1, 2, 3, 45, 48]: # codes for cloud - png.open_file("/icons/cloud.png") + png.open_file("/magic_mirror/icons/cloud.png") elif weathercode in [0]: # codes for sun - png.open_file("/icons/sun.png") + png.open_file("/magic_mirror/icons/sun.png") elif weathercode in [95, 96, 99]: # codes for storm - png.open_file("/icons/storm.png") + png.open_file("/magic_mirror/icons/storm.png") png.decode(COLUMN_3_POSITION, ROW_2_POSITION - ICON_OFFSET) # draw the weather text graphics.set_pen(WHITE) @@ -257,11 +261,11 @@ def redraw_display_if_reqd(): # show wifi details graphics.set_pen(WHITE) - if connect_to_wifi() is False: - png.open_file("/icons/no-wifi.png") + if wifi_problem is True: + png.open_file("/magic_mirror/icons/no-wifi.png") graphics.text("WiFi not available. Create secrets.py to connect to WiFi!", PADDING, ROW_4_POSITION, COLUMN_3_POSITION - PADDING, scale=3) else: - png.open_file("/icons/wifi.png") + png.open_file("/magic_mirror/icons/wifi.png") if REDACT_WIFI is True: graphics.text(f"Our WiFi network is '{WIFI_SSID}'.", PADDING, ROW_4_POSITION, COLUMN_3_POSITION - PADDING, scale=3) else: @@ -286,7 +290,6 @@ def redraw_display_if_reqd(): graphics.text("Updating!", PADDING, CLOCK_POSITION, scale=6) graphics.update() -connect_to_wifi() get_data() redraw_display_if_reqd() last_updated = time.ticks_ms() @@ -294,7 +297,6 @@ def redraw_display_if_reqd(): while True: # get new data when button Y is pressed if button_y.is_pressed: - connect_to_wifi() get_data() last_updated = time.ticks_ms() @@ -302,7 +304,6 @@ def redraw_display_if_reqd(): # get new data after UPDATE_INTERVAL has passed if time.ticks_diff(ticks_now, last_updated) > UPDATE_INTERVAL * 1000 * 60: connect_to_wifi() - get_data() last_updated = time.ticks_ms() year, month, day, wd, hour, minute, second, _ = rtc.datetime() diff --git a/examples/magic_mirror/magic_mirror_adafruit_io.py b/examples/magic_mirror/magic_mirror_adafruit_io.py index 07242e2..7a211fc 100644 --- a/examples/magic_mirror/magic_mirror_adafruit_io.py +++ b/examples/magic_mirror/magic_mirror_adafruit_io.py @@ -14,7 +14,6 @@ from modes import VGA from pimoroni import Button import pngdec -import network import urequests import ntptime import time @@ -31,9 +30,6 @@ # how often to poll the APIs for data, in minutes UPDATE_INTERVAL = 15 -# How many times to retry connecting to WiFi before giving up -MAX_RETRIES = 5 - # Weather settings: # Weather from OpenMeteo - find out more at https://open-meteo.com/ # Set your latitude/longitude here (find yours by right clicking in Google Maps!) @@ -103,11 +99,13 @@ office_temp = None kitchen_temp = None warehouse_temp = None +wifi_problem = False # Connect to wifi def connect_to_wifi(): global WIFI_SSID, WIFI_PASSWORD + import network try: from secrets import WIFI_SSID, WIFI_PASSWORD except ImportError: @@ -122,6 +120,8 @@ def connect_to_wifi(): wlan.config(pm=0xa11140) # Turn WiFi power saving off for some slow APs retries = 0 + MAX_RETRIES = 5 # How many times to retry connecting to WiFi before giving up + while retries < MAX_RETRIES: print(f"Attempt {retries + 1} to connect to WiFi...") wlan.connect(WIFI_SSID, WIFI_PASSWORD) @@ -147,8 +147,12 @@ def connect_to_wifi(): # Synchronize the RTC time from NTP and download data from our APIs def get_data(): + global wifi_problem if connect_to_wifi() is False: + wifi_problem = True return + else: + wifi_problem = False try: print("Setting time from NTP") @@ -271,15 +275,15 @@ def redraw_display_if_reqd(): # Weather codes from https://open-meteo.com/en/docs # Weather icons from https://icons8.com/ if weathercode in [71, 73, 75, 77, 85, 86]: # codes for snow - png.open_file("/icons/snow.png") + png.open_file("/magic_mirror/icons/snow.png") elif weathercode in [51, 53, 55, 56, 57, 61, 63, 65, 66, 67, 80, 81, 82]: # codes for rain - png.open_file("/icons/rain.png") + png.open_file("/magic_mirror/icons/rain.png") elif weathercode in [1, 2, 3, 45, 48]: # codes for cloud - png.open_file("/icons/cloud.png") + png.open_file("/magic_mirror/icons/cloud.png") elif weathercode in [0]: # codes for sun - png.open_file("/icons/sun.png") + png.open_file("/magic_mirror/icons/sun.png") elif weathercode in [95, 96, 99]: # codes for storm - png.open_file("/icons/storm.png") + png.open_file("/magic_mirror/icons/storm.png") png.decode(COLUMN_3_POSITION, ROW_2_POSITION - ICON_OFFSET) # draw the weather text graphics.set_pen(WHITE) @@ -298,11 +302,11 @@ def redraw_display_if_reqd(): # show wifi details graphics.set_pen(WHITE) - if connect_to_wifi() is False: - png.open_file("/icons/no-wifi.png") + if wifi_problem is True: + png.open_file("/magic_mirror/icons/no-wifi.png") graphics.text("WiFi not available. Create secrets.py to connect to WiFi!", PADDING, ROW_4_POSITION, COLUMN_3_POSITION - PADDING, scale=3) else: - png.open_file("/icons/wifi.png") + png.open_file("/magic_mirror/icons/wifi.png") if REDACT_WIFI is True: graphics.text(f"Our WiFi network is '{WIFI_SSID}'.", PADDING, ROW_4_POSITION, COLUMN_3_POSITION - PADDING, scale=3) else: @@ -327,7 +331,6 @@ def redraw_display_if_reqd(): graphics.text("Updating!", PADDING, CLOCK_POSITION, scale=6) graphics.update() -connect_to_wifi() get_data() redraw_display_if_reqd() last_updated = time.ticks_ms() @@ -335,14 +338,12 @@ def redraw_display_if_reqd(): while True: # get new data when button Y is pressed if button_y.is_pressed: - connect_to_wifi() get_data() last_updated = time.ticks_ms() ticks_now = time.ticks_ms() # get new data after UPDATE_INTERVAL has passed if time.ticks_diff(ticks_now, last_updated) > UPDATE_INTERVAL * 1000 * 60: - connect_to_wifi() get_data() last_updated = time.ticks_ms() diff --git a/examples/magic_mirror/magic_mirror_home_assistant..py b/examples/magic_mirror/magic_mirror_home_assistant..py index 205ab93..0d681b3 100644 --- a/examples/magic_mirror/magic_mirror_home_assistant..py +++ b/examples/magic_mirror/magic_mirror_home_assistant..py @@ -17,7 +17,6 @@ from modes import VGA from pimoroni import Button import pngdec -import network import urequests import ntptime import time @@ -36,9 +35,6 @@ # how often to poll the APIs for data, in minutes UPDATE_INTERVAL = 15 -# How many times to retry connecting to WiFi before giving up -MAX_RETRIES = 5 - # Home Assistant settings: HOME_ASSISTANT_URL_1 = "http://homeassistant.local:8123/api/states/sensor.sun_next_setting" HOME_ASSISTANT_URL_2 = "http://homeassistant.local:8123/api/states/sensor.sun_next_rising" @@ -88,6 +84,7 @@ second = 0 last_second = 0 temperature = None +wifi_problem = False buildings = [] BUILDING_COUNT = 20 @@ -104,6 +101,7 @@ # Connect to wifi def connect_to_wifi(): global WIFI_SSID, WIFI_PASSWORD + import network try: from secrets import WIFI_SSID, WIFI_PASSWORD except ImportError: @@ -118,6 +116,8 @@ def connect_to_wifi(): wlan.config(pm=0xa11140) # Turn WiFi power saving off for some slow APs retries = 0 + MAX_RETRIES = 5 # How many times to retry connecting to WiFi before giving up + while retries < MAX_RETRIES: print(f"Attempt {retries + 1} to connect to WiFi...") wlan.connect(WIFI_SSID, WIFI_PASSWORD) @@ -143,8 +143,12 @@ def connect_to_wifi(): # Synchronize the RTC time from NTP and download data from our APIs def get_data(): + global wifi_problem if connect_to_wifi() is False: + wifi_problem = True return + else: + wifi_problem = False try: print("Setting time from NTP") @@ -221,11 +225,11 @@ def redraw_display_if_reqd(): # show wifi details graphics.set_pen(WHITE) - if connect_to_wifi() is False: - png.open_file("/icons/no-wifi.png") + if wifi_problem is True: + png.open_file("/magic_mirror/icons/no-wifi.png") graphics.text("WiFi not available. Create secrets.py to connect to WiFi!", PADDING, ROW_2_POSITION, COLUMN_3_POSITION - PADDING, scale=3) else: - png.open_file("/icons/wifi.png") + png.open_file("/magic_mirror/icons/wifi.png") if REDACT_WIFI is True: graphics.text(f"Our WiFi network is '{WIFI_SSID}'.", PADDING, ROW_2_POSITION, COLUMN_3_POSITION - PADDING, scale=3) else: @@ -289,7 +293,6 @@ def redraw_display_if_reqd(): graphics.text("Updating!", PADDING, CLOCK_POSITION, scale=6) graphics.update() -connect_to_wifi() get_data() redraw_display_if_reqd() last_updated = time.ticks_ms() @@ -297,14 +300,12 @@ def redraw_display_if_reqd(): while True: # get new data when button Y is pressed if button_y.is_pressed: - connect_to_wifi() get_data() last_updated = time.ticks_ms() ticks_now = time.ticks_ms() # get new data after UPDATE_INTERVAL has passed if time.ticks_diff(ticks_now, last_updated) > UPDATE_INTERVAL * 1000 * 60: - connect_to_wifi() get_data() last_updated = time.ticks_ms()