|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +/* |
| 7 | + * @file: aht20_demo.c |
| 8 | + * |
| 9 | + * @brief: A simple demo to use the AHT20 driver |
| 10 | + * |
| 11 | + * @date: May 2, 2025 |
| 12 | + * |
| 13 | + * @Author: Rohan Jeet <[email protected]> |
| 14 | + * |
| 15 | + */ |
| 16 | +#include <stdio.h> |
| 17 | +#include <stdbool.h> |
| 18 | +#include <unistd.h> |
| 19 | +#include "aht20.h" |
| 20 | + |
| 21 | +//i2c configuration values |
| 22 | +#define I2C_MASTER_SCL_IO (22) // SCL pin |
| 23 | +#define I2C_MASTER_SDA_IO (21) // SDA pin |
| 24 | +#define I2C_MASTER_NUM I2C_NUM_0 |
| 25 | +#define I2C_MASTER_FREQ_HZ (400000) // I2C frequency |
| 26 | + |
| 27 | +i2c_bus_handle_t my_i2c_bus_handle; |
| 28 | + |
| 29 | +void i2c_master_init(void) |
| 30 | +{ |
| 31 | + const i2c_config_t conf = { |
| 32 | + .mode = I2C_MODE_MASTER, |
| 33 | + .sda_io_num = I2C_MASTER_SDA_IO, |
| 34 | + .sda_pullup_en = GPIO_PULLUP_ENABLE, |
| 35 | + .scl_io_num = I2C_MASTER_SCL_IO, |
| 36 | + .scl_pullup_en = GPIO_PULLUP_ENABLE, |
| 37 | + .master.clk_speed = I2C_MASTER_FREQ_HZ, |
| 38 | + }; |
| 39 | + printf("requesting i2c bus handle\n"); |
| 40 | + my_i2c_bus_handle = i2c_bus_create(I2C_MASTER_NUM, &conf); |
| 41 | + printf("i2c bus handle acquired\n"); |
| 42 | + |
| 43 | +} |
| 44 | + |
| 45 | +void read_aht20(void *my_aht20_handle) |
| 46 | +{ |
| 47 | + |
| 48 | + aht20_handle_t aht20_handle = (aht20_handle_t) my_aht20_handle; //retrieve the AHT20 device handle copy |
| 49 | + vTaskDelay(400 / portTICK_PERIOD_MS); |
| 50 | + while (1) { |
| 51 | + //read both humidity and temperature at once from device, using AHT20 device handle |
| 52 | + aht20_read_humiture(aht20_handle); |
| 53 | + //access the results stored in AHT20 device object, using the AHT20 device handle |
| 54 | + //other apis require user to explicitly pass variable address to hold data |
| 55 | + printf("tempertature = %.2fC humidity = %.3f \n", aht20_handle->humiture.temperature, aht20_handle->humiture.humidity); |
| 56 | + vTaskDelay(1000 / portTICK_PERIOD_MS); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +void init_aht20() |
| 61 | +{ |
| 62 | + //create a AHT20 device object and receive a device handle for it |
| 63 | + aht20_handle_t aht20_handle = aht20_create(my_i2c_bus_handle, AHT20_ADDRESS_LOW); |
| 64 | + |
| 65 | + printf("initializing aht20\n"); |
| 66 | + //use the previously created AHT20 device handle for initializing the associated device |
| 67 | + while (aht20_init(aht20_handle) != ESP_OK) { // wait until it is initialized |
| 68 | + vTaskDelay(100 / portTICK_PERIOD_MS); |
| 69 | + } |
| 70 | + printf("aht20 initialized\n"); |
| 71 | + |
| 72 | + //creating a task to read from AHT20, passing the AHT20 device handle copy |
| 73 | + xTaskCreate(read_aht20, "aht20_tester", 2500, aht20_handle, 5, NULL); |
| 74 | +} |
| 75 | + |
| 76 | +void app_main(void) |
| 77 | +{ |
| 78 | + i2c_master_init(); //initialize i2c master |
| 79 | + init_aht20(); // user defined function for aht20 initialization |
| 80 | +} |
0 commit comments