Page 1 of 3

DS18B20 CRC error

Posted: Fri Dec 03, 2021 7:13 am
by davef
When I get a CRC error with this code the ESP32 just locks-up until a watchdog kicks-in:

Code: Select all

    #  read 1-wire HWT temperature
        try:
            HWT_temp = ds2.read_temp(bytearray(b'(\xd4\x13\xef\x04\x00\x00y'))
        except Exception as error:
            try:
                with open('errors.txt', 'a') as outfile:
                    outfile.write('HWT ' + str(error) + '\n')
            except OSError:
                pass
I could put this in a while loop and take 5 goes at getting a correct answer, as suggested here:
viewtopic.php?f=16&t=9885&p=55282&hilit ... crc#p55282
but why does it lock-up?

Am I not handling the exception that has been raised in def read_scratch (ds18x20.py) properly?

Thanks.

Re: DS18B20 CRC error

Posted: Fri Dec 03, 2021 5:45 pm
by pythoncoder
According to the docs read_temp should not raise an exception: it should return None on a CRC error. If it's locking up this suggests something other than a CRC error. Some kind of electrical issue?

Re: DS18B20 CRC error

Posted: Fri Dec 03, 2021 6:18 pm
by davef
Hmmm ... error is returned as "CRC error". I'll study the docs more carefully.

Could be an electrical issue as I had to extend the leads on the DS18B20 by 1 metre. I have a 2mtr shielded sensor coming one day.

Thanks

Re: DS18B20 CRC error

Posted: Fri Dec 03, 2021 11:08 pm
by davef
According to:
https://stackoverflow.com/questions/484 ... n/48408976
it appears that 'CRC error' will be raised if not compiled with the -O option ... whether or not this is the case with Micropython I don't have a clue.

Because I am a beginner at handling exceptions I suspect that I am not handling this error correctly.

Any suggestions other than allowing the EP32 to re-boot would be appreciated?

Thanks.

Re: DS18B20 CRC error

Posted: Sat Dec 04, 2021 1:43 pm
by pythoncoder
Your exception handling looks OK.

Whether an exception is raised, or None is returned, depends on the design of the driver. If you're using Robert-hh's driver his docs are quite clear: None is returned. Given that it's written in Python, compile options should be irrelevant. Perhaps you're using a different driver?

In any event, the presence of CRC errors does seem to suggest electrical problems.

As for your stackoverflow link, I'm afraid I can't see its relevance.

Re: DS18B20 CRC error

Posted: Sat Dec 04, 2021 2:33 pm
by Roberthh
That depends: The function read_scratch() contains an assert for checking the CRC. The function read_temp() calls read_scratch(), but in a try/except clause, with an exception for AssertionError, where it return None.

Re: DS18B20 CRC error

Posted: Sat Dec 04, 2021 6:27 pm
by davef
Thank you both for your responses.

As far as I am aware I am just using the ds18x20.py that is part of the standard ESP32 image:
https://github.com/micropython/micropyt ... ds18x20.py
In any event, the presence of CRC errors does seem to suggest electrical problems.
Quite likely, but what generates the error message <CRC error>?

I need to read up on the difference between Exception and AssertionError.

Re: DS18B20 CRC error

Posted: Sat Dec 04, 2021 7:24 pm
by Roberthh
The assert raising the CRC error is in onewire.py. That is caught in DS18B20.py, at least in the code I wrote.

Re: DS18B20 CRC error

Posted: Sat Dec 04, 2021 7:37 pm
by davef
Roberthh,

Reading through your implementation I see the assert statement at line 56.

My understanding is that this is caught in line 84 and returns a message None.

Why don't you:

Code: Select all

except AssertionError as msg:
    print(msg)
    or
    return msg
The most important thing I am trying to prevent is the system locking up. I know the implementation on the RaspbeeryPi is probably quite different but at least it kept running when there was a bad DS18B20 reading.

Re: DS18B20 CRC error

Posted: Sat Dec 04, 2021 7:58 pm
by Roberthh
That's in read_temp, where the return value is either the temperature or None. It does not lock up.