diff --git a/drivers/dht/dht.c b/drivers/dht/dht.c index 5f26a0b533a8..437e7ae4c023 100644 --- a/drivers/dht/dht.c +++ b/drivers/dht/dht.c @@ -272,8 +272,12 @@ int dht_read(dht_t *dev, int16_t *temp, int16_t *hum) return ret; } - *hum = dev->last_val.humidity; - *temp = dev->last_val.temperature; + if (hum != NULL) { + *hum = dev->last_val.humidity; + } + if (temp != NULL) { + *temp = dev->last_val.temperature; + } return 0; } diff --git a/drivers/include/dht.h b/drivers/include/dht.h index c190b2dd3c4c..d02b4367b02d 100644 --- a/drivers/include/dht.h +++ b/drivers/include/dht.h @@ -107,14 +107,16 @@ typedef struct { int dht_init(dht_t *dev, const dht_params_t *params); /** - * @brief get a new temperature and humidity value from the device + * @brief get a new temperature and/or humidity value from the device * * @note if reading fails or checksum is invalid, no new values will be * written into the result values * * @param[in] dev device descriptor of a DHT device - * @param[out] temp temperature value [in °C * 10^-1] - * @param[out] hum relative humidity value [in percent * 10^-1] + * @param[out] temp temperature value [in °C * 10^-1], + * may be NULL if not needed + * @param[out] hum relative humidity value [in percent * 10^-1], + * may be NULL if not needed * * @retval 0 Success * @retval -ENODEV The sensor did not respond to the transmission of a diff --git a/tests/drivers/dht/main.c b/tests/drivers/dht/main.c index 8500a6f7dba2..ce70d89a56c4 100644 --- a/tests/drivers/dht/main.c +++ b/tests/drivers/dht/main.c @@ -52,7 +52,15 @@ int main(void) ztimer_sleep(ZTIMER_USEC, DELAY); if (dht_read(&dev, &temp, &hum) != DHT_OK) { - puts("Error reading values"); + puts("Error reading both values"); + continue; + } + if (dht_read(&dev, &temp, NULL) != DHT_OK) { + puts("Error reading just temperature"); + continue; + } + if (dht_read(&dev, NULL, &hum) != DHT_OK) { + puts("Error reading just humidity"); continue; }