https://toitlang.org/ logo
pkg dhtxx: reports wrong temperature value if cold...
# help
k
if temperature gets negative it returns wrong values: T: 0.00, H: 64.60 T: 3276.90, H: 64.60 reason might be here:
Copy code
parse_temperature_ data/ByteArray -> float:
    return (BIG_ENDIAN.uint16 data driver.Driver.TEMPERATURE_INTEGRAL_PART_) / 10.0
=> it works, but I think could be done nicer:
Copy code
parse_temperature_ data/ByteArray -> float:
    return not (driver.Driver.TEMPERATURE_INTEGRAL_PART_ & 0x8000) ? 
      (BIG_ENDIAN.uint16 data driver.Driver.TEMPERATURE_INTEGRAL_PART_) / 10.0 :  
      -((BIG_ENDIAN.uint16 data driver.Driver.TEMPERATURE_INTEGRAL_PART_) & 0x7ff) / 10.0
f
So the first bit is a sign bit?
k
yes, but I still struggle with correct boolean expression
f
I will fix the package when I'm back from vacation. Please post a few values you get from the sensor so I can verify that the new code will work
k
DHT data: #[0x02, 0x61, 0x00, 0x01, 0x64] 333: T: 0.10, H: 60.90 DHT data: #[0x02, 0x61, 0x00, 0x00, 0x63] 334: T: 0.00, H: 60.90 DHT data: #[0x02, 0x62, 0x00, 0x00, 0x64] 335: T: 0.00, H: 61.00 DHT data: #[0x02, 0x63, 0x00, 0x00, 0x65] 336: T: 0.00, H: 61.10 DHT data: #[0x02, 0x64, 0x00, 0x00, 0x66] 337: T: 0.00, H: 61.20 DHT data: #[0x02, 0x65, 0x00, 0x00, 0x67] 338: T: 0.00, H: 61.30 DHT data: #[0x02, 0x64, 0x80, 0x01, 0xe7] 339: T: -0.10, H: 61.20 DHT data: #[0x02, 0x64, 0x80, 0x01, 0xe7] 340: T: -0.10, H: 61.20 DHT data: #[0x02, 0x63, 0x80, 0x01, 0xe6] 341: T: -0.10, H: 61.10 DHT data: #[0x02, 0x61, 0x80, 0x01, 0xe4] 342: T: -0.10, H: 60.90 DHT data: #[0x02, 0x60, 0x80, 0x02, 0xe4] 343: T: -0.20, H: 60.80
Hope you have cold enough temperatures then. enjoy your vacation.
you changed only the import
Copy code
import io show BIG_ENDIAN`
🤔 I do not understand why this change removes the error. Could you please help me .
f
The PR with the fix hasn't been committed yet: https://github.com/toitware/toit-dhtxx/pull/12
k
😬 🤐
f
A new version of the package has been published.