DS18B20 CRC error

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

DS18B20 CRC error

Post by davef » Fri Dec 03, 2021 7:13 am

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.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: DS18B20 CRC error

Post by pythoncoder » Fri Dec 03, 2021 5:45 pm

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?
Peter Hinch
Index to my micropython libraries.

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

Re: DS18B20 CRC error

Post by davef » Fri Dec 03, 2021 6:18 pm

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

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

Re: DS18B20 CRC error

Post by davef » Fri Dec 03, 2021 11:08 pm

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.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: DS18B20 CRC error

Post by pythoncoder » Sat Dec 04, 2021 1:43 pm

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.
Peter Hinch
Index to my micropython libraries.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: DS18B20 CRC error

Post by Roberthh » Sat Dec 04, 2021 2:33 pm

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.

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

Re: DS18B20 CRC error

Post by davef » Sat Dec 04, 2021 6:27 pm

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.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: DS18B20 CRC error

Post by Roberthh » Sat Dec 04, 2021 7:24 pm

The assert raising the CRC error is in onewire.py. That is caught in DS18B20.py, at least in the code I wrote.

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

Re: DS18B20 CRC error

Post by davef » Sat Dec 04, 2021 7:37 pm

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.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: DS18B20 CRC error

Post by Roberthh » Sat Dec 04, 2021 7:58 pm

That's in read_temp, where the return value is either the temperature or None. It does not lock up.

Post Reply