7static const char *
const TAG =
"bh1750.sensor";
9static const uint8_t BH1750_COMMAND_POWER_ON = 0b00000001;
10static const uint8_t BH1750_COMMAND_MT_REG_HI = 0b01000000;
11static const uint8_t BH1750_COMMAND_MT_REG_LO = 0b01100000;
12static const uint8_t BH1750_COMMAND_ONE_TIME_L = 0b00100011;
13static const uint8_t BH1750_COMMAND_ONE_TIME_H = 0b00100000;
14static const uint8_t BH1750_COMMAND_ONE_TIME_H2 = 0b00100001;
41 uint8_t turn_on = BH1750_COMMAND_POWER_ON;
50 uint8_t turn_on = BH1750_COMMAND_POWER_ON;
52 ESP_LOGW(TAG,
"Power on failed");
59 uint8_t mtreg_hi = BH1750_COMMAND_MT_REG_HI | ((mtreg >> 5) & 0b111);
60 uint8_t mtreg_lo = BH1750_COMMAND_MT_REG_LO | ((mtreg >> 0) & 0b11111);
62 ESP_LOGW(TAG,
"Set measurement time failed");
74 cmd = BH1750_COMMAND_ONE_TIME_L;
75 meas_time = 24 * mtreg / 69;
78 cmd = BH1750_COMMAND_ONE_TIME_H;
79 meas_time = 180 * mtreg / 69;
82 cmd = BH1750_COMMAND_ONE_TIME_H2;
83 meas_time = 180 * mtreg / 69;
90 ESP_LOGW(TAG,
"Start measurement failed");
101 ESP_LOGW(TAG,
"Read data failed");
107 float lx = float(raw_value) / 1.2f;
117 LOG_SENSOR(
"",
"BH1750",
this);
118 LOG_I2C_DEVICE(
this);
120 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL_FOR, this->
get_name().c_str());
123 LOG_UPDATE_INTERVAL(
this);
130 if (std::isnan(
val)) {
147 int ideal_mtreg = 50000 * 10 * 69 / (12 * (int)
val);
148 use_mtreg = std::min(254, std::max(31, ideal_mtreg));
150 ESP_LOGV(TAG,
"L result: %f -> Calculated mode=%d, mtreg=%d",
val, (
int) use_mode, use_mtreg);
152 this->
read_lx_(use_mode, use_mtreg, [
this](
float val) {
153 if (std::isnan(
val)) {
158 ESP_LOGD(TAG,
"'%s': Illuminance=%.1flx", this->
get_name().c_str(),
val);
BedjetMode mode
BedJet operating mode.
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
const StringRef & get_name() const
void read_lx_(BH1750Mode mode, uint8_t mtreg, const std::function< void(float)> &f)
void dump_config() override
float get_setup_priority() const override
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
void publish_state(float state)
Publish a new state to the front-end.
uint16_t i2ctohs(uint16_t i2cshort)
@ ERROR_OK
No error found during execution of method.
const float DATA
For components that import data from directly connected sensors like DHT.
Providing packet encoding functions for exchanging data with a remote host.