ushiboy / pi1wire

1Wire Sensor Library for Raspberry PI

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support setting DS18B20 resolution

Misiu opened this issue · comments

I've used DS18B20 in the past with Arduino or ESP8266 and I was able to set the resolution: 9, 10, 11, or 12 bits, corresponding to increments of 0.5°C, 0.25°C, 0.125°C, and 0.0625°C.
Your library reads the value as is, so if there are a lot of sensors connected to PI and stored in the database (Home Assistant in my case) gets a lot of data.
Ideally, we should be able to specify the precision we need, so that the system using the sensor won't have to round it.

Sadly I'm not a Python developer, so I won't be able to add this by myself, but I think it's possible: https://raspberrypi.stackexchange.com/questions/71563/how-to-set-precision-of-ds18b20-via-w1-therm

Having this change we could change the Home Assistant integration to support this function.

So we can decide if we want slower, more accurate readings (and store more data in the DB) or faster but less accurate (0.5°C accuracy) and store fewer data in the DB.

Please wait as I think of support features.

@Misiu
I've implemented this as a feature in version 0.3.0, please try it out.

@ushiboy thank you for such a fast implementation.
Setting the resolution should be done as rarely as possible because the value is stored in EEPROM.
Consider adding reading sensor's current resolution and saving a new one only if the new value is different than the previous one.
Ref: https://raspberrypi.stackexchange.com/questions/71563/how-to-set-precision-of-ds18b20-via-w1-therm
I also noticed one comment after the answer:

Thanks! And I subsequently learned that writing a zero tells the driver to write it into the device's EEPROM, thereby making it permanent.

Other implementation: https://github.com/timofurrer/w1thermsensor/blob/42a9f8520b0edf088fb0a1b89428805d8adc9a7a/src/w1thermsensor/core.py#L306

Read resolution sample: https://github.com/timofurrer/w1thermsensor/blob/42a9f8520b0edf088fb0a1b89428805d8adc9a7a/src/w1thermsensor/core.py#L426

@Misiu
It's ok. I'm aware of the EEPROM issue and this implementation does not write any settings to the EEPROM (so the resolution will return to default values when power is turned off).

In that case, we should be safe. The resolution will be set during sensor initialization and changed when needed.