Compare commits

..

No commits in common. "main" and "v1.0.0" have entirely different histories.
main ... v1.0.0

5 changed files with 74 additions and 149 deletions

View File

@ -5,11 +5,6 @@
1. ESP8266 RTOS_SDK v3.4 1. ESP8266 RTOS_SDK v3.4
2. ESP32 ESP-IDF v5.2 2. ESP32 ESP-IDF v5.2
## Features
1. Recorded illuminance values from 0.11 to 121241 lux (depends on operation mode and sensitivity).
2. Support automatically sensitivity adjustment.
## Using ## Using
In an existing project, run the following command to install the component: In an existing project, run the following command to install the component:
@ -37,43 +32,33 @@ Reading the sensor:
void app_main(void) void app_main(void)
{ {
esp_log_level_set("zh_bh1750", ESP_LOG_NONE); esp_log_level_set("zh_bh1750", ESP_LOG_NONE);
#ifdef CONFIG_IDF_TARGET_ESP8266
i2c_config_t i2c_config = { i2c_config_t i2c_config = {
.mode = I2C_MODE_MASTER, .mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_4, // In accordance with used chip.
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_io_num = GPIO_NUM_5, // In accordance with used chip.
.scl_pullup_en = GPIO_PULLUP_ENABLE,
};
i2c_driver_install(I2C_PORT, i2c_config.mode);
i2c_param_config(I2C_PORT, &i2c_config);
#else
i2c_master_bus_config_t i2c_bus_config = {
.clk_source = I2C_CLK_SRC_DEFAULT,
.i2c_port = I2C_PORT,
.scl_io_num = GPIO_NUM_22, // In accordance with used chip.
.sda_io_num = GPIO_NUM_21, // In accordance with used chip. .sda_io_num = GPIO_NUM_21, // In accordance with used chip.
.glitch_ignore_cnt = 7, .sda_pullup_en = GPIO_PULLUP_ENABLE,
.flags.enable_internal_pullup = true, .scl_io_num = GPIO_NUM_22, // In accordance with used chip.
.scl_pullup_en = GPIO_PULLUP_ENABLE,
#ifndef CONFIG_IDF_TARGET_ESP8266
.master.clk_speed = 100000,
#endif
}; };
i2c_master_bus_handle_t i2c_bus_handle;
i2c_new_master_bus(&i2c_bus_config, &i2c_bus_handle);
#endif
zh_bh1750_init_config_t bh1750_init_config = ZH_BH1750_INIT_CONFIG_DEFAULT();
bh1750_init_config.auto_adjust = true; // Just for an example of how to enable auto adjustment the sensor sensitivity.
#ifdef CONFIG_IDF_TARGET_ESP8266 #ifdef CONFIG_IDF_TARGET_ESP8266
bh1750_init_config.i2c_port = I2C_PORT; i2c_param_config(I2C_PORT, &i2c_config);
i2c_driver_install(I2C_PORT, i2c_config.mode);
#else #else
bh1750_init_config.i2c_handle = i2c_bus_handle; i2c_param_config(I2C_PORT, &i2c_config);
i2c_driver_install(I2C_PORT, i2c_config.mode, 0, 0, 0);
#endif #endif
zh_bh1750_init(&bh1750_init_config); zh_bh1750_init_config_t zh_bh1750_init_config = ZH_BH1750_INIT_CONFIG_DEFAULT();
zh_bh1750_adjust(69); // Just for an example of how to change the sensor sensitivity. Dont work if auto adjustment is enabled. zh_bh1750_init_config.i2c_port = I2C_PORT;
zh_bh1750_init(&zh_bh1750_init_config);
zh_bh1750_adjust(69); // Just for an example of how to change the sensor sensitivity.
float lux = 0.0; float lux = 0.0;
for (;;) for (;;)
{ {
zh_bh1750_read(&lux); zh_bh1750_read(&lux);
printf("Lux %0.2f\n", lux); printf("Lux %0.2f\n", lux);
vTaskDelay(5000 / portTICK_PERIOD_MS); vTaskDelay(1000 / portTICK_PERIOD_MS);
} }
} }
``` ```

Binary file not shown.

View File

@ -2,21 +2,22 @@
#include "esp_err.h" #include "esp_err.h"
#include "esp_log.h" #include "esp_log.h"
#ifdef CONFIG_IDF_TARGET_ESP8266
#include "driver/i2c.h" #include "driver/i2c.h"
#else #ifdef CONFIG_IDF_TARGET_ESP8266
#include "driver/i2c_master.h"
#endif
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#endif
/**
* @brief Default values for zh_bh1750_init_config_t structure for initial initialization of the sensor.
*
*/
#define ZH_BH1750_INIT_CONFIG_DEFAULT() \ #define ZH_BH1750_INIT_CONFIG_DEFAULT() \
{ \ { \
.i2c_address = I2C_ADDRESS_LOW, \ .i2c_address = I2C_ADDRESS_LOW, \
.operation_mode = HIGH_RESOLUTION, \ .operation_mode = HIGH_RESOLUTION, \
.work_mode = ONE_TIME, \ .work_mode = ONE_TIME, \
.i2c_port = 0, \ .i2c_port = 0 \
.auto_adjust = false \
} }
#ifdef __cplusplus #ifdef __cplusplus
@ -24,9 +25,16 @@ extern "C"
{ {
#endif #endif
typedef struct // Structure for initial initialization of BH1750 sensor. /**
* @brief Structure for initial initialization of the sensor.
*
* @note Before initialize the sensor recommend initialize zh_bh1750_init_config_t structure with default values.
*
* @code zh_bh1750_init_config_t config = ZH_BH1750_INIT_CONFIG_DEFAULT() @endcode
*/
typedef struct
{ {
enum // Sensor I2C address. enum // Sensor address.
{ {
I2C_ADDRESS_HIGH = 0x5C, // Data pin is connected to VCC. I2C_ADDRESS_HIGH = 0x5C, // Data pin is connected to VCC.
I2C_ADDRESS_LOW = 0x23 // Data pin is not connected / connected to GND. I2C_ADDRESS_LOW = 0x23 // Data pin is not connected / connected to GND.
@ -43,11 +51,6 @@ extern "C"
ONE_TIME // One time measurement. Sensor is power down after measurement. ONE_TIME // One time measurement. Sensor is power down after measurement.
} work_mode; } work_mode;
bool i2c_port; // I2C port. bool i2c_port; // I2C port.
bool auto_adjust; // Flag of automatic sensitivity adjustment.
#ifndef CONFIG_IDF_TARGET_ESP8266
i2c_master_bus_handle_t i2c_handle; // Unique I2C bus handle.
#endif
} zh_bh1750_init_config_t; } zh_bh1750_init_config_t;
/** /**
@ -64,9 +67,8 @@ extern "C"
* @return * @return
* - ESP_OK if initialization was success * - ESP_OK if initialization was success
* - ESP_ERR_INVALID_ARG if parameter error * - ESP_ERR_INVALID_ARG if parameter error
* - ESP_ERR_NOT_FOUND if sensor not connected or not responded
*/ */
esp_err_t zh_bh1750_init(const zh_bh1750_init_config_t *config); esp_err_t zh_bh1750_init(zh_bh1750_init_config_t *config);
/** /**
* @brief Read BH1750 sensor. * @brief Read BH1750 sensor.
@ -76,16 +78,16 @@ extern "C"
* @return * @return
* - ESP_OK if read was success * - ESP_OK if read was success
* - ESP_ERR_INVALID_ARG if parameter error * - ESP_ERR_INVALID_ARG if parameter error
* - ESP_ERR_NOT_FOUND if sensor is not initialized * - ESP_ERR_NOT_FOUND if BH1750 is not initialized
* - ESP_ERR_INVALID_RESPONSE if I2C driver error * - ESP_FAIL if sending command error or slave has not ACK the transfer
* - ESP_ERR_INVALID_STATE if I2C driver not installed or not in master mode
* - ESP_ERR_TIMEOUT if operation timeout because the bus is busy
*/ */
esp_err_t zh_bh1750_read(float *data); esp_err_t zh_bh1750_read(float *data);
/** /**
* @brief Adjust BH1750 sensor sensitivity. * @brief Adjust BH1750 sensor sensitivity.
* *
* @attention Can only be used if the automatic adjustment is not switched on.
*
* @param[in] value Value of sensitivity. * @param[in] value Value of sensitivity.
* *
* @note Range value from 31 to 254. 69 by default. Can be changed continuously during program work. * @note Range value from 31 to 254. 69 by default. Can be changed continuously during program work.
@ -93,10 +95,12 @@ extern "C"
* @return * @return
* - ESP_OK if adjust was success * - ESP_OK if adjust was success
* - ESP_ERR_INVALID_ARG if parameter error * - ESP_ERR_INVALID_ARG if parameter error
* - ESP_ERR_NOT_FOUND if sensor is not initialized or auto adjust is enabled * - ESP_ERR_NOT_FOUND if BH1750 is not initialized
* - ESP_ERR_INVALID_RESPONSE if I2C driver error * - ESP_FAIL if sending command error or slave has not ACK the transfer
* - ESP_ERR_INVALID_STATE if I2C driver not installed or not in master mode
* - ESP_ERR_TIMEOUT if operation timeout because the bus is busy
*/ */
esp_err_t zh_bh1750_adjust(const uint8_t value); esp_err_t zh_bh1750_adjust(uint8_t value);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -1 +1 @@
1.0.5 1.0.0

View File

@ -1,7 +1,7 @@
#include "zh_bh1750.h" #include "zh_bh1750.h"
#define LOW_RESOLUTION_MEASUREMENT_TIME 24 // L-Resolution mode measurement time. #define LOW_RESOLUTION_MEASUREMENT_TIME 24 // L-Resolution mode measurement time max value.
#define HIGH_RESOLUTION_MEASUREMENT_TIME 180 // H-Resolution mode measurement time. #define HIGH_RESOLUTION_MEASUREMENT_TIME 180 // H-Resolution mode measurement time max value.
#define SENSITIVITY_DEFAULT 69 #define SENSITIVITY_DEFAULT 69
@ -21,15 +21,10 @@ static bool _is_first_start = false;
static uint8_t _time = 0; static uint8_t _time = 0;
static uint8_t _sensivity = SENSITIVITY_DEFAULT; static uint8_t _sensivity = SENSITIVITY_DEFAULT;
static bool _is_initialized = false; static bool _is_initialized = false;
#ifndef CONFIG_IDF_TARGET_ESP8266
static i2c_master_dev_handle_t _bh1750_handle = {0};
#endif
esp_err_t _adjust(const uint8_t value);
static const char *TAG = "zh_bh1750"; static const char *TAG = "zh_bh1750";
esp_err_t zh_bh1750_init(const zh_bh1750_init_config_t *config) esp_err_t zh_bh1750_init(zh_bh1750_init_config_t *config)
{ {
ESP_LOGI(TAG, "BH1750 initialization begin."); ESP_LOGI(TAG, "BH1750 initialization begin.");
if (config == NULL) if (config == NULL)
@ -38,19 +33,6 @@ esp_err_t zh_bh1750_init(const zh_bh1750_init_config_t *config)
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
} }
_init_config = *config; _init_config = *config;
#ifndef CONFIG_IDF_TARGET_ESP8266
i2c_device_config_t bh1750_config = {
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
.device_address = _init_config.i2c_address,
.scl_speed_hz = 100000,
};
i2c_master_bus_add_device(_init_config.i2c_handle, &bh1750_config, &_bh1750_handle);
if (i2c_master_probe(_init_config.i2c_handle, _init_config.i2c_address, 1000 / portTICK_PERIOD_MS) != ESP_OK)
{
ESP_LOGE(TAG, "BH1750 initialization fail. Sensor not connected or not responded.");
return ESP_ERR_NOT_FOUND;
}
#endif
switch (_init_config.operation_mode) switch (_init_config.operation_mode)
{ {
case LOW_RESOLUTION:; case LOW_RESOLUTION:;
@ -86,9 +68,9 @@ esp_err_t zh_bh1750_read(float *data)
ESP_LOGE(TAG, "BH1750 read fail. BH1750 not initialized."); ESP_LOGE(TAG, "BH1750 read fail. BH1750 not initialized.");
return ESP_ERR_NOT_FOUND; return ESP_ERR_NOT_FOUND;
} }
REPEATE:; esp_err_t esp_err = ESP_OK;
esp_err_t err = ESP_OK; uint8_t sensor_data_high = 0;
uint8_t sensor_data[2] = {0}; uint8_t sensor_data_low = 0;
if (_init_config.work_mode == CONTINUOUSLY) if (_init_config.work_mode == CONTINUOUSLY)
{ {
if (_is_first_start == true) if (_is_first_start == true)
@ -100,86 +82,46 @@ REPEATE:;
_is_first_start = true; _is_first_start = true;
} }
} }
#ifdef CONFIG_IDF_TARGET_ESP8266
i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create(); i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create();
i2c_master_start(i2c_cmd_handle); i2c_master_start(i2c_cmd_handle);
i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_WRITE, true); i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_WRITE, true);
i2c_master_write_byte(i2c_cmd_handle, _command, true); i2c_master_write_byte(i2c_cmd_handle, _command, true);
i2c_master_stop(i2c_cmd_handle); i2c_master_stop(i2c_cmd_handle);
err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS); esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(i2c_cmd_handle); i2c_cmd_link_delete(i2c_cmd_handle);
#else if (esp_err != ESP_OK)
err = i2c_master_transmit(_bh1750_handle, &_command, sizeof(_command), 1000 / portTICK_PERIOD_MS);
#endif
if (err != ESP_OK)
{ {
ESP_LOGE(TAG, "BH1750 read fail. I2C driver error at line %d.", __LINE__); ESP_LOGE(TAG, "BH1750 read fail. I2C driver error.");
return ESP_ERR_INVALID_RESPONSE; return esp_err;
} }
vTaskDelay(_time / portTICK_PERIOD_MS); vTaskDelay(((_sensivity / SENSITIVITY_DEFAULT) * _time) / portTICK_PERIOD_MS);
READ: READ:
#ifdef CONFIG_IDF_TARGET_ESP8266
i2c_cmd_handle = i2c_cmd_link_create(); i2c_cmd_handle = i2c_cmd_link_create();
i2c_master_start(i2c_cmd_handle); i2c_master_start(i2c_cmd_handle);
i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_READ, true); i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_READ, true);
i2c_master_read_byte(i2c_cmd_handle, &sensor_data[0], I2C_MASTER_ACK); i2c_master_read_byte(i2c_cmd_handle, &sensor_data_high, I2C_MASTER_ACK);
i2c_master_read_byte(i2c_cmd_handle, &sensor_data[1], I2C_MASTER_NACK); i2c_master_read_byte(i2c_cmd_handle, &sensor_data_low, I2C_MASTER_NACK);
i2c_master_stop(i2c_cmd_handle); i2c_master_stop(i2c_cmd_handle);
err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS); esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(i2c_cmd_handle); i2c_cmd_link_delete(i2c_cmd_handle);
#else if (esp_err != ESP_OK)
err = i2c_master_receive(_bh1750_handle, sensor_data, sizeof(sensor_data), 1000 / portTICK_PERIOD_MS);
#endif
if (err != ESP_OK)
{ {
ESP_LOGE(TAG, "BH1750 read fail. I2C driver error at line %d.", __LINE__); ESP_LOGE(TAG, "BH1750 read fail. I2C driver error.");
return ESP_ERR_INVALID_RESPONSE; return esp_err;
}
uint32_t raw_data = sensor_data[0] << 8 | sensor_data[1];
if (raw_data == 65535 || raw_data == 0)
{
ESP_LOGW(TAG, "BH1750 read error. Sensitivity adjustment required. Current sensivity level %d.", _sensivity);
}
if (_init_config.auto_adjust == true)
{
if (raw_data == 65535 && _sensivity > 31)
{
if (_adjust(_sensivity - 1) == ESP_OK)
{
goto REPEATE;
}
}
if (raw_data == 0 && _sensivity < 254)
{
if (_adjust(_sensivity + 1) == ESP_OK)
{
goto REPEATE;
}
}
} }
if (_init_config.operation_mode == HIGH_RESOLUTION_2) if (_init_config.operation_mode == HIGH_RESOLUTION_2)
{ {
*data = raw_data * (1 / 1.2 * ((float)SENSITIVITY_DEFAULT / _sensivity) / 2); *data = (sensor_data_high << 8 | sensor_data_low) * (1 / 1.2 * (69.0 / _sensivity) / 2);
} }
else else
{ {
*data = raw_data * (1 / 1.2 * ((float)SENSITIVITY_DEFAULT / _sensivity)); *data = (sensor_data_high << 8 | sensor_data_low) * (1 / 1.2 * (69.0 / _sensivity));
} }
ESP_LOGI(TAG, "BH1750 read success."); ESP_LOGI(TAG, "BH1750 read success.");
return ESP_OK; return ESP_OK;
} }
esp_err_t zh_bh1750_adjust(const uint8_t value) esp_err_t zh_bh1750_adjust(uint8_t value)
{
if (_init_config.auto_adjust == true)
{
ESP_LOGE(TAG, "BH1750 adjust fail. Auto adjust is enabled.");
return ESP_ERR_NOT_FOUND;
}
return _adjust(value);
}
esp_err_t _adjust(const uint8_t value)
{ {
ESP_LOGI(TAG, "BH1750 adjust begin."); ESP_LOGI(TAG, "BH1750 adjust begin.");
if ((value < 31 || value > 254)) if ((value < 31 || value > 254))
@ -192,42 +134,36 @@ esp_err_t _adjust(const uint8_t value)
ESP_LOGE(TAG, "BH1750 read fail. BH1750 not initialized."); ESP_LOGE(TAG, "BH1750 read fail. BH1750 not initialized.");
return ESP_ERR_NOT_FOUND; return ESP_ERR_NOT_FOUND;
} }
esp_err_t err = ESP_OK; esp_err_t esp_err = ESP_OK;
uint8_t sensitivity_data[2] = {value >> 5 | MEASUREMENT_TIME_HIGH_BIT, (value & 0b00011111) | MEASUREMENT_TIME_LOW_BIT}; _sensivity = value;
#ifdef CONFIG_IDF_TARGET_ESP8266
i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create(); i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create();
i2c_master_start(i2c_cmd_handle); i2c_master_start(i2c_cmd_handle);
i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_WRITE, true); i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_WRITE, true);
i2c_master_write_byte(i2c_cmd_handle, sensitivity_data[0], true); i2c_master_write_byte(i2c_cmd_handle, value >> 5 | MEASUREMENT_TIME_HIGH_BIT, true);
i2c_master_stop(i2c_cmd_handle); i2c_master_stop(i2c_cmd_handle);
err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS); esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(i2c_cmd_handle); i2c_cmd_link_delete(i2c_cmd_handle);
if (esp_err != ESP_OK) if (esp_err != ESP_OK)
{ {
ESP_LOGE(TAG, "BH1750 adjust fail. I2C driver error at line %d.", __LINE__); ESP_LOGE(TAG, "BH1750 adjust fail. I2C driver error.");
return ESP_ERR_INVALID_RESPONSE; return esp_err;
} }
i2c_cmd_handle = i2c_cmd_link_create(); i2c_cmd_handle = i2c_cmd_link_create();
i2c_master_start(i2c_cmd_handle); i2c_master_start(i2c_cmd_handle);
i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_WRITE, true); i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_WRITE, true);
i2c_master_write_byte(i2c_cmd_handle, sensitivity_data[1], true); i2c_master_write_byte(i2c_cmd_handle, (value & 0b00011111) | MEASUREMENT_TIME_LOW_BIT, true);
i2c_master_stop(i2c_cmd_handle); i2c_master_stop(i2c_cmd_handle);
err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS); esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(i2c_cmd_handle); i2c_cmd_link_delete(i2c_cmd_handle);
#else if (esp_err != ESP_OK)
err = i2c_master_transmit(_bh1750_handle, &sensitivity_data[0], 1, 1000 / portTICK_PERIOD_MS);
err = i2c_master_transmit(_bh1750_handle, &sensitivity_data[1], 1, 1000 / portTICK_PERIOD_MS);
#endif
if (err != ESP_OK)
{ {
ESP_LOGE(TAG, "BH1750 adjust fail. I2C driver error at line %d.", __LINE__); ESP_LOGE(TAG, "BH1750 adjust fail. I2C driver error.");
return ESP_ERR_INVALID_RESPONSE; return esp_err;
} }
if (_init_config.work_mode == CONTINUOUSLY) if (_init_config.work_mode == CONTINUOUSLY)
{ {
_is_first_start = false; _is_first_start = false;
} }
_sensivity = value;
ESP_LOGI(TAG, "BH1750 adjust success."); ESP_LOGI(TAG, "BH1750 adjust success.");
return ESP_OK; return ESP_OK;
} }