Skip to content

Commit 9e1e5a7

Browse files
committed
Add VL53L0 distance sensor.
Added new Environment Telemetry class VL53L0XSensor. Tested with a Heltec V3.
1 parent 58496e5 commit 9e1e5a7

File tree

8 files changed

+90
-9
lines changed

8 files changed

+90
-9
lines changed

platformio.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ lib_deps =
199199
adafruit/Adafruit SHT31 Library@2.2.2
200200
# renovate: datasource=custom.pio depName=Adafruit VEML7700 packageName=adafruit/library/Adafruit VEML7700 Library
201201
adafruit/Adafruit VEML7700 Library@2.1.6
202+
# renovate: datasource=custom.pio depName=Adafruit VL53L0X packageName=adafruit/library/Adafruit VL53L0X
203+
adafruit/Adafruit_VL53L0X@^1.2.4
202204
# renovate: datasource=custom.pio depName=Adafruit SHT4x packageName=adafruit/library/Adafruit SHT4x Library
203205
adafruit/Adafruit SHT4x Library@1.0.5
204206
# renovate: datasource=custom.pio depName=SparkFun Qwiic Scale NAU7802 packageName=sparkfun/library/SparkFun Qwiic Scale NAU7802 Arduino Library

src/configuration.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
225225
#define AHT10_ADDR 0x38
226226
#define RCWL9620_ADDR 0x57
227227
#define VEML7700_ADDR 0x10
228+
#define VL530X_ADDR 0x29
228229
#define TSL25911_ADDR 0x29
229230
#define OPT3001_ADDR 0x45
230231
#define OPT3001_ADDR_ALT 0x44

src/detect/ScanI2C.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class ScanI2C
5050
TCA9535,
5151
TCA9555,
5252
VEML7700,
53+
VL53L0X,
5354
RCWL9620,
5455
NCP5623,
5556
LP5562,

src/detect/ScanI2CTwoWire.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -566,12 +566,21 @@ void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address, uint8_t asize)
566566
break;
567567
case TSL25911_ADDR:
568568
registerValue = getRegisterValue(ScanI2CTwoWire::RegisterLocation(addr, 0xA0 | 0x12), 1);
569-
if (registerValue == 0x50) {
570-
type = TSL2591;
571-
logFoundDevice("TSL25911", (uint8_t)addr.address);
572-
} else {
573-
type = TSL2561;
574-
logFoundDevice("TSL2561", (uint8_t)addr.address);
569+
switch (registerValue) {
570+
case 0x00 ... 0x04:
571+
type = VL53L0X;
572+
logFoundDevice("VL53L0X", (uint8_t)addr.address);
573+
break;
574+
case 0x50:
575+
type = TSL2591;
576+
logFoundDevice("TSL25911", (uint8_t)addr.address);
577+
break;
578+
case 0x51 ... 0x57:
579+
type = TSL2561;
580+
logFoundDevice("TSL2561", (uint8_t)addr.address);
581+
break;
582+
default:
583+
break;
575584
}
576585
break;
577586

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ typedef enum _meshtastic_TelemetrySensorType {
111111
/* Sensirion STC31 CO2 sensor */
112112
meshtastic_TelemetrySensorType_STC31 = 48,
113113
/* SCD30 CO2, humidity, temperature sensor */
114-
meshtastic_TelemetrySensorType_SCD30 = 49
114+
meshtastic_TelemetrySensorType_SCD30 = 49,
115+
/* VL53L0X distance sensor */
116+
meshtastic_TelemetrySensorType_VL53L0X = 50
115117
} meshtastic_TelemetrySensorType;
116118

117119
/* Struct definitions */
@@ -489,8 +491,8 @@ extern "C" {
489491

490492
/* Helper constants for enums */
491493
#define _meshtastic_TelemetrySensorType_MIN meshtastic_TelemetrySensorType_SENSOR_UNSET
492-
#define _meshtastic_TelemetrySensorType_MAX meshtastic_TelemetrySensorType_SCD30
493-
#define _meshtastic_TelemetrySensorType_ARRAYSIZE ((meshtastic_TelemetrySensorType)(meshtastic_TelemetrySensorType_SCD30+1))
494+
#define _meshtastic_TelemetrySensorType_MAX meshtastic_TelemetrySensorType_VL53L0X
495+
#define _meshtastic_TelemetrySensorType_ARRAYSIZE ((meshtastic_TelemetrySensorType)(meshtastic_TelemetrySensorType_VL53L0X+1))
494496

495497

496498

src/modules/Telemetry/EnvironmentTelemetry.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ extern void drawCommonHeader(OLEDDisplay *display, int16_t x, int16_t y, const c
8585
#include "Sensor/VEML7700Sensor.h"
8686
#endif
8787

88+
#if __has_include(<Adafruit_VL53L0X.h>)
89+
#include "Sensor/VL53L0XSensor.h"
90+
#endif
91+
8892
#if __has_include(<Adafruit_TSL2591.h>)
8993
#include "Sensor/TSL2591Sensor.h"
9094
#endif
@@ -214,6 +218,9 @@ void EnvironmentTelemetryModule::i2cScanFinished(ScanI2C *i2cScanner)
214218
#if __has_include(<Adafruit_VEML7700.h>)
215219
addSensor<VEML7700Sensor>(i2cScanner, ScanI2C::DeviceType::VEML7700);
216220
#endif
221+
#if __has_include(<Adafruit_VL53L0X.h>)
222+
addSensor<VL53L0XSensor>(i2cScanner, ScanI2C::DeviceType::VL53L0X);
223+
#endif
217224
#if __has_include(<Adafruit_TSL2591.h>)
218225
addSensor<TSL2591Sensor>(i2cScanner, ScanI2C::DeviceType::TSL2591);
219226
#endif
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "configuration.h"
2+
#include <memory>
3+
4+
#if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && __has_include(<Adafruit_VL53L0X.h>)
5+
6+
#include "../mesh/generated/meshtastic/telemetry.pb.h"
7+
#include "TelemetrySensor.h"
8+
#include "VL53L0XSensor.h"
9+
10+
#include <Adafruit_VL53L0X.h>
11+
#include <typeinfo>
12+
13+
VL53L0XSensor::VL53L0XSensor() : TelemetrySensor(meshtastic_TelemetrySensorType_VL53L0X, "VL53L0X") {}
14+
15+
bool VL53L0XSensor::initDevice(TwoWire *bus, ScanI2C::FoundDevice *dev)
16+
{
17+
LOG_INFO("Init sensor: %s", sensorName);
18+
status = vl53l0x.begin(VL53L0X_I2C_ADDR, false, bus);
19+
if (!status) {
20+
return status;
21+
}
22+
23+
initI2CSensor();
24+
return status;
25+
}
26+
27+
bool VL53L0XSensor::getMetrics(meshtastic_Telemetry *measurement)
28+
{
29+
VL53L0X_RangingMeasurementData_t measure;
30+
vl53l0x.rangingTest(&measure, false);
31+
32+
measurement->variant.environment_metrics.has_distance = true;
33+
measurement->variant.environment_metrics.distance = measure.RangeMilliMeter;
34+
35+
LOG_INFO("distance %f", measurement->variant.environment_metrics.distance);
36+
37+
return true;
38+
}
39+
#endif
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "configuration.h"
2+
3+
#if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && __has_include(<Adafruit_VL53L0X.h>)
4+
5+
#include "../mesh/generated/meshtastic/telemetry.pb.h"
6+
#include "TelemetrySensor.h"
7+
#include <Adafruit_VL53L0X.h>
8+
9+
class VL53L0XSensor : public TelemetrySensor
10+
{
11+
private:
12+
13+
Adafruit_VL53L0X vl53l0x;
14+
15+
public:
16+
VL53L0XSensor();
17+
virtual bool getMetrics(meshtastic_Telemetry *measurement) override;
18+
virtual bool initDevice(TwoWire *bus, ScanI2C::FoundDevice *dev) override;
19+
};
20+
#endif

0 commit comments

Comments
 (0)