Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1584

Python • Re: DS18B20 SensorNotReadyError

$
0
0
I find with any sensor, you need to trap and potentially retry. FWIW here are my routines for the DS18B20

Code:

def init_water_sensor():# The water sensor is a DS18B20 which uses the RIP 1-wire protocol. By default, when this is# enable in the RIP Configuration it uses GPIO 4. For this reason, the air sensor is on a# different GPIO.# From help(W1ThermSensor)#     Help on class W1ThermSensor in module w1thermsensor.core:# W1ThermSensor()#     raises KernelModuleLoadError: if the w1 therm kernel modules could not be loaded correctly#     raises NoSensorFoundError: if the sensor with the given type and/or id does not exist or is not connected    global water_sensor    success=False        if not params.water_sensor_available: return True        try:        water_sensor = W1ThermSensor()        success=True    except Exception as err:        with open(params.log_file,"a") as f:            f.write(log_time("init_water_sensor")+"Exception caught\n")            print_exc(file=f)        success=False    return success

Code:

def get_water_reading():# Only get the water temperature reading in C and convert to F.# I'm not sure about what can be considered run-time error hear such as raised by# the adafruit.dht22 software so I've taken a guess.# From help(W1ThermSensor)#     Help on class W1ThermSensor in module w1thermsensor.core:# get_temperature()#     raises UnsupportedUnitError: if the unit is not supported#     raises NoSensorFoundError: if the sensor could not be found#     raises SensorNotReadyError: if the sensor is not ready yet#     raises ResetValueError: if the sensor has still the initial value and no measurment    if not params.water_sensor_available: return True,0.0,0.0        recoverable_err = 0    while recoverable_err <= __MAXERR__:        rt_msg=""        try:            temp_c = round(water_sensor.get_temperature(),2) # Get Temperatures            temp_f = round((temp_c * (9 / 5) + 32) ,2)            success = True            break        except SensorNotReadyError:            rt_msg="SensorNotReadyError"                except ResetValueError:            rt_msg="ResetValueError"               except Exception as err:            with open(params.log_file,"a") as f:                f.write(log_time("get_water_reading")+"Exception caught\n")                print_exc(file=f)            success = False            break                   if rt_msg != "":            if param.debug: print(log_time("get_water_reading")+"runtime error="+rt_msg+". Sleeping 2 secs")            recoverable_err=recoverable_err+1            sleep(2.0)            continue        if not success:        if recoverable_err >= __MAXERR__:            with open(params.log_file,"a") as f:                f.write(log_time("get_water_reading")+"Error: maxium runtime limit reached. Zeros returned")        global reading_err_cnt        reading_err_cnt=reading_err_cnt+1        temp_c=0.0        temp_f=0.0            return success,temp_c,temp_f    

Statistics: Posted by DS256 — Fri May 09, 2025 5:17 pm



Viewing all articles
Browse latest Browse all 1584

Trending Articles