Hey everyone,
I am working on a python script that uses a thermometer. I want to put a line of code in that test to make sure the thermometer is hooked up, so I using "try except".
I found a library that seems to do a good job, but the error code that comes back when the thermometer is missing is:
w1thermsensor.errors.NoSensorFoundError:
Could not find sensor of type DS18B20 with id 0300a27989942
Please check cabling and check your /boot/config.txt for
dtoverlay=w1-gpio
is there a way I can tell Try ... Except to look for that error in particular?
I tried:
try:
verify code:
except w1thermsensor.errors.NoSensorFoundError
return results
Any ideas?
Thanks
What happens when you try the above? The except
line should have a colon, so this doesn't seem runnable.
Is verify code:
some placeholder? What are you actually calling?
First I'd check if the library is throwing something that can actually be caught (as opposed to exiting). If you replace the except with just a bare except:
, does it run the exception block?
Also, show as your imports with respect to
w1thermsensor
or
w1thermsensor.errors
Looking at the project
__init__.py they import their custom errors at to level, so
import w1thermsensor
try:
my_sensor = w1thermsensor.W1ThermSensor()
except w1thermsensor.NoSensorFoundError
print('No sensor found')
(Sep-21-2020, 05:25 AM)buran Wrote: [ -> ]Also, show as your imports with respect to w1thermsensor
or w1thermsensor.errors
Looking at the project __init__.py they import their custom errors at to level, so
import w1thermsensor
try:
my_sensor = w1thermsensor.W1ThermSensor()
except w1thermsensor.NoSensorFoundError
print('No sensor found')
Here is my version of that:
from w1thermsensor import W1ThermSensor
try:
sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, '0300a27989945')
except w1thermsensor.errors.NoSensorFoundError:
print('Thermometer not Found')
here is the output:
Error:
Traceback (most recent call last):
File "curl.py", line 5, in <module>
sensor = w1ThermSensor(w1ThermSensor.THERM_SENSOR_DS18B20, '0300a27989945')
NameError: name 'w1ThermSensor' is not defined
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "curl.py", line 6, in <module>
except w1thermsensor.errors.NoSensorFoundError:
NameError: name 'w1thermsensor' is not defined
Here is what is in the modules __init__.py file
from .__version__ import __version__ # noqa
from .core import W1ThermSensor # noqa
from .errors import NoSensorFoundError # noqa
from .errors import SensorNotReadyError # noqa
from .errors import UnsupportedUnitError # noqa
from .errors import ResetValueError # noqa
here is a file called errors.py in the module dir
# -*- coding: utf-8 -*-
"""
This module provides exceptions for the w1thermsensor.
"""
import textwrap
class W1ThermSensorError(Exception):
"""Exception base-class for W1ThermSensor errors"""
pass
class KernelModuleLoadError(W1ThermSensorError):
"""Exception when the w1 therm kernel modules could not be loaded properly"""
def __init__(self):
super(KernelModuleLoadError, self).__init__(
"Cannot load w1 therm kernel modules"
)
class NoSensorFoundError(W1ThermSensorError):
"""Exception when no sensor is found"""
def __init__(self, message):
super(NoSensorFoundError, self).__init__(
textwrap.dedent(
"""
{}
Please check cabling and check your /boot/config.txt for
dtoverlay=w1-gpio
""".format(
message
)
).rstrip()
)
class ResetValueError(W1ThermSensorError):
"""Exception when the reset value is yield from the hardware"""
def __init__(self, sensor):
super(ResetValueError, self).__init__(
"Sensor {} yields the reset value of 85 degree millicelsius. "
"Please check the hardware.".format(sensor.id)
)
self.sensor = sensor
I can't really find where the errors are called
(Sep-21-2020, 05:48 PM)JarredAwesome Wrote: [ -> ]Here is what is in the modules __init__.py file
I know what's in
__init__.py
- that's why I show it to you. In my code I import the module
w1thermsensor
and then fully reference the
W1ThermSensor
and
NoSensorFoundError
If you prefer
from w1thermsensor import W1ThermSensor
, then it should be
from w1thermsensor import W1ThermSensor, NoSensorFoundError
try:
sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, '0300a27989945')
except NoSensorFoundError:
print('Thermometer not Found')
(Sep-21-2020, 05:53 PM)buran Wrote: [ -> ]I know what's in __init__.py
- that's why I show it to you.
Sorry about that. I missread what you wrote.
That updated code works perfected! thanks.