Skip to content

Trouble implementing the driver in my project #13

@Huruvarshan

Description

@Huruvarshan

Sali Zäme!

My project includes a SEN66 and an ESP32-C6 for which I use ESP-IDF (v5.2.2) for programming. The measured data gets sent to a MQTT-server. As of now, I am trying to get test code for the SEN66 running.

How do I correctly get the I2C-bus initialized with the driver? It's my first time using drivers like this.

  • I have followed the instructions on the main page with including the drivers, "sensirion_i2c_esp32_config.h" and adjusting "senirion_i2c_hal.c".
  • Including "sensirion_i2c_esp32_config.h" gives me an error that I don't have a driver called <i2cdev.h>.
  • When passing in the config for the bus into sensirion_i2c_config_esp32(), the compiler gives me an error saying "implicit declaration of function 'sensirion_i2c_config_esp32' [-Werror=implicit-function-declaration]"

Here is the code I have written:

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "driver/ledc.h"
#include "esp_log.h"
#include "driver/i2c_master.h"
#include "sen66_i2c.h"
#include "sensirion_i2c_hal.h"
#include "sensirion_common.h"
#include "sensirion_i2c_esp32_config.h"


#define TOUCH_PAD 0         //Touch pad GPIO number
#define LED1_IO 14          //LED1 GPIO number

#define I2C_SCL_IO 7        //I2C SCL GPIO number
#define I2C_SDA_IO 6        //I2C SDA GPIO number
#define I2C_FREQ_HZ 100000  //I2C frequency

#define LED_FREQ_HZ 5000    //LEDC frequency

void app_main(void)
{
    //Variables 
    bool toggle = 0; 
    bool levelTouchPad = 0; 

    /*---------------------------------------------------------- Touch Pad Initialization ----------------------------------------------------------*/
    char* TAG_TOUCH_PAD = "TOUCH_PAD";                  //Tag for logging

    gpio_set_direction(TOUCH_PAD, GPIO_MODE_INPUT);     //Set touch pad as input
    gpio_set_pull_mode(TOUCH_PAD, GPIO_PULLUP_ONLY);    //Enable pull-up resistor
    ESP_LOGI(TAG_TOUCH_PAD, "Touch pad initialized.");  //Log touch pad initialization
    

    /*---------------------------------------------------------- LEDC Initialization ----------------------------------------------------------*/
    char* TAG_LEDC = "LEDC";                    //Tag for logging

    //LEDC Timer Configuration
    ledc_timer_config_t timer = {               //Create LEDC timer configuration
    .speed_mode = LEDC_LOW_SPEED_MODE,          //Set LEDC speed mode to low speed mode
    .duty_resolution = LEDC_TIMER_10_BIT,       //Set duty resolution to 10 bits
    .timer_num = LEDC_TIMER_0,                  //Select timer 0
    .freq_hz = LED_FREQ_HZ,                     //Set frequency
    .clk_cfg = LEDC_AUTO_CLK};                  //Set clock source to auto
    ledc_timer_config(&timer);                  //Configure LEDC timer

    //LEDC Channel Configuration   
    ledc_channel_config_t channel = {           //Create LEDC channel configuration
        .gpio_num = LED1_IO,                    //Set LED GPIO number
        .speed_mode = LEDC_LOW_SPEED_MODE,      //Set LEDC speed mode to low speed mode
        .channel = LEDC_CHANNEL_0,              //Select LEDC channel 0
        .timer_sel = LEDC_TIMER_0,              //Select timer 0
        .duty = 0,                              //Set initial duty cycle to 0
        .hpoint = 0};                           //Set hpoint to 0
        
    ledc_channel_config(&channel);              //Configure LEDC channel
    ledc_fade_func_install(0);                  //Install LEDC fade function
    ESP_LOGI(TAG_LEDC, "LEDC initialized.");    //Log LEDC initialization
    


    /*---------------------------------------------------------- I2C Configuration ----------------------------------------------------------*/ 
    char* TAG_I2C = "I2C_CONFIG";                                           //Tag for logging
    i2c_master_bus_handle_t i2c_bus_handle = NULL;                          //Create I2C bus handle
    
    i2c_master_bus_config_t i2c_bus_config = {                              //Create I2C bus configuration
        .i2c_port = I2C_NUM_0,                                              //Select I2C port 0
        .sda_io_num = I2C_SDA_IO,                                           //Set SDA GPIO number
        .scl_io_num = I2C_SCL_IO,                                           //Set SCL GPIO number
        .clk_source = I2C_CLK_SRC_DEFAULT,                                  //Set clock source to default
        .glitch_ignore_cnt = 7,                                             //Set glitch ignore count to 7
        .flags.enable_internal_pullup = true,};                             //Enable internal pull-up resistors

    ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_config, &i2c_bus_handle));  //Create new I2C master bus

    sensirion_i2c_config_esp32(&i2c_bus_config);                            //Configure Sensirion I2C for ESP32
    sensirion_i2c_hal_init();                                               //Initialize Sensirion I2C HAL
 
    ESP_LOGI(TAG_I2C, "I2C bus initialized.");                              //Log I2C bus initialization
    
    /*esp32_i2c_config i2c_config = {
        .freq = 100000,
        .addr = SEN66_I2C_ADDR_6B,
        .port = I2C_NUM_0,
        .sda = 6,
        .scl = 7,
        .sda_pullup = true,
        .scl_pullup = true,
    };
    sensirion_i2c_config_esp32(&i2c_config);
    sensirion_i2c_hal_init();

    ESP_LOGI(TAG_I2C, "I2C bus initialized."); */


    /*---------------------------------------------------------- SEN66 Initialization ----------------------------------------------------------*/ 
    char* TAG_SEN66 = "SEN66";                              //Tag for logging
    int16_t error = 0;                                      //Variable to store error codes
    int8_t serial_number[32] = {0};                         //Array to store serial number

    //initialize I2C for SEN66    
    i2c_master_dev_handle_t i2c_dev_handle_SEN66 = NULL;    //Create I2C device handle for SEN66
    i2c_device_config_t i2c_dev_config_SEN66 = {            //Create I2C device configuration for SEN66
        .dev_addr_length = I2C_ADDR_BIT_LEN_7,              //Set address length to 7 bits
        .device_address = SEN66_I2C_ADDR_6B,                //Set device address to 0x6b
        .scl_speed_hz = I2C_FREQ_HZ,};                      //Set clock speed

    ESP_ERROR_CHECK(i2c_master_bus_add_device(i2c_bus_handle, &i2c_dev_config_SEN66, &i2c_dev_handle_SEN66));   //Add SEN66 device to I2C bus


    // Reset SEN66
    error = sen66_device_reset();                                   //Reset SEN66 device
    if (error != NO_ERROR) {                                        //Check for errors
        ESP_LOGE(TAG_SEN66, "Error resetting SEN66: %d", error);    //Log error, if one occurred
    }
    vTaskDelay(pdMS_TO_TICKS(1200));                                // Wait for reset

    // Get SEN66 serial number
    error = sen66_get_serial_number(serial_number, 32);                                 //Get serial number
    if (error != NO_ERROR) {                                                            //Check for errors
        ESP_LOGE(TAG_SEN66, "Error getting serial number of the SEN66: %d", error);     //Log error, if one occurred
    } else {
        ESP_LOGI(TAG_SEN66, "Serial number of the SEN66: %s", serial_number);           //Log serial number
    }

    // Start continuous measurements
    error = sen66_start_continuous_measurement();                                               //Start continuous measurement
    if (error != NO_ERROR) {                                                                    //Check for errors
        ESP_LOGE(TAG_SEN66, "Error starting continuous measurement of the SEN66: %d", error);   //Log error, if one occurred
    } else {
        ESP_LOGI(TAG_SEN66, "Continuous measurement started.");                                 //Log successful start
    }

    ESP_LOGI(TAG_SEN66, "SEN66 initialized"); //Log SEN66 initialization


    /*---------------------------------------------------------- While-Loop ----------------------------------------------------------*/
    while (1) {
        int levelTouchPad = gpio_get_level(TOUCH_PAD);
        if (levelTouchPad) {
            ESP_LOGI(TAG_TOUCH_PAD, "Touch pad is touched");
            toggle = !toggle;
        }

        if (toggle)
        {
            ledc_set_fade_time_and_start(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 1023, 500, LEDC_FADE_WAIT_DONE);
        } else {
            ledc_set_fade_time_and_start(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 10, 500, LEDC_FADE_WAIT_DONE);
        }
        

        vTaskDelay(pdMS_TO_TICKS(250)); //250 ms delay, remove later
    }
}

Out of desperation I tried to generate a solution with AI and it stated to insert this:

esp32_i2c_config i2c_config = {
        .freq = 100000,
        .addr = SEN66_I2C_ADDR_6B,
        .port = I2C_NUM_0,
        .sda = 6,
        .scl = 7,
        .sda_pullup = true,
        .scl_pullup = true,
    };
    sensirion_i2c_config_esp32(&i2c_config);
    sensirion_i2c_hal_init();

    ESP_LOGI(TAG_I2C, "I2C bus initialized."); 

Which I did, after commenting out the configuration and initialization of the driver ESPRESSIF-provided. But with no luck either.

If you need more information, I will try to send it as quickly as I can.
Any help is appreciated!

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions