UncleRus / esp-idf-lib

Component library for ESP32-xx and ESP8266
https://esp-idf-lib.readthedocs.io/en/latest/
1.37k stars 425 forks source link

Hi UncleRus I altered your library to read data from AHT10 #333

Closed shivapower1985 closed 2 years ago

shivapower1985 commented 2 years ago

The issue

Hi UncleRus, I altered the library to read temperature and humidity data from AHT10. Please see the Mail code file and output below. Please help me to solve the problem

################Here is my code below############### // MAIN FILE

include "esp_event.h"

include "esp_log.h"

include "esp_spi_flash.h"

include "esp_system.h"

include "freertos/FreeRTOS.h"

include "freertos/event_groups.h"

include "freertos/task.h"

include "nvs_flash.h"

include "sdkconfig.h"

include "driver/gpio.h"

include "esp_log.h"

include "driver/i2c.h"

include

include "esp_err.h"

define I2C_MASTER_NUM 0

define ESP_ERR_AHT10_BASE 0x30000

define ESP_ERR_TOO_SLOW_TICK_RATE (ESP_ERR_AHT10_BASE + 1)

define ESP_ERR_AHT10_NOT_DETECTED (ESP_ERR_AHT10_BASE + 2)

define ESP_ERR_AHT10_CALIBRATION_FAILURE (ESP_ERR_AHT10_BASE + 3)

// Tag for logging from this file static const char TAG = "user"; static const char AHT10_I2C_LOG_TAG = "AHT10 I2C Read";

// slave address of aht10 uint8_t fd_AHT = 0x39;

uint8_t pl_i2c_read(uint8_t slave_addr) { // hold payload data to read uint8_t byte = 0;

// create a command link which holds the sequence of
// i2c commands to execute.
i2c_cmd_handle_t cmd_link = i2c_cmd_link_create();

// populate the command link with start bit, write slave
// address (write bit 0), read payload byte and stop
// bit.
i2c_master_start(cmd_link);
i2c_master_write_byte(cmd_link,
                      ((slave_addr << 1) | 0x01),
                      true);
i2c_master_read_byte(cmd_link, &byte, true);
i2c_master_stop(cmd_link);

// execute commands and the read value will be saved to
// 'byte'
byte = i2c_master_cmd_begin(I2C_NUM_0,
                     cmd_link,
                     1000 / portTICK_PERIOD_MS);

// delete command link
i2c_cmd_link_delete(cmd_link);

ESP_LOGV(TAG,
         "master read from 0x%02X: byte: 0x%02X",
         slave_addr,
         byte);

// return the read value, invalid is 0xFF
ESP_LOGI(TAG, "byte= %x", byte);
return byte;

}

esp_err_t pl_i2c_init(gpio_num_t sda_pin, gpio_num_t scl_pin, uint32_t clock_speed) { // configuration object for i2c driver esp_err_t err; i2c_config_t i2c_conf; int i2c_master_port = I2C_MASTER_NUM; memset(&i2c_conf, 0, sizeof(i2c_config_t)); // set parameters a master i2c_conf.mode = I2C_MODE_MASTER; i2c_conf.sda_io_num = sda_pin; i2c_conf.sda_pullup_en = GPIO_PULLUP_ENABLE; i2c_conf.scl_io_num = scl_pin; i2c_conf.scl_pullup_en = GPIO_PULLUP_ENABLE; i2c_conf.master.clk_speed = clock_speed;

// apply configuration with port 0
err = i2c_param_config(i2c_master_port, &i2c_conf);

// install driver
i2c_driver_install(i2c_master_port, i2c_conf.mode, 0, 0, 0);

ESP_LOGI(TAG, "finished init");
ESP_LOGI(AHT10_I2C_LOG_TAG, "I2C successfully initialized");

if (err != ESP_OK) {
    ESP_LOGE(TAG, "I2C driver configuration failed with error = %d", err);
    return ESP_ERR_AHT10_NOT_DETECTED;
}
return ESP_OK;

} //end

//Description // Write 0x2E into reg 0xF4, wait 4.5ms and then read // registers 0xF6 (MSB), 0xF7 (LSB). Log the value to the // console. //*/ // Get the temperature int32_t al_aht10_get_ut(uint8_t slave_addr) { int32_t ut = 0;

// delay time: 4.5ms = 4500µs
vTaskDelay(5);

// read out uncompensated temperature
ut=pl_i2c_read(slave_addr);

return ut;

}

int32_t al_aht10_get_temperature() { float t = al_aht10_get_ut(fd_AHT); // algorithm for the temperature

// divide the temperature by 10 to get the value in multiples of 1.0 celsius
ESP_LOGD(TAG,
         "temperature in degree celsius: %.1f",
         t);
ESP_LOGI(TAG, "temperature in degree celsius: %f",
         t);

//ESP_LOGI(TAG, "temperature in raw degree celsius: %.1f",
//         (float)t);

return t;

}

// application entry point void app_main() { // Initialize NVS esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); } ESP_ERROR_CHECK(ret);

// log on app start as warning to make it visible
ESP_LOGW(TAG, "Hello world!");

esp_log_level_set("user", ESP_LOG_DEBUG);

//esp_err_t err;
pl_i2c_init(GPIO_NUM_22, GPIO_NUM_23, 400000);

// now init the aht10 itself
while (1) {    

    float te=al_aht10_get_temperature();

    ESP_LOGI(TAG, "temperature= %.1f", te);
    //ESP_LOGI(TAG, "pressure= %X", p);

    vTaskDelay(5000 / portTICK_PERIOD_MS);
}

}

##############End of the CODE #############

OUTPUT: W (350) user: Hello world! I (350) user: finished init I (350) AHT10 I2C Read: I2C successfully initialized I (410) user: byte= 7 I (410) user: temperature in degree celsius: 7.000000 I (410) user: temperature= 7.0 I (5470) user: byte= 7 I (5470) user: temperature in degree celsius: 7.000000 I (5470) user: temperature= 7.0

The answer shows only 7 .............please help me to solve this problem

Which SDK are you using?

esp-idf

Which version of SDK are you using?

ESP-IDF V4.4

Which build target have you used?

Component causing the issue

aht

Anything in the logs that might be useful for us?

No response

Additional information or context

No response

Confirmation

UncleRus commented 2 years ago

There is no use of esp-idf-lib in your code. It has nothing to do with esp-idf-lib. This repository is not a beginner support forum. To successfully work with aht10, use the appropriate component (aht) from esp-idf-lib.