Page 1 of 3

Having issues with trying to catch "OneWireError" exception

Posted: Mon Mar 29, 2021 8:56 pm
by rpi_nerd
Occasionally I get a failure when reading a ds18bs20 sensor and it throws a "OneWireError" exception.
I tried catching it with:

Code: Select all

		except	OneWireError:
			pass
But then I get:

Code: Select all

NameError: name 'OneWireError' isn't defined

Re: Having issues with trying to catch "OneWireError" exception

Posted: Mon Mar 29, 2021 9:21 pm
by davef
Do you have both:

Code: Select all

import onewire
import ds18x20
Also, there has been a suggestion that one waits 1 second rather than the normal 750ms for conversions to take place.

Re: Having issues with trying to catch "OneWireError" exception

Posted: Mon Mar 29, 2021 9:24 pm
by rpi_nerd
I do.

Re: Having issues with trying to catch "OneWireError" exception

Posted: Mon Mar 29, 2021 9:30 pm
by davef
Added a comment to my previous response.

Re: Having issues with trying to catch "OneWireError" exception

Posted: Mon Mar 29, 2021 9:34 pm
by rpi_nerd
I'm taking readings every 15 seconds.

Anyways I'll go ahead and post the entire code if that's of help:

Code: Select all

def go():
	import time
	import machine
	import urequests
	import network
	import onewire, ds18x20
	sta_if = network.WLAN(network.STA_IF)

	
	dat = machine.Pin(12)
	ds = ds18x20.DS18X20(onewire.OneWire(dat))
	roms = ds.scan()
	dat_i = machine.Pin(14)
	ds_i = ds18x20.DS18X20(onewire.OneWire(dat_i))
	roms_i = ds_i.scan()
	time.sleep(2)

	

	def send_data():
		url = 'http://[server ip address]/sensorlogging/php/rsensor_ds18b20_d.php'
		headerd = {'Content-Type': 'application/x-www-form-urlencoded'}
		jsons = "data=" + str(ds18b20temp_f) + "&" + "data2=" + str(ds18b20temp_f_i)
		if sta_if.isconnected() == True:	
			try:
				r = urequests.post(url, data=jsons, headers= headerd)
				r.close()
			except OSError:
				pass
	while True:
		try:
			ds.convert_temp()
			ds_i.convert_temp()
			time.sleep(15)
			ds18b20temp_c=ds.read_temp(roms[0])
			ds18b20temp_f=ds18b20temp_c * 9.0/5.0 +32
			ds18b20temp_c_i=ds_i.read_temp(roms_i[0])
			ds18b20temp_f_i=ds18b20temp_c_i * 9.0/5.0 +32
			send_data()
		except	OneWireError:
			pass

Re: Having issues with trying to catch "OneWireError" exception

Posted: Mon Mar 29, 2021 9:42 pm
by davef
I wonder if waiting that long before processing the reading could be a problem.

Maybe try time.sleep(1) and then put the 15 sec delay after send_data

Re: Having issues with trying to catch "OneWireError" exception

Posted: Mon Mar 29, 2021 9:55 pm
by davef
Couple more thoughts:

- like you I do the scan before the while loop, maybe the scan results are being corrupted ... try scanning within the while loop.

Also, are you aware of issues with connecting multiple 1-wire sensors. Describe how you have them connected.

Re: Having issues with trying to catch "OneWireError" exception

Posted: Mon Mar 29, 2021 10:19 pm
by rpi_nerd
Might try putting the scan in the loop. I have each sensor on a different i/o pin.

I initially had the exception handling as a catch-all:

Code: Select all

		except:
			pass
But apparently, a major side-effect of that is that I can't kill it with CTRL+C.

Re: Having issues with trying to catch "OneWireError" exception

Posted: Mon Mar 29, 2021 10:25 pm
by davef
Are you still getting the same error message?

Re: Having issues with trying to catch "OneWireError" exception

Posted: Mon Mar 29, 2021 10:37 pm
by rpi_nerd
With the catch-all exception handling? Nope, it handles failed reads without a problem (I'm triggering read failures by disconnecting a data lead running to a sensor as it can be quite some time for it occur on its own.)

Here's what I get when I leave out the exception handling and trigger a read failure:

Code: Select all

Traceback (most recent call last):                                                                                                                    
  File "<stdin>", line 1, in <module>                                                                                                                 
  File "esp8266_1_wire_d_ds.py", line 32, in go                                                                                                       
  File "ds18x20.py", line 19, in convert_temp                                                                                                         
  File "onewire.py", line 22, in reset                                                                                                                
OneWireError:                                                                                                                                         
>>>