WIP
This commit is contained in:
@ -5,6 +5,11 @@
|
|||||||
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:
|
||||||
@ -66,7 +71,7 @@ void app_main(void)
|
|||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
zh_bh1750_read(&lux);
|
zh_bh1750_read(&lux);
|
||||||
printf("Lux %d\n", (uint32_t)lux);
|
printf("Lux %0.2f\n", lux);
|
||||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,8 @@
|
|||||||
.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
|
||||||
@ -52,7 +53,8 @@ extern "C"
|
|||||||
CONTINUOUSLY, // Continuously measurement.
|
CONTINUOUSLY, // Continuously measurement.
|
||||||
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
|
#ifndef CONFIG_IDF_TARGET_ESP8266
|
||||||
i2c_master_bus_handle_t i2c_handle; // Unique I2C bus handle.
|
i2c_master_bus_handle_t i2c_handle; // Unique I2C bus handle.
|
||||||
#endif
|
#endif
|
||||||
|
31
zh_bh1750.c
31
zh_bh1750.c
@ -84,6 +84,7 @@ 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 esp_err = ESP_OK;
|
||||||
uint8_t sensor_data[2] = {0};
|
uint8_t sensor_data[2] = {0};
|
||||||
if (_init_config.work_mode == CONTINUOUSLY)
|
if (_init_config.work_mode == CONTINUOUSLY)
|
||||||
@ -132,13 +133,39 @@ READ:
|
|||||||
ESP_LOGE(TAG, "BH1750 read fail. I2C driver error.");
|
ESP_LOGE(TAG, "BH1750 read fail. I2C driver error.");
|
||||||
return esp_err;
|
return esp_err;
|
||||||
}
|
}
|
||||||
|
uint32_t raw_data = sensor_data[0] << 8 | sensor_data[1];
|
||||||
|
if (raw_data == 65536 || raw_data == 0)
|
||||||
|
{
|
||||||
|
ESP_LOGW(TAG, "BH1750 read error. Sensitivity adjustment required. Current sensivity level %d", _sensivity);
|
||||||
|
}
|
||||||
|
if (_init_config.auto_adjust == true)
|
||||||
|
{
|
||||||
|
printf("RAW data %ld\n", raw_data);
|
||||||
|
printf("Sensivity %d\n", _sensivity);
|
||||||
|
if (raw_data == 65536 && _sensivity > 31)
|
||||||
|
{
|
||||||
|
if (zh_bh1750_adjust(_sensivity - 1) == ESP_OK)
|
||||||
|
{
|
||||||
|
--_sensivity;
|
||||||
|
goto REPEATE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (raw_data == 0 && _sensivity < 254)
|
||||||
|
{
|
||||||
|
if (zh_bh1750_adjust(_sensivity + 1) == ESP_OK)
|
||||||
|
{
|
||||||
|
++_sensivity;
|
||||||
|
goto REPEATE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if (_init_config.operation_mode == HIGH_RESOLUTION_2)
|
if (_init_config.operation_mode == HIGH_RESOLUTION_2)
|
||||||
{
|
{
|
||||||
*data = (sensor_data[0] << 8 | sensor_data[1]) * (1 / 1.2 * (69.0 / _sensivity) / 2);
|
*data = raw_data * (1 / 1.2 * (69.0 / _sensivity) / 2);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
*data = (sensor_data[0] << 8 | sensor_data[1]) * (1 / 1.2 * (69.0 / _sensivity));
|
*data = raw_data * (1 / 1.2 * (69.0 / _sensivity));
|
||||||
}
|
}
|
||||||
ESP_LOGI(TAG, "BH1750 read success.");
|
ESP_LOGI(TAG, "BH1750 read success.");
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
|
Reference in New Issue
Block a user