The reading doesn't work with WiFi enabled

Just get timing errors
This commit is contained in:
2023-10-31 20:59:54 +00:00
parent 03b4bc7e5a
commit 36041e63d2
2 changed files with 17 additions and 11 deletions

View File

@@ -53,6 +53,7 @@ uint32_t wait_for_signal_to_change_from(int signal_level) {
dht22_error dht22_read() { dht22_error dht22_read() {
portDISABLE_INTERRUPTS(); portDISABLE_INTERRUPTS();
/* /*
* From datasheet: * From datasheet:
* - Host pulls low "beyond at least 1ms" * - Host pulls low "beyond at least 1ms"
@@ -73,8 +74,10 @@ dht22_error dht22_read() {
uint32_t elapsed_time; uint32_t elapsed_time;
elapsed_time = wait_for_signal_to_change_from(1); elapsed_time = wait_for_signal_to_change_from(1);
if (elapsed_time == DHT22_TIMEOUT_ERROR) { if (elapsed_time == DHT22_TIMEOUT_ERROR) {
portENABLE_INTERRUPTS();
return DHT22_TIMEOUT_ERROR; return DHT22_TIMEOUT_ERROR;
} else if (elapsed_time > 40) { } else if (elapsed_time > 40) {
portENABLE_INTERRUPTS();
return DHT22_TIMING_ERROR; return DHT22_TIMING_ERROR;
} }
@@ -86,15 +89,19 @@ dht22_error dht22_read() {
elapsed_time = wait_for_signal_to_change_from(0); elapsed_time = wait_for_signal_to_change_from(0);
if (elapsed_time == DHT22_TIMEOUT_ERROR) { if (elapsed_time == DHT22_TIMEOUT_ERROR) {
portENABLE_INTERRUPTS();
return DHT22_TIMEOUT_ERROR; return DHT22_TIMEOUT_ERROR;
} else if (elapsed_time < 70 || elapsed_time > 90) { } else if (elapsed_time < 70 || elapsed_time > 90) {
portENABLE_INTERRUPTS();
return DHT22_TIMING_ERROR; return DHT22_TIMING_ERROR;
} }
elapsed_time = wait_for_signal_to_change_from(1); elapsed_time = wait_for_signal_to_change_from(1);
if (elapsed_time == DHT22_TIMEOUT_ERROR) { if (elapsed_time == DHT22_TIMEOUT_ERROR) {
portENABLE_INTERRUPTS();
return DHT22_TIMEOUT_ERROR; return DHT22_TIMEOUT_ERROR;
} else if (elapsed_time < 70 || elapsed_time > 90) { } else if (elapsed_time < 70 || elapsed_time > 90) {
portENABLE_INTERRUPTS();
return DHT22_TIMING_ERROR; return DHT22_TIMING_ERROR;
} }
@@ -108,7 +115,9 @@ dht22_error dht22_read() {
uint8_t data[5] = {0, 0, 0, 0, 0}; uint8_t data[5] = {0, 0, 0, 0, 0};
for (uint8_t i = 0; i < 40; i++) { for (uint8_t i = 0; i < 40; i++) {
elapsed_time = wait_for_signal_to_change_from(0); elapsed_time = wait_for_signal_to_change_from(0);
if (elapsed_time == DHT22_TIMEOUT_ERROR || elapsed_time < 30 || elapsed_time > 70) { if (elapsed_time == DHT22_TIMEOUT_ERROR || elapsed_time < 30 ||
elapsed_time > 70) {
portENABLE_INTERRUPTS();
return DHT22_TIMEOUT_ERROR; return DHT22_TIMEOUT_ERROR;
} }

View File

@@ -65,21 +65,18 @@ uint32_t num_samples_last_ten_mins = 0;
void read_from_dht22() { void read_from_dht22() {
// From datasheet: // From datasheet:
// Don't start within 1 second of powering on // Don't start within 1 second of powering on
// vTaskDelay(pdMS_TO_TICKS(2000)); FIXME - still doesn't actually fix the problem though // FIXME - still doesn't actually fix the problem though
//vTaskDelay(pdMS_TO_TICKS(20000));
while (run_dht) { while (run_dht) {
// Data reading is based on time, so need to block interrups etc
// I _think_ these are freeRTOS task interrupts - I wonder if
// I need to also block ESP interrupts? Should I even do that?
portDISABLE_INTERRUPTS();
int ret = dht22_read(); int ret = dht22_read();
portENABLE_INTERRUPTS();
if (ret != DHT22_OK) { if (ret != DHT22_OK) {
dht22_handle_error(ret); dht22_handle_error(ret);
} else { } else {
latest_datapoint.temp = dht22_temperature(); latest_datapoint.temp = dht22_temperature();
latest_datapoint.rh = dht22_relative_humidity(); latest_datapoint.rh = dht22_relative_humidity();
ESP_LOGW(TAG, "%f & %f", latest_datapoint.temp / 10.0, latest_datapoint.rh / 10.0 ); ESP_LOGW(TAG, "temperature: %f & relative humidity: %f",
latest_datapoint.temp / 10.0, latest_datapoint.rh / 10.0 );
if (has_ntp_time_obtained_once()) { if (has_ntp_time_obtained_once()) {
// I _think_ I only want to be doing that rolling average and saving // I _think_ I only want to be doing that rolling average and saving
@@ -146,7 +143,7 @@ void app_main(void) {
#ifdef CONFIG_DHT22_ENABLED #ifdef CONFIG_DHT22_ENABLED
xTaskCreate(read_from_dht22, "DHT22", xTaskCreate(read_from_dht22, "DHT22",
8 * 1024, // honestly I have _no_ idea 4 * 1024, // honestly I have _no_ idea
NULL, NULL,
5, // no idea either 5, // no idea either
&dht22_handle); &dht22_handle);
@@ -162,6 +159,6 @@ void app_main(void) {
&pms5003_handle); &pms5003_handle);
#endif /* CONFIG_PMS5003_ENABLED */ #endif /* CONFIG_PMS5003_ENABLED */
start_wifi();
//start_wifi();
} }