Skip to content

Commit

Permalink
adjust paths and tweak wi-fi code
Browse files Browse the repository at this point in the history
  • Loading branch information
helgibbons committed Oct 5, 2023
1 parent ac83bfb commit b220ba0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 40 deletions.
31 changes: 16 additions & 15 deletions examples/magic_mirror/magic_mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from modes import VGA
from pimoroni import Button
import pngdec
import network
import urequests
import ntptime
import time
Expand All @@ -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/

Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -286,23 +290,20 @@ 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()

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()

year, month, day, wd, hour, minute, second, _ = rtc.datetime()
Expand Down
31 changes: 16 additions & 15 deletions examples/magic_mirror/magic_mirror_adafruit_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from modes import VGA
from pimoroni import Button
import pngdec
import network
import urequests
import ntptime
import time
Expand All @@ -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!)
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -327,22 +331,19 @@ 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()

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()

Expand Down
21 changes: 11 additions & 10 deletions examples/magic_mirror/magic_mirror_home_assistant..py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from modes import VGA
from pimoroni import Button
import pngdec
import network
import urequests
import ntptime
import time
Expand All @@ -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"
Expand Down Expand Up @@ -88,6 +84,7 @@
second = 0
last_second = 0
temperature = None
wifi_problem = False

buildings = []
BUILDING_COUNT = 20
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -289,22 +293,19 @@ 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()

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()

Expand Down

0 comments on commit b220ba0

Please sign in to comment.