Skip to content

Commit c39e83f

Browse files
committed
Add TSL2561 sensor
1 parent 95200e8 commit c39e83f

File tree

9 files changed

+99
-6
lines changed

9 files changed

+99
-6
lines changed

platformio.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ lib_deps =
177177
adafruit/Adafruit [email protected]
178178
# renovate: datasource=custom.pio depName=DFRobot_BMM150 packageName=dfrobot/library/DFRobot_BMM150
179179
180+
# renovate: datasource=custom.pio depName=Adafruit_TSL2561 packageName=adafruit/library/Adafruit TSL2561
181+
adafruit/Adafruit TSL2561
180182

181183
; (not included in native / portduino)
182184
[environmental_extra]

protobufs

src/detect/ScanI2C.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ class ScanI2C
7979
BQ27220,
8080
LTR553ALS,
8181
BHI260AP,
82-
BMM150
82+
BMM150,
83+
TSL2561
8384
} DeviceType;
8485

8586
// typedef uint8_t DeviceAddress;

src/detect/ScanI2CTwoWire.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,17 @@ void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address, uint8_t asize)
461461
SCAN_SIMPLE_CASE(LSM6DS3_ADDR, LSM6DS3, "LSM6DS3", (uint8_t)addr.address);
462462
SCAN_SIMPLE_CASE(TCA9555_ADDR, TCA9555, "TCA9555", (uint8_t)addr.address);
463463
SCAN_SIMPLE_CASE(VEML7700_ADDR, VEML7700, "VEML7700", (uint8_t)addr.address);
464-
SCAN_SIMPLE_CASE(TSL25911_ADDR, TSL2591, "TSL2591", (uint8_t)addr.address);
464+
case TSL25911_ADDR:
465+
registerValue = getRegisterValue(ScanI2CTwoWire::RegisterLocation(addr, 0x12), 1);
466+
if (registerValue == 0x50) {
467+
type = TSL2591;
468+
logFoundDevice("TSL25911", (uint8_t)addr.address);
469+
} else {
470+
type = TSL2561;
471+
logFoundDevice("TSL2561", (uint8_t)addr.address);
472+
}
473+
break;
474+
465475
SCAN_SIMPLE_CASE(MLX90632_ADDR, MLX90632, "MLX90632", (uint8_t)addr.address);
466476
SCAN_SIMPLE_CASE(NAU7802_ADDR, NAU7802, "NAU7802", (uint8_t)addr.address);
467477
SCAN_SIMPLE_CASE(MAX1704X_ADDR, MAX17048, "MAX17048", (uint8_t)addr.address);

src/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,7 @@ void setup()
706706
scannerToSensorsMap(i2cScanner, ScanI2C::DeviceType::RAK12035, meshtastic_TelemetrySensorType_RAK12035);
707707
scannerToSensorsMap(i2cScanner, ScanI2C::DeviceType::PCT2075, meshtastic_TelemetrySensorType_PCT2075);
708708
scannerToSensorsMap(i2cScanner, ScanI2C::DeviceType::SCD4X, meshtastic_TelemetrySensorType_SCD4X);
709+
scannerToSensorsMap(i2cScanner, ScanI2C::DeviceType::TSL2561, meshtastic_TelemetrySensorType_TSL2561);
709710

710711
i2cScanner.reset();
711712
#endif

src/mesh/generated/meshtastic/telemetry.pb.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ typedef enum _meshtastic_TelemetrySensorType {
9999
/* Sensirion SFA30 Formaldehyde sensor */
100100
meshtastic_TelemetrySensorType_SFA30 = 42,
101101
/* SEN5X PM SENSORS */
102-
meshtastic_TelemetrySensorType_SEN5X = 43
102+
meshtastic_TelemetrySensorType_SEN5X = 43,
103+
/* TSL2561 light sensor */
104+
meshtastic_TelemetrySensorType_TSL2561 = 44
103105
} meshtastic_TelemetrySensorType;
104106

105107
/* Struct definitions */
@@ -434,8 +436,8 @@ extern "C" {
434436

435437
/* Helper constants for enums */
436438
#define _meshtastic_TelemetrySensorType_MIN meshtastic_TelemetrySensorType_SENSOR_UNSET
437-
#define _meshtastic_TelemetrySensorType_MAX meshtastic_TelemetrySensorType_SEN5X
438-
#define _meshtastic_TelemetrySensorType_ARRAYSIZE ((meshtastic_TelemetrySensorType)(meshtastic_TelemetrySensorType_SEN5X+1))
439+
#define _meshtastic_TelemetrySensorType_MAX meshtastic_TelemetrySensorType_TSL2561
440+
#define _meshtastic_TelemetrySensorType_ARRAYSIZE ((meshtastic_TelemetrySensorType)(meshtastic_TelemetrySensorType_TSL2561+1))
439441

440442

441443

src/modules/Telemetry/EnvironmentTelemetry.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,13 @@ T1000xSensor t1000xSensor;
198198
IndicatorSensor indicatorSensor;
199199
#endif
200200

201+
#if __has_include(<Adafruit_TSL2561_U.h>)
202+
#include "Sensor/TSL2561Sensor.h"
203+
TSL2561Sensor tsl2561Sensor;
204+
#else
205+
NullSensor tsl2561Sensor;
206+
#endif
207+
201208
#define FAILED_STATE_SENSOR_READ_MULTIPLIER 10
202209
#define DISPLAY_RECEIVEID_MEASUREMENTS_ON_SCREEN true
203210

@@ -296,6 +303,8 @@ int32_t EnvironmentTelemetryModule::runOnce()
296303
result = max17048Sensor.runOnce();
297304
if (cgRadSens.hasSensor())
298305
result = cgRadSens.runOnce();
306+
if (tsl2561Sensor.hasSensor())
307+
result = tsl2561Sensor.runOnce();
299308
if (pct2075Sensor.hasSensor())
300309
result = pct2075Sensor.runOnce();
301310
// this only works on the wismesh hub with the solar option. This is not an I2C sensor, so we don't need the
@@ -642,6 +651,10 @@ bool EnvironmentTelemetryModule::getEnvironmentTelemetry(meshtastic_Telemetry *m
642651
valid = valid && nau7802Sensor.getMetrics(m);
643652
hasSensor = true;
644653
}
654+
if (tsl2561Sensor.hasSensor()) {
655+
valid = valid && tsl2561Sensor.getMetrics(m);
656+
hasSensor = true;
657+
}
645658
if (aht10Sensor.hasSensor()) {
646659
if (!bmp280Sensor.hasSensor() && !bmp3xxSensor.hasSensor()) {
647660
valid = valid && aht10Sensor.getMetrics(m);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "configuration.h"
2+
3+
#if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && __has_include(<Adafruit_TSL2561_U.h>)
4+
5+
#include "../mesh/generated/meshtastic/telemetry.pb.h"
6+
#include "TSL2561Sensor.h"
7+
#include "TelemetrySensor.h"
8+
#include <Adafruit_TSL2561_U.h>
9+
#include <typeinfo>
10+
11+
TSL2561Sensor::TSL2561Sensor() : TelemetrySensor(meshtastic_TelemetrySensorType_TSL2561, "TSL2561") {}
12+
13+
int32_t TSL2561Sensor::runOnce()
14+
{
15+
LOG_INFO("Init sensor: %s", sensorName);
16+
if (!hasSensor()) {
17+
return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS;
18+
}
19+
20+
status = tsl.begin(nodeTelemetrySensorsMap[sensorType].second);
21+
22+
return initI2CSensor();
23+
}
24+
25+
void TSL2561Sensor::setup()
26+
{
27+
tsl.setGain(TSL2561_GAIN_1X);
28+
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS);
29+
}
30+
31+
bool TSL2561Sensor::getMetrics(meshtastic_Telemetry *measurement)
32+
{
33+
measurement->variant.environment_metrics.has_lux = true;
34+
sensors_event_t event;
35+
tsl.getEvent(&event);
36+
measurement->variant.environment_metrics.lux = event.light;
37+
LOG_INFO("Lux: %f", measurement->variant.environment_metrics.lux);
38+
39+
return true;
40+
}
41+
42+
#endif
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "configuration.h"
2+
3+
#if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && __has_include(<Adafruit_TSL2561_U.h>)
4+
5+
#include "../mesh/generated/meshtastic/telemetry.pb.h"
6+
#include "TelemetrySensor.h"
7+
#include <Adafruit_TSL2561_U.h>
8+
9+
class TSL2561Sensor : public TelemetrySensor
10+
{
11+
private:
12+
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_LOW, 12345);
13+
14+
protected:
15+
virtual void setup() override;
16+
17+
public:
18+
TSL2561Sensor();
19+
virtual int32_t runOnce() override;
20+
virtual bool getMetrics(meshtastic_Telemetry *measurement) override;
21+
};
22+
#endif

0 commit comments

Comments
 (0)