From 9efa265ff49c6ce32bb32b4e2066bf03b66519c2 Mon Sep 17 00:00:00 2001 From: Paolo Leggio Date: Thu, 16 Oct 2025 14:22:18 +0200 Subject: [PATCH 1/6] regenerate protobuf with bh1750 TelemetrySensorType --- protobufs | 2 +- src/mesh/generated/meshtastic/telemetry.pb.h | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/protobufs b/protobufs index 38638f19f..1f616fff3 160000 --- a/protobufs +++ b/protobufs @@ -1 +1 @@ -Subproject commit 38638f19f84ad886222b484d6bf5a8459aed8c7e +Subproject commit 1f616fff30768e34fc9603d30d6b1a0fe290e097 diff --git a/src/mesh/generated/meshtastic/telemetry.pb.h b/src/mesh/generated/meshtastic/telemetry.pb.h index a8ca96e95..dec89ba15 100644 --- a/src/mesh/generated/meshtastic/telemetry.pb.h +++ b/src/mesh/generated/meshtastic/telemetry.pb.h @@ -101,7 +101,9 @@ typedef enum _meshtastic_TelemetrySensorType { /* SEN5X PM SENSORS */ meshtastic_TelemetrySensorType_SEN5X = 43, /* TSL2561 light sensor */ - meshtastic_TelemetrySensorType_TSL2561 = 44 + meshtastic_TelemetrySensorType_TSL2561 = 44, + /* BH1750 light sensor */ + meshtastic_TelemetrySensorType_BH1750 = 45 } meshtastic_TelemetrySensorType; /* Struct definitions */ @@ -438,8 +440,8 @@ extern "C" { /* Helper constants for enums */ #define _meshtastic_TelemetrySensorType_MIN meshtastic_TelemetrySensorType_SENSOR_UNSET -#define _meshtastic_TelemetrySensorType_MAX meshtastic_TelemetrySensorType_TSL2561 -#define _meshtastic_TelemetrySensorType_ARRAYSIZE ((meshtastic_TelemetrySensorType)(meshtastic_TelemetrySensorType_TSL2561+1)) +#define _meshtastic_TelemetrySensorType_MAX meshtastic_TelemetrySensorType_BH1750 +#define _meshtastic_TelemetrySensorType_ARRAYSIZE ((meshtastic_TelemetrySensorType)(meshtastic_TelemetrySensorType_BH1750+1)) From 81fc8bfa3afaf01c226d90b44e06b220e327b401 Mon Sep 17 00:00:00 2001 From: Paolo Leggio Date: Thu, 16 Oct 2025 14:37:32 +0200 Subject: [PATCH 2/6] Added wollewald/BH1750_WE@^1.1.10 dependecy --- platformio.ini | 2 ++ 1 file changed, 2 insertions(+) diff --git a/platformio.ini b/platformio.ini index 7c63ad7ad..fe2f5e28a 100644 --- a/platformio.ini +++ b/platformio.ini @@ -181,6 +181,8 @@ lib_deps = dfrobot/DFRobot_BMM150@1.0.0 # renovate: datasource=custom.pio depName=Adafruit_TSL2561 packageName=adafruit/library/Adafruit TSL2561 adafruit/Adafruit TSL2561@1.1.2 + # renovate: datasource=custom.pio depName=BH1750_WE packageName=wollewald/BH1750_WE@^1.1.10 + wollewald/BH1750_WE@^1.1.10 ; (not included in native / portduino) [environmental_extra] From 18e08cd9f9132ec61254ae68fa8e7d88c77d6834 Mon Sep 17 00:00:00 2001 From: Paolo Leggio Date: Thu, 16 Oct 2025 15:06:10 +0200 Subject: [PATCH 3/6] Added support for BH1750 during i2C detection --- src/detect/ScanI2C.h | 3 ++- src/detect/ScanI2CTwoWire.cpp | 21 ++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/detect/ScanI2C.h b/src/detect/ScanI2C.h index 2e602338c..e7bdf58c5 100644 --- a/src/detect/ScanI2C.h +++ b/src/detect/ScanI2C.h @@ -82,7 +82,8 @@ class ScanI2C BHI260AP, BMM150, TSL2561, - DRV2605 + DRV2605, + BH1750 } DeviceType; // typedef uint8_t DeviceAddress; diff --git a/src/detect/ScanI2CTwoWire.cpp b/src/detect/ScanI2CTwoWire.cpp index da2a57fee..ef438d27b 100644 --- a/src/detect/ScanI2CTwoWire.cpp +++ b/src/detect/ScanI2CTwoWire.cpp @@ -485,7 +485,26 @@ void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address, uint8_t asize) SCAN_SIMPLE_CASE(LTR390UV_ADDR, LTR390UV, "LTR390UV", (uint8_t)addr.address); SCAN_SIMPLE_CASE(PCT2075_ADDR, PCT2075, "PCT2075", (uint8_t)addr.address); SCAN_SIMPLE_CASE(CST328_ADDR, CST328, "CST328", (uint8_t)addr.address); - SCAN_SIMPLE_CASE(LTR553ALS_ADDR, LTR553ALS, "LTR553ALS", (uint8_t)addr.address); + case LTR553ALS_ADDR: // 0x23 - condiviso con BH1750 + // Test first LTR553ALS (has identifiable registers) + registerValue = getRegisterValue(ScanI2CTwoWire::RegisterLocation(addr, 0x86), 1); // Part ID register + if (registerValue == 0x92) { // LTR553ALS Part ID + type = LTR553ALS; + logFoundDevice("LTR553ALS", (uint8_t)addr.address); + } else { + // Test BH1750 - send power on command + i2cBus->beginTransmission(addr.address); + i2cBus->write(0x01); // Power On command + uint8_t bh1750_error = i2cBus->endTransmission(); + if (bh1750_error == 0) { + type = BH1750; + logFoundDevice("BH1750", (uint8_t)addr.address); + } else { + LOG_INFO("Device found at address 0x%x was not able to be enumerated", (uint8_t)addr.address); + } + } + break; + SCAN_SIMPLE_CASE(BHI260AP_ADDR, BHI260AP, "BHI260AP", (uint8_t)addr.address); SCAN_SIMPLE_CASE(SCD4X_ADDR, SCD4X, "SCD4X", (uint8_t)addr.address); SCAN_SIMPLE_CASE(BMM150_ADDR, BMM150, "BMM150", (uint8_t)addr.address); From 8a85cc2daeb3c1bc22a439d70553882a3d053872 Mon Sep 17 00:00:00 2001 From: Paolo Leggio Date: Thu, 16 Oct 2025 21:29:27 +0200 Subject: [PATCH 4/6] Create new BH1750Sensor and added in EnvironmentTelemetry --- .../Telemetry/EnvironmentTelemetry.cpp | 7 +++ src/modules/Telemetry/Sensor/BH1750Sensor.cpp | 54 +++++++++++++++++++ src/modules/Telemetry/Sensor/BH1750Sensor.h | 21 ++++++++ 3 files changed, 82 insertions(+) create mode 100644 src/modules/Telemetry/Sensor/BH1750Sensor.cpp create mode 100644 src/modules/Telemetry/Sensor/BH1750Sensor.h diff --git a/src/modules/Telemetry/EnvironmentTelemetry.cpp b/src/modules/Telemetry/EnvironmentTelemetry.cpp index 2337af808..a923ab457 100644 --- a/src/modules/Telemetry/EnvironmentTelemetry.cpp +++ b/src/modules/Telemetry/EnvironmentTelemetry.cpp @@ -134,6 +134,10 @@ extern void drawCommonHeader(OLEDDisplay *display, int16_t x, int16_t y, const c #include "Sensor/TSL2561Sensor.h" #endif +#if __has_include() +#include "Sensor/BH1750Sensor.h" +#endif + #define FAILED_STATE_SENSOR_READ_MULTIPLIER 10 #define DISPLAY_RECEIVEID_MEASUREMENTS_ON_SCREEN true @@ -262,6 +266,9 @@ void EnvironmentTelemetryModule::i2cScanFinished(ScanI2C *i2cScanner) #if __has_include() addSensor(i2cScanner, ScanI2C::DeviceType::NAU7802); #endif +#if __has_include() + addSensor(i2cScanner, ScanI2C::DeviceType::BH1750); +#endif #endif } diff --git a/src/modules/Telemetry/Sensor/BH1750Sensor.cpp b/src/modules/Telemetry/Sensor/BH1750Sensor.cpp new file mode 100644 index 000000000..b8790dcd5 --- /dev/null +++ b/src/modules/Telemetry/Sensor/BH1750Sensor.cpp @@ -0,0 +1,54 @@ +#include "configuration.h" + +#if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && __has_include() + +#include "../mesh/generated/meshtastic/telemetry.pb.h" +#include "BH1750Sensor.h" +#include "TelemetrySensor.h" +#include + +#ifndef BH1750_SENSOR_MODE +#define BH1750_SENSOR_MODE BH1750Mode::CHM +#endif + +BH1750Sensor::BH1750Sensor() : TelemetrySensor(meshtastic_TelemetrySensorType_BH1750, "BH1750") {} + +bool BH1750Sensor::initDevice(TwoWire *bus, ScanI2C::FoundDevice *dev) +{ + LOG_INFO("Init sensor: %s with mode %d", sensorName, BH1750_SENSOR_MODE); + + bh1750 = BH1750_WE(bus, dev->address.address); + status = bh1750.init(); + if (!status) { + return status; + } + + bh1750.setMode(BH1750_SENSOR_MODE); + + initI2CSensor(); + return status; +} + +bool BH1750Sensor::getMetrics(meshtastic_Telemetry *measurement) +{ + + /* An OTH and OTH_2 measurement takes ~120 ms. I suggest to wait + 140 ms to be on the safe side. + An OTL measurement takes about 16 ms. I suggest to wait 20 ms + to be on the safe side. */ + if (BH1750_SENSOR_MODE == BH1750Mode::OTH || BH1750_SENSOR_MODE == BH1750Mode::OTH_2) { + bh1750.setMode(BH1750_SENSOR_MODE); + delay(140); // wait for measurement to be completed + } else if (BH1750_SENSOR_MODE == BH1750Mode::OTL) { + bh1750.setMode(BH1750_SENSOR_MODE); + delay(20); + } + + measurement->variant.environment_metrics.has_lux = true; + float lightIntensity = bh1750.getLux(); + + measurement->variant.environment_metrics.lux = lightIntensity; + return true; +} + +#endif diff --git a/src/modules/Telemetry/Sensor/BH1750Sensor.h b/src/modules/Telemetry/Sensor/BH1750Sensor.h new file mode 100644 index 000000000..d9a4ded95 --- /dev/null +++ b/src/modules/Telemetry/Sensor/BH1750Sensor.h @@ -0,0 +1,21 @@ +#pragma once +#include "configuration.h" + +#if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && __has_include() + +#include "../mesh/generated/meshtastic/telemetry.pb.h" +#include "TelemetrySensor.h" +#include + +class BH1750Sensor : public TelemetrySensor +{ + private: + BH1750_WE bh1750; + + public: + BH1750Sensor(); + virtual bool getMetrics(meshtastic_Telemetry *measurement) override; + virtual bool initDevice(TwoWire *bus, ScanI2C::FoundDevice *dev) override; +}; + +#endif From 1f46a7fb1071b8e1c4278ea3721a48a9b67680c8 Mon Sep 17 00:00:00 2001 From: Paolo Leggio Date: Thu, 16 Oct 2025 21:54:46 +0200 Subject: [PATCH 5/6] clean code --- src/detect/ScanI2CTwoWire.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/detect/ScanI2CTwoWire.cpp b/src/detect/ScanI2CTwoWire.cpp index ef438d27b..59d93d74f 100644 --- a/src/detect/ScanI2CTwoWire.cpp +++ b/src/detect/ScanI2CTwoWire.cpp @@ -485,8 +485,7 @@ void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address, uint8_t asize) SCAN_SIMPLE_CASE(LTR390UV_ADDR, LTR390UV, "LTR390UV", (uint8_t)addr.address); SCAN_SIMPLE_CASE(PCT2075_ADDR, PCT2075, "PCT2075", (uint8_t)addr.address); SCAN_SIMPLE_CASE(CST328_ADDR, CST328, "CST328", (uint8_t)addr.address); - case LTR553ALS_ADDR: // 0x23 - condiviso con BH1750 - // Test first LTR553ALS (has identifiable registers) + case LTR553ALS_ADDR: registerValue = getRegisterValue(ScanI2CTwoWire::RegisterLocation(addr, 0x86), 1); // Part ID register if (registerValue == 0x92) { // LTR553ALS Part ID type = LTR553ALS; From af7def4b6416f55860c9c39e7afc4dfdfe29f17e Mon Sep 17 00:00:00 2001 From: Tom Fifield Date: Wed, 22 Oct 2025 08:04:09 +1100 Subject: [PATCH 6/6] Attempt to fix protobuf include --- protobufs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobufs b/protobufs index 1f616fff3..bf149bbdc 160000 --- a/protobufs +++ b/protobufs @@ -1 +1 @@ -Subproject commit 1f616fff30768e34fc9603d30d6b1a0fe290e097 +Subproject commit bf149bbdcce45ba7cd8643db7cb25e5c8815072b