timofurrer / w1thermsensor

A Python package and CLI tool to work with w1 temperature sensors like DS1822, DS18S20 & DS18B20 on the Raspberry Pi, Beagle Bone and other devices.
MIT License
493 stars 113 forks source link

IndexError: list index out of range #94

Closed bggardner closed 4 years ago

bggardner commented 4 years ago

I have four DS18B20 sensors on the 1-wire network of a Raspberry Pi. After a long time (multiple hours, actual time may vary) of calling get_temperature() every 30 seconds in a while loop, I get an IndexError with the following trace:

Aug 18 03:19:00 hot-tub balboa-mqtt.py[9327]:   File "/usr/local/lib/python3.7/dist-packages/w1thermsensor/core.py", line 263, in get_temperature
Aug 18 03:19:00 hot-tub balboa-mqtt.py[9327]:     raw_temperature_line = self.get_raw_sensor_strings()[1]
Aug 18 03:19:00 hot-tub balboa-mqtt.py[9327]:   File "/usr/local/lib/python3.7/dist-packages/w1thermsensor/core.py", line 245, in get_raw_sensor_strings
Aug 18 03:19:00 hot-tub balboa-mqtt.py[9327]:     if data[0].strip()[-3:] != "YES" or "00 00 00 00 00 00 00 00 00" in data[0]:
Aug 18 03:19:00 hot-tub balboa-mqtt.py[9327]: IndexError: list index out of range

After I restart the script, everything is happy, but it eventually occurs again. I will be adding some logging and updating this issue as I discover more. Any guidance is appreciated.

Environment and Version

timofurrer commented 4 years ago

Do you mind changing the source a little for your installation - so that data[0].strip() and the [-3:] is done on different lines and log the individual results? Like the contents of data, data[0].strip() ?

bggardner commented 4 years ago

I added some logging to print data and its length. I'll post the results, but it will probably take a while. I suspect data is an empty string...maybe the read occurs right when the OS is trying to write to the file?

timofurrer commented 4 years ago

maybe the read occurs right when the OS is trying to write to the file?

I'm not really sure how atomic that operation is on /sys. Might be the case though. Or maybe a defective connection or something similar could cause this.

bggardner commented 4 years ago

I can confirm that data is an empty list ([]) when this exception is thrown. I do not know the cause, but how should we handle it? Assume the sensor is not ready and add len(data) == 0 to the if statement?

timofurrer commented 4 years ago

Assume the sensor is not ready and add len(data) == 0 to the if statement?

Yes, that's an option. I'm wondering if it should retry a few times (honestly, I'd rather not) - because as you report it's sporadic and it'll recover again.

For the time being though I'd recommend to raise the exception.

Can you try another installation to see if your connections are a problem? I've never had this issue myself ...

tomneko commented 2 years ago

Hi I use Python try..except block. It is works fine with ver.1.0.5

!/usr/bin/python3

from w1thermsensor import W1ThermSensor import time

sensor = W1ThermSensor()

while True: try: temperature_in_celsius = sensor.get_temperature(W1ThermSensor.DEGREES_C) print("celsius: {0:.3f}".format(temperature_in_celsius)) except IndexError as e: print(e) print(type(e))

Error

    finally:
            time.sleep(5.0)