2019-05-22 12:21:41 +01:00
|
|
|
/*
|
2021-03-16 15:55:34 +00:00
|
|
|
xsns_45_vl53l0x.ino - VL53L0X time of flight multiple sensors support for Tasmota
|
2019-05-22 12:21:41 +01:00
|
|
|
|
2021-03-16 15:55:34 +00:00
|
|
|
Copyright (C) 2021 Theo Arends, Gerhard Mutz and Adrian Scillato
|
2019-05-22 12:21:41 +01:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_I2C
|
|
|
|
#ifdef USE_VL53L0X
|
2020-06-08 14:09:51 +01:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* VL53L0x time of flight sensor
|
|
|
|
*
|
|
|
|
* I2C Addres: 0x29
|
2021-03-16 15:55:34 +00:00
|
|
|
*********************************************************************************************
|
2022-03-11 22:12:57 +00:00
|
|
|
*
|
2021-03-16 15:55:34 +00:00
|
|
|
* Note: When using multiple VL53L0X, it is required to also wire the XSHUT pin of all those sensors
|
|
|
|
* in order to let Tasmota change by software the I2C address of those and give them an unique address
|
|
|
|
* for operation. The sensor don't save its address, so this procedure of changing its address is needed
|
|
|
|
* to be performed every restart. The Addresses used for this are 120 (0x78) to 127 (0x7F). In the I2c
|
|
|
|
* Standard (https://i2cdevices.org/addresses) those addresses are used by the PCA9685, so take into
|
|
|
|
* account they won't work together.
|
2022-03-11 22:12:57 +00:00
|
|
|
*
|
|
|
|
* The default value of VL53LXX_MAX_SENSORS is set in the file tasmota.h
|
2021-03-16 15:55:34 +00:00
|
|
|
* Changing that is backwards incompatible - Max supported devices by this driver are 8
|
2022-03-11 22:12:57 +00:00
|
|
|
*
|
2021-03-16 15:55:34 +00:00
|
|
|
**********************************************************************************************
|
|
|
|
*
|
|
|
|
* How to install this sensor: https://www.st.com/resource/en/datasheet/vl53l0x.pdf
|
2022-03-11 22:12:57 +00:00
|
|
|
*
|
2021-03-16 15:55:34 +00:00
|
|
|
* If you are going to use long I2C wires read this:
|
|
|
|
* https://hackaday.com/2017/02/08/taking-the-leap-off-board-an-introduction-to-i2c-over-long-wires/
|
2022-03-11 22:12:57 +00:00
|
|
|
*
|
2020-06-08 14:09:51 +01:00
|
|
|
\*********************************************************************************************/
|
2019-05-22 12:21:41 +01:00
|
|
|
|
2021-01-22 15:48:41 +00:00
|
|
|
#define XSNS_45 45
|
|
|
|
#define XI2C_31 31 // See I2CDEVICES.md
|
|
|
|
|
2021-03-16 15:55:34 +00:00
|
|
|
// Uncomment this line to use long range mode. This
|
|
|
|
// increases the sensitivity of the sensor and extends its
|
|
|
|
// potential range, but increases the likelihood of getting
|
|
|
|
// an inaccurate reading because of reflections from objects
|
|
|
|
// other than the intended target. It works best in dark
|
|
|
|
// conditions.
|
|
|
|
|
|
|
|
//#define VL53L0X_LONG_RANGE
|
|
|
|
|
|
|
|
// Uncomment ONE of these two lines to get
|
|
|
|
// - higher speed at the cost of lower accuracy OR
|
|
|
|
// - higher accuracy at the cost of lower speed
|
|
|
|
|
|
|
|
//#define VL53L0X_HIGH_SPEED
|
|
|
|
//#define VL53L0X_HIGH_ACCURACY
|
|
|
|
|
2021-01-22 15:48:41 +00:00
|
|
|
#define USE_VL_MEDIAN
|
|
|
|
#define USE_VL_MEDIAN_SIZE 5 // Odd number of samples median detection
|
2019-11-03 16:54:39 +00:00
|
|
|
|
2019-05-22 12:21:41 +01:00
|
|
|
#include <Wire.h>
|
|
|
|
#include "VL53L0X.h"
|
|
|
|
|
2022-03-11 22:12:57 +00:00
|
|
|
#define VL53L0X_ADDRESS 0x29
|
|
|
|
#ifndef VL53L0X_XSHUT_ADDRESS
|
|
|
|
#define VL53L0X_XSHUT_ADDRESS 0x78
|
|
|
|
#endif
|
|
|
|
|
|
|
|
VL53L0X VL53L0X_device[VL53LXX_MAX_SENSORS];
|
2020-06-08 14:09:51 +01:00
|
|
|
struct {
|
|
|
|
uint16_t distance;
|
|
|
|
uint16_t distance_prev;
|
|
|
|
uint16_t buffer[5];
|
|
|
|
uint8_t ready = 0;
|
|
|
|
uint8_t index;
|
2022-03-11 22:12:57 +00:00
|
|
|
} Vl53l0x_data[VL53LXX_MAX_SENSORS];
|
2021-03-16 15:55:34 +00:00
|
|
|
|
2022-03-11 22:12:57 +00:00
|
|
|
bool VL53L0X_xshut = false;
|
2021-03-16 15:55:34 +00:00
|
|
|
bool VL53L0X_detected = false;
|
2019-05-22 12:21:41 +01:00
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
2020-06-08 14:09:51 +01:00
|
|
|
void Vl53l0Detect(void) {
|
2019-05-22 12:21:41 +01:00
|
|
|
|
2022-03-11 22:12:57 +00:00
|
|
|
for (uint32_t i = 0; i < VL53LXX_MAX_SENSORS; i++) {
|
|
|
|
if (PinUsed(GPIO_VL53LXX_XSHUT1, i)) {
|
|
|
|
pinMode(Pin(GPIO_VL53LXX_XSHUT1, i), OUTPUT);
|
|
|
|
digitalWrite(Pin(GPIO_VL53LXX_XSHUT1, i), i==0 ? 1 : 0);
|
|
|
|
VL53L0X_xshut = true;
|
2021-03-16 15:55:34 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-22 12:21:41 +01:00
|
|
|
|
2022-03-11 22:12:57 +00:00
|
|
|
for (uint32_t i = 0; i < VL53LXX_MAX_SENSORS; i++) {
|
|
|
|
if (PinUsed(GPIO_VL53LXX_XSHUT1, i) || (!VL53L0X_xshut)) {
|
|
|
|
if (VL53L0X_xshut) { pinMode(Pin(GPIO_VL53LXX_XSHUT1, i), INPUT); delay(1); }
|
|
|
|
if (!I2cSetDevice(VL53L0X_ADDRESS) && !I2cSetDevice((uint8_t)(VL53L0X_XSHUT_ADDRESS+i))) { return; } // Detection for unconfigured OR configured sensor
|
|
|
|
if (VL53L0X_device[i].init()) {
|
|
|
|
if (VL53L0X_xshut) { VL53L0X_device[i].setAddress((uint8_t)(VL53L0X_XSHUT_ADDRESS+i)); }
|
|
|
|
uint8_t addr = VL53L0X_device[i].getAddress();
|
|
|
|
if (VL53L0X_xshut) {
|
2021-03-16 15:55:34 +00:00
|
|
|
I2cSetActive(addr);
|
|
|
|
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_I2C D_SENSOR " VL53L0X %d " D_SENSOR_DETECTED " - " D_NEW_ADDRESS " 0x%02X"), i+1, addr);
|
|
|
|
} else {
|
|
|
|
I2cSetActiveFound(addr, "VL53L0X");
|
|
|
|
}
|
2022-03-11 22:12:57 +00:00
|
|
|
VL53L0X_device[i].setTimeout(500);
|
2019-05-22 12:21:41 +01:00
|
|
|
|
2021-03-16 15:55:34 +00:00
|
|
|
#if defined VL53L0X_LONG_RANGE
|
|
|
|
// lower the return signal rate limit (default is 0.25 MCPS)
|
2022-03-11 22:12:57 +00:00
|
|
|
VL53L0X_device[i].setSignalRateLimit(0.1);
|
2021-03-16 15:55:34 +00:00
|
|
|
// increase laser pulse periods (defaults are 14 and 10 PCLKs)
|
2022-03-11 22:12:57 +00:00
|
|
|
VL53L0X_device[i].setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18);
|
|
|
|
VL53L0X_device[i].setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, 14);
|
2021-03-16 15:55:34 +00:00
|
|
|
#endif
|
|
|
|
#if defined VL53L0X_HIGH_SPEED
|
|
|
|
// reduce timing budget to 20 ms (default is about 33 ms)
|
2022-03-11 22:12:57 +00:00
|
|
|
VL53L0X_device[i].setMeasurementTimingBudget(20000);
|
2021-03-16 15:55:34 +00:00
|
|
|
#elif defined VL53L0X_HIGH_ACCURACY
|
|
|
|
// increase timing budget to 200 ms
|
2022-03-11 22:12:57 +00:00
|
|
|
VL53L0X_device[i].setMeasurementTimingBudget(200000);
|
2021-03-16 15:55:34 +00:00
|
|
|
#endif
|
|
|
|
// Start continuous back-to-back mode (take readings as
|
|
|
|
// fast as possible). To use continuous timed mode
|
|
|
|
// instead, provide a desired inter-measurement period in
|
|
|
|
// ms (e.g. sensor.startContinuous(100)).
|
2022-03-11 22:12:57 +00:00
|
|
|
VL53L0X_device[i].startContinuous();
|
2019-05-22 12:21:41 +01:00
|
|
|
|
2022-03-11 22:12:57 +00:00
|
|
|
Vl53l0x_data[i].ready = 1;
|
|
|
|
Vl53l0x_data[i].index = 0;
|
2021-03-16 15:55:34 +00:00
|
|
|
VL53L0X_detected = true;
|
2022-03-11 22:12:57 +00:00
|
|
|
if (!VL53L0X_xshut) { break; }
|
2021-03-16 15:55:34 +00:00
|
|
|
} else {
|
|
|
|
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_I2C D_SENSOR " VL53L0X %d - " D_FAILED_TO_START), i+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-22 12:21:41 +01:00
|
|
|
}
|
|
|
|
|
2020-06-08 14:09:51 +01:00
|
|
|
void Vl53l0Every_250MSecond(void) {
|
2022-03-11 22:12:57 +00:00
|
|
|
for (uint32_t i = 0; i < VL53LXX_MAX_SENSORS; i++) {
|
|
|
|
if (PinUsed(GPIO_VL53LXX_XSHUT1, i) || (!VL53L0X_xshut)) {
|
|
|
|
uint16_t dist = VL53L0X_device[i].readRangeContinuousMillimeters();
|
2021-03-16 15:55:34 +00:00
|
|
|
if ((0 == dist) || (dist > 2200)) {
|
|
|
|
dist = 9999;
|
|
|
|
}
|
2019-05-22 12:21:41 +01:00
|
|
|
|
|
|
|
#ifdef USE_VL_MEDIAN
|
2021-03-16 15:55:34 +00:00
|
|
|
// store in ring buffer
|
2022-03-11 22:12:57 +00:00
|
|
|
Vl53l0x_data[i].buffer[Vl53l0x_data[i].index] = dist;
|
|
|
|
Vl53l0x_data[i].index++;
|
|
|
|
if (Vl53l0x_data[i].index >= USE_VL_MEDIAN_SIZE) {
|
|
|
|
Vl53l0x_data[i].index = 0;
|
2021-03-16 15:55:34 +00:00
|
|
|
}
|
2019-05-22 12:21:41 +01:00
|
|
|
|
2021-03-16 15:55:34 +00:00
|
|
|
// sort list and take median
|
|
|
|
uint16_t tbuff[USE_VL_MEDIAN_SIZE];
|
2022-03-11 22:12:57 +00:00
|
|
|
memmove(tbuff, Vl53l0x_data[i].buffer, sizeof(tbuff));
|
2021-03-16 15:55:34 +00:00
|
|
|
uint16_t tmp;
|
|
|
|
uint8_t flag;
|
|
|
|
for (uint32_t ocnt = 0; ocnt < USE_VL_MEDIAN_SIZE; ocnt++) {
|
|
|
|
flag = 0;
|
|
|
|
for (uint32_t count = 0; count < USE_VL_MEDIAN_SIZE -1; count++) {
|
|
|
|
if (tbuff[count] > tbuff[count +1]) {
|
|
|
|
tmp = tbuff[count];
|
|
|
|
tbuff[count] = tbuff[count +1];
|
|
|
|
tbuff[count +1] = tmp;
|
|
|
|
flag = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!flag) { break; }
|
|
|
|
}
|
2022-03-11 22:12:57 +00:00
|
|
|
Vl53l0x_data[i].distance = tbuff[(USE_VL_MEDIAN_SIZE -1) / 2];
|
2019-05-22 12:21:41 +01:00
|
|
|
#else
|
2022-03-11 22:12:57 +00:00
|
|
|
Vl53l0x_data[i].distance = dist;
|
2019-05-22 12:21:41 +01:00
|
|
|
#endif
|
2021-03-16 15:55:34 +00:00
|
|
|
}
|
2022-03-11 22:12:57 +00:00
|
|
|
if (!VL53L0X_xshut) { break; }
|
2021-03-16 15:55:34 +00:00
|
|
|
}
|
2019-05-22 12:21:41 +01:00
|
|
|
}
|
|
|
|
|
2020-06-08 14:09:51 +01:00
|
|
|
#ifdef USE_DOMOTICZ
|
|
|
|
void Vl53l0Every_Second(void) {
|
2022-03-11 22:12:57 +00:00
|
|
|
if (abs(Vl53l0x_data[0].distance - Vl53l0x_data[0].distance_prev) > 8) {
|
|
|
|
Vl53l0x_data[0].distance_prev = Vl53l0x_data[0].distance;
|
2022-11-08 15:59:46 +00:00
|
|
|
float distance = (float)Vl53l0x_data[0].distance / 10; // cm
|
|
|
|
DomoticzFloatSensor(DZ_ILLUMINANCE, distance);
|
2020-06-08 14:09:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
|
|
|
|
void Vl53l0Show(boolean json) {
|
2022-03-11 22:12:57 +00:00
|
|
|
for (uint32_t i = 0; i < VL53LXX_MAX_SENSORS; i++) {
|
2022-11-08 15:59:46 +00:00
|
|
|
char types[12] = "VL53L0X";
|
|
|
|
if (VL53L0X_xshut) {
|
|
|
|
snprintf_P(types, sizeof(types), PSTR("VL53L0X%c%d"), IndexSeparator(), i +1);
|
|
|
|
}
|
2022-03-11 22:12:57 +00:00
|
|
|
if (PinUsed(GPIO_VL53LXX_XSHUT1, i) || (!VL53L0X_xshut)) {
|
2022-11-08 15:59:46 +00:00
|
|
|
float distance = (Vl53l0x_data[i].distance == 9999) ? NAN : (float)Vl53l0x_data[i].distance / 10; // cm
|
|
|
|
if (json) {
|
|
|
|
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_DISTANCE "\":%1_f}"), types, &distance);
|
2019-05-22 12:21:41 +01:00
|
|
|
#ifdef USE_WEBSERVER
|
2022-11-08 15:59:46 +00:00
|
|
|
} else {
|
|
|
|
WSContentSend_PD(HTTP_SNS_F_DISTANCE_CM, types, &distance);
|
2019-05-22 12:21:41 +01:00
|
|
|
#endif
|
2022-11-08 15:59:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (VL53L0X_device[i].timeoutOccurred()) {
|
|
|
|
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_I2C "Timeout waiting for %s"), types);
|
2021-03-16 15:55:34 +00:00
|
|
|
}
|
2022-03-11 22:12:57 +00:00
|
|
|
if (!VL53L0X_xshut) { break; }
|
2019-05-22 12:21:41 +01:00
|
|
|
}
|
2021-03-16 15:55:34 +00:00
|
|
|
#ifdef USE_DOMOTICZ
|
2022-11-08 15:59:46 +00:00
|
|
|
if (json && (0 == TasmotaGlobal.tele_period)){
|
|
|
|
float distance = (float)Vl53l0x_data[0].distance / 10; // cm
|
|
|
|
DomoticzFloatSensor(DZ_ILLUMINANCE, distance);
|
|
|
|
}
|
2021-03-16 15:55:34 +00:00
|
|
|
#endif // USE_DOMOTICZ
|
2019-05-22 12:21:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2022-11-11 09:44:56 +00:00
|
|
|
bool Xsns45(uint32_t function) {
|
2019-11-04 09:38:05 +00:00
|
|
|
if (!I2cEnabled(XI2C_31)) { return false; }
|
2019-11-03 16:54:39 +00:00
|
|
|
|
2019-05-22 12:21:41 +01:00
|
|
|
bool result = false;
|
|
|
|
|
2019-11-11 17:36:13 +00:00
|
|
|
if (FUNC_INIT == function) {
|
|
|
|
Vl53l0Detect();
|
|
|
|
}
|
2021-03-16 15:55:34 +00:00
|
|
|
else if (VL53L0X_detected) {
|
2019-11-11 17:36:13 +00:00
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_250_MSECOND:
|
|
|
|
Vl53l0Every_250MSecond();
|
|
|
|
break;
|
2020-06-08 14:09:51 +01:00
|
|
|
#ifdef USE_DOMOTICZ
|
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
Vl53l0Every_Second();
|
|
|
|
break;
|
|
|
|
#endif // USE_DOMOTICZ
|
2019-11-11 17:36:13 +00:00
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
Vl53l0Show(1);
|
|
|
|
break;
|
2019-05-22 12:21:41 +01:00
|
|
|
#ifdef USE_WEBSERVER
|
2019-11-11 17:36:13 +00:00
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
Vl53l0Show(0);
|
|
|
|
break;
|
2019-05-22 12:21:41 +01:00
|
|
|
#endif // USE_WEBSERVER
|
2019-11-11 17:36:13 +00:00
|
|
|
}
|
2019-05-22 12:21:41 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_VL53L0X
|
|
|
|
#endif // USE_I2C
|