11# SPDX-FileCopyrightText: 2018 Mikey Sklar for Adafruit Industries
22#
33# SPDX-License-Identifier: MIT
4-
4+ # Modified by Jonathan Seyfert, 2022-01-22, to keep code from crashing when WiFi or IP is unavailable
55from subprocess import Popen , PIPE
6- from time import sleep
6+ from time import sleep , perf_counter
77from datetime import datetime
88import board
99import digitalio
2929
3030# looking for an active Ethernet or WiFi device
3131def find_interface ():
32+ # dev_name = 0 # sets dev_name so that function does not return Null and crash code
3233 find_device = "ip addr show"
3334 interface_parse = run_cmd (find_device )
3435 for line in interface_parse .splitlines ():
3536 if "state UP" in line :
3637 dev_name = line .split (':' )[1 ]
37- return dev_name
38+ return dev_name
39+ return 1 # avoids returning Null if "state UP" doesn't exist
3840
3941# find an active IP on the first LIVE network device
4042def parse_ip ():
41- find_ip = "ip addr show %s" % interface
43+ if interface == 1 : # if true, no device is in "state UP", skip IP check
44+ return "not assigned " # display "IP not assigned"
45+ ip = "0"
4246 find_ip = "ip addr show %s" % interface
4347 ip_parse = run_cmd (find_ip )
4448 for line in ip_parse .splitlines ():
4549 if "inet " in line :
4650 ip = line .split (' ' )[5 ]
4751 ip = ip .split ('/' )[0 ]
48- return ip
52+ return ip # returns IP address, if found
53+ return "pending " # display "IP pending" when "state UP", but no IPv4 address yet
4954
5055# run unix shell command, return as ASCII
5156def run_cmd (cmd ):
@@ -56,12 +61,19 @@ def run_cmd(cmd):
5661# wipe LCD screen before we start
5762lcd .clear ()
5863
64+
5965# before we start the main loop - detect active network device and ip address
60- sleep ( 2 )
66+ # set timer to = perf_counter(), for later use in IP update check
6167interface = find_interface ()
6268ip_address = parse_ip ()
69+ timer = perf_counter ()
6370
6471while True :
72+ # check for new IP addresses, at a slower rate than updating the clock
73+ if perf_counter () - timer >= 15 :
74+ interface = find_interface ()
75+ ip_address = parse_ip ()
76+ timer = perf_counter ()
6577
6678 # date and time
6779 lcd_line_1 = datetime .now ().strftime ('%b %d %H:%M:%S\n ' )
@@ -72,4 +84,4 @@ def run_cmd(cmd):
7284 # combine both lines into one update to the display
7385 lcd .message = lcd_line_1 + lcd_line_2
7486
75- sleep (2 )
87+ sleep (1 )
0 commit comments