|
|
@@ -48,10 +48,9 @@ static esp_err_t get_change_time(gpio_num_t pin, uint8_t timeout_us, bool expect
|
|
|
return ESP_ERR_TIMEOUT;
|
|
|
}
|
|
|
|
|
|
-static inline esp_err_t dht_communication(gpio_num_t pin, uint32_t *data){
|
|
|
+static inline esp_err_t dht_communication(gpio_num_t pin, uint8_t data[DHT_DATA_BITS/8]){
|
|
|
uint32_t high_time;
|
|
|
uint32_t low_time;
|
|
|
- uint8_t checksum;
|
|
|
|
|
|
// Send start signal
|
|
|
gpio_set_direction(pin, GPIO_MODE_INPUT_OUTPUT_OD);
|
|
|
@@ -64,52 +63,56 @@ static inline esp_err_t dht_communication(gpio_num_t pin, uint32_t *data){
|
|
|
CHECK_LOGE(get_change_time(pin, 88, 0, &low_time), "Failed to get dht response"); // Wait for the sensor to pull low 80us
|
|
|
|
|
|
|
|
|
- for (uint8_t i = DHT_DATA_BITS; i > 0; i--){
|
|
|
+ for (uint8_t i = 0; i < DHT_DATA_BITS; i++){
|
|
|
|
|
|
// Wait for the sensor to pull low 50us
|
|
|
CHECK_LOGE(get_change_time(pin, 65, 1, &low_time), "Failed dht transmission %d h", i);
|
|
|
|
|
|
- // Wait for the sensor to pull high 26-28us for 0 and 70us for 1
|
|
|
+ // Wait for the sensor to pull high 26-28us for 0 or 70us for 1
|
|
|
CHECK_LOGE(get_change_time(pin, 80, 0, &high_time), "Failed dht transmission %d l", i);
|
|
|
|
|
|
- if (i < 9){
|
|
|
- checksum |= (high_time > low_time) << (i - 1);
|
|
|
- }
|
|
|
- else {
|
|
|
- *data |= (high_time > low_time) << (i - 9);
|
|
|
- }
|
|
|
+ uint8_t b = i / 8;
|
|
|
+ uint8_t m = i % 8;
|
|
|
+ if (!m) data[b] = 0; // Clear the byte at the start of each new byte
|
|
|
|
|
|
- }
|
|
|
+ data[b] |= (high_time > low_time) << (7 - m); // Set the bit if high_time is greater than low_time
|
|
|
|
|
|
- if (checksum != ((uint8_t)(*data >> 24) + (uint8_t)(*data >> 16) + (uint8_t)(*data >> 8) + (uint8_t)(*data))) {
|
|
|
- ESP_LOGE(TAG, "DHT22 checksum mismatch");
|
|
|
- return ESP_ERR_INVALID_CRC;
|
|
|
}
|
|
|
+
|
|
|
|
|
|
return ESP_OK;
|
|
|
}
|
|
|
|
|
|
+static inline int16_t data_to_int16(uint8_t MSB, uint8_t LSB){
|
|
|
+ return (int16_t)((MSB << 8) | LSB);
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
esp_err_t dht_read(gpio_num_t pin, float *temperature, float *humidity){
|
|
|
- uint32_t data;
|
|
|
+ uint8_t data[DHT_DATA_BITS/8] = {0};
|
|
|
|
|
|
gpio_set_direction(pin, GPIO_MODE_INPUT_OUTPUT_OD);
|
|
|
gpio_set_level(pin, 1);
|
|
|
|
|
|
//portENTER_CRITICAL(&mux);
|
|
|
- esp_err_t err = dht_communication(pin, &data);
|
|
|
+ esp_err_t err = dht_communication(pin, data);
|
|
|
if (err == ESP_OK) {
|
|
|
// portEXIT_CRITICAL(&mux);
|
|
|
}
|
|
|
|
|
|
+ if (data[4] != ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) {
|
|
|
+ ESP_LOGE(TAG, "DHT22 checksum mismatch");
|
|
|
+ return ESP_ERR_INVALID_CRC;
|
|
|
+ }
|
|
|
+
|
|
|
gpio_set_direction(pin, GPIO_MODE_INPUT_OUTPUT_OD);
|
|
|
gpio_set_level(pin, 1);
|
|
|
|
|
|
if (err != ESP_OK) return err;
|
|
|
|
|
|
|
|
|
- *humidity = ((int16_t)(data >> 16)) / 10.0;
|
|
|
- *temperature = ((int16_t)(data & 0xFFFF)) / 10.0;
|
|
|
+ *humidity = data_to_int16(data[0], data[1]) / 10.0;
|
|
|
+ *temperature = data_to_int16(data[2], data[3]) / 10.0;
|
|
|
return ESP_OK;
|
|
|
}
|
|
|
|