Skip to content

Commit 3be95a0

Browse files
committed
sensor-stick-temperature.py: adding error handling when sensor not connected or removed
1 parent a738b89 commit 3be95a0

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

examples/sensor-stick-temperature.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
from picovector import ANTIALIAS_BEST, PicoVector, Polygon, Transform
88
import machine
99

10-
# Setup for the i2c and bme sensor
11-
bme = BreakoutBME280(machine.I2C())
12-
1310
# Setup for the Presto display
1411
presto = Presto(ambient_light=True)
1512
display = presto.display
@@ -36,6 +33,22 @@
3633
vector.set_transform(t)
3734

3835

36+
def show_message(text):
37+
display.set_pen(BACKGROUND)
38+
display.clear()
39+
display.set_pen(FOREGROUND)
40+
display.text(f"{text}", 5, 10, WIDTH, 2)
41+
presto.update()
42+
43+
44+
# Setup for the i2c and bme sensor
45+
try:
46+
bme = BreakoutBME280(machine.I2C())
47+
except RuntimeError:
48+
while True:
49+
show_message("No Multi-Sensor stick detected!\n\nConnect and try again.")
50+
51+
3952
class Widget(object):
4053
def __init__(self, x, y, w, h, radius=10, text_size=42):
4154
self.x = x
@@ -102,7 +115,12 @@ def set_title(self, title):
102115
vector.draw(background_rect)
103116

104117
# Get readings and format strings
105-
reading = bme.read()
118+
try:
119+
reading = bme.read()
120+
except RuntimeError:
121+
while True:
122+
show_message("Failed to get reading from BME280.\n\nCheck connection and reset :)")
123+
106124
temp_string = f"{reading[0]:.1f}C"
107125
pressure_string = f"{reading[1] // 100:.0f} hPa"
108126
humidity_string = f"{reading[2]:.0f}%"

0 commit comments

Comments
 (0)