Create new BH1750Sensor and added in EnvironmentTelemetry

This commit is contained in:
Paolo Leggio 2025-10-16 21:29:27 +02:00 committed by Tom Fifield
parent 18e08cd9f9
commit 8a85cc2dae
3 changed files with 82 additions and 0 deletions

View File

@ -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(<BH1750_WE.h>)
#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(<SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.h>)
addSensor<NAU7802Sensor>(i2cScanner, ScanI2C::DeviceType::NAU7802);
#endif
#if __has_include(<BH1750_WE.h>)
addSensor<BH1750Sensor>(i2cScanner, ScanI2C::DeviceType::BH1750);
#endif
#endif
}

View File

@ -0,0 +1,54 @@
#include "configuration.h"
#if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && __has_include(<BH1750_WE.h>)
#include "../mesh/generated/meshtastic/telemetry.pb.h"
#include "BH1750Sensor.h"
#include "TelemetrySensor.h"
#include <BH1750_WE.h>
#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

View File

@ -0,0 +1,21 @@
#pragma once
#include "configuration.h"
#if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && __has_include(<BH1750_WE.h>)
#include "../mesh/generated/meshtastic/telemetry.pb.h"
#include "TelemetrySensor.h"
#include <BH1750_WE.h>
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