-1

Below given the code for reading AHT10 sensor using ESP-IDF.The output to the code is shown below. It is not reading the temperature instead it prints only 7 always. Please help me to solve the problem

########################### 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        

############## BEGIN of CODE ############################
#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 <string.h>
#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





// 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);

    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);
    }
}
273K
  • 29,503
  • 10
  • 41
  • 64
  • 4
    Looking through your chain of calls, it seems you're essentially casting the return value of `i2c_master_cmd_begin` to a `float` value and then using that as your temperature. Looking at the [documentation](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/i2c.html), the return value from this command has nothing to do with any actual data delivered via I2C. It is just a status. It looks like you already read something into that value when you called `i2c_master_read_byte` but in the subsequent call you overwrite that value. Take the time to read your code. – paddy Jun 01 '22 at 04:26

1 Answers1

0

The i2c_master_cmd_begin has a return value of esp_err_t and not the value red by i2c.

Make something like this: esp_err_t returnValue = i2c_master_cmd_begin etc. And investigate the return value. Try and if you’re stuck, just ask again! (The return value will give you some clue about what is happening)

RemyHx
  • 325
  • 3
  • 9