Files
esp-air-monitor/components/dht22/dht22.c
Arthur Roberts 822b73597e Looked into another DHT22 library for inspiration
Still not working and I really can't tell why
2023-10-30 23:37:36 +00:00

175 lines
4.7 KiB
C

#include "dht22.h"
#include "esp_log.h"
#include "hal/gpio_types.h"
#include "sdkconfig.h"
#include "driver/gpio.h"
#include <rom/ets_sys.h>
#include "esp_timer.h"
#include "freertos/portmacro.h"
#define DATA_GPIO CONFIG_DHT22_DATA_GPIO
#if DATA_GPIO == -1
#error "Please define your DHT22 GPIO Data Pin"
#endif /* CONFIG_DHT22_DATA_GPIO == -1 */
static const char *TAG = "DHT22";
deci_percent _dht22_relative_humidity = UINT16_MAX;
deci_degrees_c _dht22_temperature = INT16_MIN;
void dht22_handle_error(dht22_error e) {
if (e == DHT22_OK) {
ESP_LOGI(TAG, "No errors reading DHT22 value");
} else if (e == DHT22_CHECKSUM_ERROR) {
ESP_LOGE(TAG, "Error calculating checksum");
} else if (e == DHT22_TIMING_ERROR) {
ESP_LOGE(TAG, "Timing error while obtaining data");
} else if (e == DHT22_TIMEOUT_ERROR) {
ESP_LOGE(TAG, "Timeout error - waited >2ms for signal to change");
} else {
ESP_LOGE(TAG, "Unknown error: %i", e);
}
}
uint32_t wait_for_signal_to_change_from(int signal_level) {
uint64_t start_time = esp_timer_get_time();
uint64_t next_time = start_time;
/*
A vague attempt at "waiting" until the pin comes some kind of level
if (gpio_get_level(CONFIG_DHT22_DATA_GPIO) != signal_level) {
ets_delay_us(15);
}
*/
while (gpio_get_level(CONFIG_DHT22_DATA_GPIO) == signal_level) {
next_time = esp_timer_get_time();
if ((next_time - start_time) >= 1000 * 10) {
return DHT22_TIMEOUT_ERROR;
}
}
uint32_t elapsed_time = next_time - start_time;
if (elapsed_time == 0) {
return 1000;
}
return elapsed_time;
}
dht22_error dht22_read() {
uint8_t data[5] = {0, 0, 0, 0, 0};
uint32_t elapsed_time;
/*
* From datasheet:
* - Host pulls low "beyond at least 1ms"
* - Host pulls up 20us to 40us
*/
gpio_set_direction(CONFIG_DHT22_DATA_GPIO, GPIO_MODE_OUTPUT);
gpio_set_level(CONFIG_DHT22_DATA_GPIO, 0);
ets_delay_us(5000);
gpio_set_level(CONFIG_DHT22_DATA_GPIO, 1);
ets_delay_us(25);
/*
* From datasheet:
* - DHT pulls low 80us
* - DHT pulls high 80us
*/
gpio_set_direction(CONFIG_DHT22_DATA_GPIO, GPIO_MODE_INPUT);
// Maybe wait here until it goes high?
while (gpio_get_level(CONFIG_DHT22_DATA_GPIO) == 1) {
// wait for a while - should only be 20 uS
}
elapsed_time = wait_for_signal_to_change_from(0);
if (elapsed_time < 70 || elapsed_time > 90) {
return DHT22_TIMING_ERROR;
}
elapsed_time = wait_for_signal_to_change_from(1);
if (elapsed_time < 70 || elapsed_time > 90) {
return DHT22_TIMING_ERROR;
}
/*
* From datasheet:
* - DHT pulls low 50us -> into to next bit
* - DHT pulls high:
* - if 26-28us -> 0 bit
* - if 70us -> 1 bit
*/
//portENABLE_INTERRUPTS();
uint8_t byte_i = 0;
uint8_t bit_i = 7;
for (int i = 0; i < 40; i++) {
//for (int byte_i = 0; byte_i <= 4; byte_i++) {
// for (int bit_i = 7; bit_i >= 0; bit_i--) {
elapsed_time = wait_for_signal_to_change_from(0);
if (elapsed_time == DHT22_TIMEOUT_ERROR) {
return DHT22_TIMEOUT_ERROR;
}
if (elapsed_time < 35 || elapsed_time > 65) {
return elapsed_time;
}
elapsed_time = wait_for_signal_to_change_from(1);
if (elapsed_time < 60 || elapsed_time > 80) {
data[byte_i] |= (1 << bit_i);
// TODO: check why this doesn't work
//data[i / 8] |= (1 << (7 - (i % 8));
}
//ESP_LOGE(TAG, "bit: %i, byte: %i, elapsed_time: %lu", bit_i, byte_i, elapsed_time);
//}
///*
if (bit_i == 0) {
bit_i = 7;
byte_i++;
} else {
bit_i--;
}
}
portENABLE_INTERRUPTS();
ESP_LOGE(TAG, "data[0-4]: %x %x %x %x %x", data[0], data[1], data[2], data[3], data[4]);
// TODO test that data[x] << 8 will be promoted to 16bits otherwise I'm
// shifting them to just zeros
_dht22_relative_humidity = (data[0] << 8) | data[1];
_dht22_temperature = ((data[2] & 0x7f) << 8) | data[3];
float relative_humidity = data[0];
relative_humidity *= 0x100;
relative_humidity += data[1];
relative_humidity /= 10;
float temp = data[2] & 0x7f;
temp *= 0x100;
temp += data[3];
temp /= 10;
if (data[2] & 0x80) {
temp *= -1;
}
ESP_LOGE(TAG, "rh: %f, temp: %f", relative_humidity, temp);
if (data[2] & 0x80) {
_dht22_temperature = -1 * _dht22_temperature;
}
ESP_LOGE(TAG, "rh: %i, temp: %i", _dht22_relative_humidity / 10, _dht22_temperature / 10);
uint8_t checksum_val = (data[0] + data[1] + data[2] + data[3]) & 0xff;
if (checksum_val != data[4]) {
_dht22_relative_humidity = UINT16_MAX;
_dht22_temperature = INT16_MIN;
return DHT22_CHECKSUM_ERROR;
}
return DHT22_OK;
}
deci_percent dht22_relative_humidity() { return _dht22_relative_humidity; }
deci_degrees_c dht22_temperature() { return _dht22_temperature; }