Skip to content

Commit b3a993d

Browse files
committed
16x2 LCD example updated with Bullseye compat init script and now works with no IP address
1 parent 726cd11 commit b3a993d

2 files changed

Lines changed: 20 additions & 10 deletions

File tree

Drive_a_16x2_LCD_with_the_Raspberry_Pi/Drive_a_16x2_LCD_with_the_Raspberry_Pi.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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
55
from subprocess import Popen, PIPE
6-
from time import sleep
6+
from time import sleep, perf_counter
77
from datetime import datetime
88
import board
99
import digitalio
@@ -29,23 +29,28 @@
2929

3030
# looking for an active Ethernet or WiFi device
3131
def 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
4042
def 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
5156
def run_cmd(cmd):
@@ -56,12 +61,19 @@ def run_cmd(cmd):
5661
# wipe LCD screen before we start
5762
lcd.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
6167
interface = find_interface()
6268
ip_address = parse_ip()
69+
timer = perf_counter()
6370

6471
while 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)
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
[Unit]
22
Description=LCD date|time|ip
3-
Requires=network-online.target
4-
After=network-online.target
53

64
[Service]
75
ExecStart=/usr/bin/python3 Drive_a_16x2_LCD_with_the_Raspberry_Pi.py
@@ -12,4 +10,4 @@ Restart=always
1210
User=pi
1311

1412
[Install]
15-
WantedBy=network-online.target
13+
WantedBy=multi-user.target

0 commit comments

Comments
 (0)