Having issues with trying to catch "OneWireError" exception

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
rpi_nerd
Posts: 33
Joined: Sat Jul 29, 2017 2:05 pm

Having issues with trying to catch "OneWireError" exception

Post by rpi_nerd » Mon Mar 29, 2021 8:56 pm

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

davef
Posts: 241
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

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

Post by davef » Mon Mar 29, 2021 9:21 pm

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.
Last edited by davef on Mon Mar 29, 2021 9:29 pm, edited 1 time in total.

rpi_nerd
Posts: 33
Joined: Sat Jul 29, 2017 2:05 pm

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

Post by rpi_nerd » Mon Mar 29, 2021 9:24 pm

I do.

davef
Posts: 241
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

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

Post by davef » Mon Mar 29, 2021 9:30 pm

Added a comment to my previous response.

rpi_nerd
Posts: 33
Joined: Sat Jul 29, 2017 2:05 pm

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

Post by rpi_nerd » Mon Mar 29, 2021 9:34 pm

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

davef
Posts: 241
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

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

Post by davef » Mon Mar 29, 2021 9:42 pm

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

davef
Posts: 241
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

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

Post by davef » Mon Mar 29, 2021 9:55 pm

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.

rpi_nerd
Posts: 33
Joined: Sat Jul 29, 2017 2:05 pm

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

Post by rpi_nerd » Mon Mar 29, 2021 10:19 pm

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.

davef
Posts: 241
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

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

Post by davef » Mon Mar 29, 2021 10:25 pm

Are you still getting the same error message?

rpi_nerd
Posts: 33
Joined: Sat Jul 29, 2017 2:05 pm

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

Post by rpi_nerd » Mon Mar 29, 2021 10:37 pm

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:                                                                                                                                         
>>>         

Post Reply