|
| 1 | +/* |
| 2 | + * @file: aht20_demo.c |
| 3 | + * |
| 4 | + * @brief: A simple demo to use the AHT20 driver |
| 5 | + * |
| 6 | + * @date: April 20, 2025 |
| 7 | + * |
| 8 | + * Copyright 2025 Rohan Jeet <jeetrohan92@gmail.com> |
| 9 | + * |
| 10 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 | + * you may not use this file except in compliance with the License. |
| 12 | + * You may obtain a copy of the License at |
| 13 | + * |
| 14 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | + * |
| 16 | + * Unless required by applicable law or agreed to in writing, software |
| 17 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | + * See the License for the specific language governing permissions and |
| 20 | + * limitations under the License. |
| 21 | + */ |
| 22 | +#include <stdio.h> |
| 23 | +#include <stdbool.h> |
| 24 | +#include <unistd.h> |
| 25 | +#include "aht20.h" |
| 26 | + |
| 27 | + |
| 28 | +//i2c configuration values |
| 29 | +#define I2C_MASTER_SCL_IO (22) // SCL pin |
| 30 | +#define I2C_MASTER_SDA_IO (21) // SDA pin |
| 31 | +#define I2C_MASTER_NUM I2C_NUM_0 |
| 32 | +#define I2C_MASTER_FREQ_HZ (400000) // I2C frequency |
| 33 | + |
| 34 | +i2c_master_bus_handle_t my_i2c_bus_handle; |
| 35 | + |
| 36 | +void i2c_master_init(void) |
| 37 | +{ |
| 38 | + i2c_master_bus_config_t i2c_mst_config = { |
| 39 | + .clk_source = I2C_CLK_SRC_DEFAULT, |
| 40 | + .i2c_port = I2C_MASTER_NUM, |
| 41 | + .scl_io_num = I2C_MASTER_SCL_IO, |
| 42 | + .sda_io_num = I2C_MASTER_SDA_IO, |
| 43 | + .glitch_ignore_cnt = 7, |
| 44 | + .flags.enable_internal_pullup = true, |
| 45 | + }; |
| 46 | + printf("requesting i2c bus handle\n"); |
| 47 | + ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_mst_config, &my_i2c_bus_handle)); |
| 48 | + printf("i2c bus handle acquired\n"); |
| 49 | + |
| 50 | + |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +void read_aht20 (void *my_aht20_handle) |
| 55 | +{ |
| 56 | + |
| 57 | + aht20_handle_t aht20_handle = (aht20_handle_t) my_aht20_handle; //retrieve the AHT20 device handle copy |
| 58 | + vTaskDelay(400 / portTICK_PERIOD_MS); |
| 59 | + while (1) { |
| 60 | + //read both humidity and temperature at once from device, using AHT20 device handle |
| 61 | + aht20_read_humiture(aht20_handle); |
| 62 | + //access the results stored in AHT20 device object, using the AHT20 device handle |
| 63 | + //other apis require user to explicitly pass variable address to hold data |
| 64 | + printf("tempertature = %.2fC humidity = %.3f \n", aht20_handle->humiture.temperature, aht20_handle->humiture.humidity); |
| 65 | + vTaskDelay(1000 / portTICK_PERIOD_MS); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +void init_aht20() |
| 71 | +{ |
| 72 | + //create a AHT20 device object and receive a device handle for it |
| 73 | + aht20_handle_t aht20_handle = aht20_create( my_i2c_bus_handle, AHT20_ADDRESS_LOW ); //addresses in aht.h |
| 74 | + |
| 75 | + printf("initializing aht20\n"); |
| 76 | + //use the previously created AHT20 device handle for initializing the associated device |
| 77 | + while (aht20_init(aht20_handle) != ESP_OK) { // wait until it is initialized |
| 78 | + vTaskDelay(100 / portTICK_PERIOD_MS); |
| 79 | + } |
| 80 | + printf("aht20 initialized\n"); |
| 81 | + |
| 82 | + //creating a task to read from AHT20, passing the AHT20 device handle copy |
| 83 | + xTaskCreate(read_aht20, "aht20_tester", 2500, aht20_handle, 5, NULL); |
| 84 | +} |
| 85 | + |
| 86 | +void app_main(void) |
| 87 | +{ |
| 88 | + i2c_master_init(); //initialize i2c master |
| 89 | + init_aht20(); // user defined function for aht20 initialization |
| 90 | +} |
0 commit comments