diff --git a/components/dht22/dht22.c b/components/dht22/dht22.c index bf23858..a77f89a 100644 --- a/components/dht22/dht22.c +++ b/components/dht22/dht22.c @@ -53,6 +53,7 @@ uint32_t wait_for_signal_to_change_from(int signal_level) { dht22_error dht22_read() { portDISABLE_INTERRUPTS(); + /* * From datasheet: * - Host pulls low "beyond at least 1ms" @@ -73,8 +74,10 @@ dht22_error dht22_read() { uint32_t elapsed_time; elapsed_time = wait_for_signal_to_change_from(1); if (elapsed_time == DHT22_TIMEOUT_ERROR) { + portENABLE_INTERRUPTS(); return DHT22_TIMEOUT_ERROR; } else if (elapsed_time > 40) { + portENABLE_INTERRUPTS(); return DHT22_TIMING_ERROR; } @@ -86,15 +89,19 @@ dht22_error dht22_read() { elapsed_time = wait_for_signal_to_change_from(0); if (elapsed_time == DHT22_TIMEOUT_ERROR) { + portENABLE_INTERRUPTS(); return DHT22_TIMEOUT_ERROR; } else if (elapsed_time < 70 || elapsed_time > 90) { + portENABLE_INTERRUPTS(); return DHT22_TIMING_ERROR; } elapsed_time = wait_for_signal_to_change_from(1); if (elapsed_time == DHT22_TIMEOUT_ERROR) { + portENABLE_INTERRUPTS(); return DHT22_TIMEOUT_ERROR; } else if (elapsed_time < 70 || elapsed_time > 90) { + portENABLE_INTERRUPTS(); return DHT22_TIMING_ERROR; } @@ -108,7 +115,9 @@ dht22_error dht22_read() { uint8_t data[5] = {0, 0, 0, 0, 0}; for (uint8_t i = 0; i < 40; i++) { 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; } diff --git a/main/main.c b/main/main.c index 30977c9..75fdb14 100644 --- a/main/main.c +++ b/main/main.c @@ -65,21 +65,18 @@ uint32_t num_samples_last_ten_mins = 0; void read_from_dht22() { // From datasheet: // 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) { - // 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(); - portENABLE_INTERRUPTS(); if (ret != DHT22_OK) { dht22_handle_error(ret); } else { - latest_datapoint.temp = dht22_temperature(); 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()) { // 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 xTaskCreate(read_from_dht22, "DHT22", - 8 * 1024, // honestly I have _no_ idea + 4 * 1024, // honestly I have _no_ idea NULL, 5, // no idea either &dht22_handle); @@ -162,6 +159,6 @@ void app_main(void) { &pms5003_handle); #endif /* CONFIG_PMS5003_ENABLED */ + start_wifi(); - //start_wifi(); }