Skip to content

Commit 7f78a62

Browse files
pa0lin082fifieldt
andauthored
Add support for Bh1750 Light Sensor (#8376)
* regenerate protobuf with bh1750 TelemetrySensorType * Added wollewald/BH1750_WE@^1.1.10 dependecy * Added support for BH1750 during i2C detection * Create new BH1750Sensor and added in EnvironmentTelemetry * clean code * Attempt to fix protobuf include --------- Co-authored-by: Tom Fifield <tom@tomfifield.net>
1 parent 17324fa commit 7f78a62

File tree

6 files changed

+105
-2
lines changed

6 files changed

+105
-2
lines changed

platformio.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ lib_deps =
181181
dfrobot/DFRobot_BMM150@1.0.0
182182
# renovate: datasource=custom.pio depName=Adafruit_TSL2561 packageName=adafruit/library/Adafruit TSL2561
183183
adafruit/Adafruit TSL2561@1.1.2
184+
# renovate: datasource=custom.pio depName=BH1750_WE packageName=wollewald/BH1750_WE@^1.1.10
185+
wollewald/BH1750_WE@^1.1.10
184186

185187
; (not included in native / portduino)
186188
[environmental_extra]

src/detect/ScanI2C.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ class ScanI2C
8282
BHI260AP,
8383
BMM150,
8484
TSL2561,
85-
DRV2605
85+
DRV2605,
86+
BH1750
8687
} DeviceType;
8788

8889
// typedef uint8_t DeviceAddress;

src/detect/ScanI2CTwoWire.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,25 @@ void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address, uint8_t asize)
485485
SCAN_SIMPLE_CASE(LTR390UV_ADDR, LTR390UV, "LTR390UV", (uint8_t)addr.address);
486486
SCAN_SIMPLE_CASE(PCT2075_ADDR, PCT2075, "PCT2075", (uint8_t)addr.address);
487487
SCAN_SIMPLE_CASE(CST328_ADDR, CST328, "CST328", (uint8_t)addr.address);
488-
SCAN_SIMPLE_CASE(LTR553ALS_ADDR, LTR553ALS, "LTR553ALS", (uint8_t)addr.address);
488+
case LTR553ALS_ADDR:
489+
registerValue = getRegisterValue(ScanI2CTwoWire::RegisterLocation(addr, 0x86), 1); // Part ID register
490+
if (registerValue == 0x92) { // LTR553ALS Part ID
491+
type = LTR553ALS;
492+
logFoundDevice("LTR553ALS", (uint8_t)addr.address);
493+
} else {
494+
// Test BH1750 - send power on command
495+
i2cBus->beginTransmission(addr.address);
496+
i2cBus->write(0x01); // Power On command
497+
uint8_t bh1750_error = i2cBus->endTransmission();
498+
if (bh1750_error == 0) {
499+
type = BH1750;
500+
logFoundDevice("BH1750", (uint8_t)addr.address);
501+
} else {
502+
LOG_INFO("Device found at address 0x%x was not able to be enumerated", (uint8_t)addr.address);
503+
}
504+
}
505+
break;
506+
489507
SCAN_SIMPLE_CASE(BHI260AP_ADDR, BHI260AP, "BHI260AP", (uint8_t)addr.address);
490508
SCAN_SIMPLE_CASE(SCD4X_ADDR, SCD4X, "SCD4X", (uint8_t)addr.address);
491509
SCAN_SIMPLE_CASE(BMM150_ADDR, BMM150, "BMM150", (uint8_t)addr.address);

src/modules/Telemetry/EnvironmentTelemetry.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ extern void drawCommonHeader(OLEDDisplay *display, int16_t x, int16_t y, const c
134134
#include "Sensor/TSL2561Sensor.h"
135135
#endif
136136

137+
#if __has_include(<BH1750_WE.h>)
138+
#include "Sensor/BH1750Sensor.h"
139+
#endif
140+
137141
#define FAILED_STATE_SENSOR_READ_MULTIPLIER 10
138142
#define DISPLAY_RECEIVEID_MEASUREMENTS_ON_SCREEN true
139143

@@ -262,6 +266,9 @@ void EnvironmentTelemetryModule::i2cScanFinished(ScanI2C *i2cScanner)
262266
#if __has_include(<SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.h>)
263267
addSensor<NAU7802Sensor>(i2cScanner, ScanI2C::DeviceType::NAU7802);
264268
#endif
269+
#if __has_include(<BH1750_WE.h>)
270+
addSensor<BH1750Sensor>(i2cScanner, ScanI2C::DeviceType::BH1750);
271+
#endif
265272

266273
#endif
267274
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "configuration.h"
2+
3+
#if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && __has_include(<BH1750_WE.h>)
4+
5+
#include "../mesh/generated/meshtastic/telemetry.pb.h"
6+
#include "BH1750Sensor.h"
7+
#include "TelemetrySensor.h"
8+
#include <BH1750_WE.h>
9+
10+
#ifndef BH1750_SENSOR_MODE
11+
#define BH1750_SENSOR_MODE BH1750Mode::CHM
12+
#endif
13+
14+
BH1750Sensor::BH1750Sensor() : TelemetrySensor(meshtastic_TelemetrySensorType_BH1750, "BH1750") {}
15+
16+
bool BH1750Sensor::initDevice(TwoWire *bus, ScanI2C::FoundDevice *dev)
17+
{
18+
LOG_INFO("Init sensor: %s with mode %d", sensorName, BH1750_SENSOR_MODE);
19+
20+
bh1750 = BH1750_WE(bus, dev->address.address);
21+
status = bh1750.init();
22+
if (!status) {
23+
return status;
24+
}
25+
26+
bh1750.setMode(BH1750_SENSOR_MODE);
27+
28+
initI2CSensor();
29+
return status;
30+
}
31+
32+
bool BH1750Sensor::getMetrics(meshtastic_Telemetry *measurement)
33+
{
34+
35+
/* An OTH and OTH_2 measurement takes ~120 ms. I suggest to wait
36+
140 ms to be on the safe side.
37+
An OTL measurement takes about 16 ms. I suggest to wait 20 ms
38+
to be on the safe side. */
39+
if (BH1750_SENSOR_MODE == BH1750Mode::OTH || BH1750_SENSOR_MODE == BH1750Mode::OTH_2) {
40+
bh1750.setMode(BH1750_SENSOR_MODE);
41+
delay(140); // wait for measurement to be completed
42+
} else if (BH1750_SENSOR_MODE == BH1750Mode::OTL) {
43+
bh1750.setMode(BH1750_SENSOR_MODE);
44+
delay(20);
45+
}
46+
47+
measurement->variant.environment_metrics.has_lux = true;
48+
float lightIntensity = bh1750.getLux();
49+
50+
measurement->variant.environment_metrics.lux = lightIntensity;
51+
return true;
52+
}
53+
54+
#endif
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
#include "configuration.h"
3+
4+
#if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && __has_include(<BH1750_WE.h>)
5+
6+
#include "../mesh/generated/meshtastic/telemetry.pb.h"
7+
#include "TelemetrySensor.h"
8+
#include <BH1750_WE.h>
9+
10+
class BH1750Sensor : public TelemetrySensor
11+
{
12+
private:
13+
BH1750_WE bh1750;
14+
15+
public:
16+
BH1750Sensor();
17+
virtual bool getMetrics(meshtastic_Telemetry *measurement) override;
18+
virtual bool initDevice(TwoWire *bus, ScanI2C::FoundDevice *dev) override;
19+
};
20+
21+
#endif

0 commit comments

Comments
 (0)