Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 2522
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 1, 20222022-06-01T04:32:23+00:00 2022-06-01T04:32:23+00:00

c++ – I am running a code to read temperature and humidity sensor (AHT10), but the output is 7 always

  • 0

[ad_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);
    }
}

[ad_2]

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 2 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 4 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 3 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.