Ver Fonte

da error de ESP_log, no se porque

dacowars há 1 mês atrás
pai
commit
7edaa29c5e
1 ficheiros alterados com 14 adições e 7 exclusões
  1. 14 7
      main/main.c

+ 14 - 7
main/main.c

@@ -11,6 +11,8 @@
 
 static const char *TAG = "DHT22";
 
+static portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
+
 // Macro to check the return value of a function and log an error if it is not ESP_OK
 #define CHECK_LOGE(x, msg, ...) do { \
         esp_err_t __; \
@@ -23,7 +25,6 @@ static const char *TAG = "DHT22";
 
 gpio_num_t dht_pin = DHT_PIN;
 
-static portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
 
 static esp_err_t dht_attach_pin(gpio_num_t pin){
     if (pin < GPIO_NUM_0 || pin >= GPIO_NUM_MAX) {
@@ -65,10 +66,10 @@ static inline esp_err_t dht_communication(gpio_num_t pin, uint32_t *data){
     for (uint8_t i = DHT_DATA_BITS; i > 0; i--){
         
         // Wait for the sensor to pull low 50us
-        CHECK_LOGE(get_change_time(pin, 50, 0, &low_time), "Failed dht transmission"); 
+        CHECK_LOGE(get_change_time(pin, 65, 0, &low_time), "Failed dht transmission"); 
         
         // Wait for the sensor to pull high 26-28us for 0 and 70us for 1
-        CHECK_LOGE(get_change_time(pin, 70, 1, &high_time), "Failed dht transmission"); 
+        CHECK_LOGE(get_change_time(pin, 80, 1, &high_time), "Failed dht transmission"); 
 
         if (i < 9){
             checksum |= (high_time > low_time) << (i - 1);
@@ -80,7 +81,7 @@ static inline esp_err_t dht_communication(gpio_num_t pin, uint32_t *data){
     }
 
     if (checksum != ((uint8_t)(*data >> 24) + (uint8_t)(*data >> 16) + (uint8_t)(*data >> 8) + (uint8_t)(*data))) {
-        ESP_LOGE(TAG, "DHT22 checksum mismatch");
+        //ESP_LOGE(TAG, "DHT22 checksum mismatch");
         return ESP_ERR_INVALID_CRC;
     }
 
@@ -95,12 +96,17 @@ esp_err_t dht_read(gpio_num_t pin, float *temperature, float *humidity){
     gpio_set_level(pin, 1); 
 
     portENTER_CRITICAL(&mux);
-    CHECK_LOGE(dht_communication(pin, &data), "Failed to read dht data");
-    portEXIT_CRITICAL(&mux);
+    esp_err_t err = dht_communication(pin, &data);
+    if (err == ESP_OK) {
+        portEXIT_CRITICAL(&mux);
+    }
 
     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;
     return ESP_OK;
@@ -108,6 +114,7 @@ esp_err_t dht_read(gpio_num_t pin, float *temperature, float *humidity){
 
 void app_main(void)
 {
+    
     float temperature;
     float humidity;
     esp_err_t err = dht_attach_pin(dht_pin);
@@ -116,7 +123,7 @@ void app_main(void)
         vTaskDelay(pdMS_TO_TICKS(5000)); // Wait for 5 seconds before retrying
         return;
     }
-
+    vTaskDelay(pdMS_TO_TICKS(2000)); // Wait for 2 seconds before starting the main loop
     while (1) {
         if (dht_read(dht_pin, &temperature, &humidity) == ESP_OK) {
             ESP_LOGI(TAG, "Temperature: %.1f°C, Humidity: %.1f%%", temperature, humidity);