0

I'm trying to read the input from a DHT22 sensor using an ESP-01. Following is my code:

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"

#include "driver/gpio.h"
#include "driver/hw_timer.h"

#include "esp_log.h"

#define INPUT_PIN GPIO_NUM_2

static const char *tag = "app";
static xQueueHandle queue = NULL;

void task(void *arg)
{
    int n;

    for (;;) {
        if (xQueueReceive(queue, &n, portMAX_DELAY)) {
            ESP_LOGI(tag, "sig: %d", n);
        }
    }
}

void gpio_isr_handler(void *arg)
{
    int n;

    n = gpio_get_level(INPUT_PIN);
    xQueueSendFromISR(queue, &n, NULL);
}

void app_main()
{
    gpio_config_t conf;

    conf.pin_bit_mask = 1UL << INPUT_PIN;
    gpio_config(&conf);

    gpio_install_isr_service(0);
    gpio_isr_handler_add(INPUT_PIN, gpio_isr_handler, NULL);

    queue = xQueueCreate(10, sizeof(uint32_t));
    xTaskCreate(task, "task", 2048, NULL, 10, NULL);

    gpio_set_intr_type(INPUT_PIN, GPIO_INTR_DISABLE);
    gpio_set_direction(INPUT_PIN, GPIO_MODE_OUTPUT);
    gpio_pullup_en(INPUT_PIN);

    gpio_set_level(INPUT_PIN, 0);
    vTaskDelay(1 / portTICK_RATE_MS);
    gpio_set_level(INPUT_PIN, 1);

    gpio_pullup_dis(INPUT_PIN);
    gpio_set_direction(INPUT_PIN, GPIO_MODE_INPUT);
    gpio_set_intr_type(INPUT_PIN, GPIO_INTR_ANYEDGE);
}

And following are the logs on the serial monitor:

load 0x40100000, len 7040, room 16 
tail 0
chksum 0xc0
load 0x3ffe8408, len 24, room 8 
tail 0
chksum 0x49
load 0x3ffe8420, len 3304, room 8 
tail 0
chksum 0x54
csum 0x54
I (46) boot: ESP-IDF  2nd stage bootloader
I (46) boot: compile time 10:20:22
I (46) qio_mode: Enabling default flash chip QIO
I (52) boot: SPI Speed      : 40MHz
I (58) boot: SPI Mode       : QIO
I (64) boot: SPI Flash Size : 2MB
I (70) boot: Partition Table:
I (76) boot: ## Label            Usage          Type ST Offset   Length
I (87) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (99) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (110) boot:  2 factory          factory app      00 00 00010000 000f0000
I (122) boot: End of partition table
I (128) esp_image: segment 0: paddr=0x00010010 vaddr=0x40210010 size=0x14770 ( 83824) map
0x40210010: _stext at ??:?

I (170) esp_image: segment 1: paddr=0x00024788 vaddr=0x40224780 size=0x05728 ( 22312) map
I (178) esp_image: segment 2: paddr=0x00029eb8 vaddr=0x3ffe8000 size=0x003dc (   988) load
I (181) esp_image: segment 3: paddr=0x0002a29c vaddr=0x40100000 size=0x00080 (   128) load
I (195) esp_image: segment 4: paddr=0x0002a324 vaddr=0x40100080 size=0x045a8 ( 17832) load
I (214) boot: Loaded app from partition at offset 0x10000
I (238) gpio: GPIO[2]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0

If I disconnect the wire connected to GP02 from the SDA of the sensor and mnaully connect it to VCC, ISR runs. I noticed that if I manually ground the GP02 first, ISR is not called. So, I have to first connect it to VCC for the interrupts to start. After that when I connect to ground or vcc I get the interrupts as expected. The sensor itself seems to be working, because when I use hw_timer_alarm_us() to read the value of GP02 periodically I get 0s and 1s. Problem is I can't get it to work with GPIO interrupts.

kovac
  • 343
  • 1
  • 8

0 Answers0