Skip to content

Commit

Permalink
Wrap reading in try/except
Browse files Browse the repository at this point in the history
  • Loading branch information
ROV Laptop committed Jun 8, 2024
1 parent bd2e794 commit 9bd8684
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
17 changes: 10 additions & 7 deletions src/pi/temp_sensor/temp_sensor/temp_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ def __init__(self) -> None:
self.timer = self.create_timer(timer_period, self.timer_callback)

def timer_callback(self) -> None:
self.sensor.read()
temp_reading = self.sensor.temperature()

# If any of the sensors detect water, send true to /tether/flooding
msg = Temperature()
msg.reading = temp_reading
self.publisher.publish(msg)
try:
self.sensor.read()
temp_reading = self.sensor.temperature()

# If any of the sensors detect water, send true to /tether/flooding
msg = Temperature()
msg.reading = temp_reading
self.publisher.publish(msg)
except OSError:
print('Failed to read temperature, skipping this read')


def main(args: None = None) -> None:
Expand Down
16 changes: 10 additions & 6 deletions src/pi/temp_sensor/temp_sensor/temp_sensor_dry_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ def debug_log() -> None:
sensor.init()

while True:
sensor.read()
print(
sensor.temperature(), # Get temperature in default units (Centigrade)
'\t',
sensor.temperature(tsys01.UNITS_Farenheit)
)
try:
sensor.read() # Sometimes throws OSError: [Errno 121] Remote I/O error
print(
sensor.temperature(), # Get temperature in default units (Centigrade)
'\t',
sensor.temperature(tsys01.UNITS_Farenheit)
)
except OSError:
print('Failed to read temperature, trying again')

sleep(1)


Expand Down

0 comments on commit 9bd8684

Please sign in to comment.