PlantCtrl/esp32test/Esp32DeepSleepTest/src/main.cpp

84 lines
1.9 KiB
C++
Raw Normal View History

2020-09-12 16:47:58 +02:00
#include <Arduino.h>
2023-03-22 20:02:23 +01:00
#include "driver/pcnt.h"
#include <VL53L0X.h>
2020-09-12 16:47:58 +02:00
2023-03-22 20:06:35 +01:00
#define SENSOR_TANK_SDA GPIO_NUM_16 /**< GPIO 16 - echo feedback of water sensor */
#define SENSOR_TANK_SCL GPIO_NUM_17 /**< GPIO 17 - trigger for water sensor */
2022-04-02 22:10:31 +02:00
#define OUTPUT_SENSOR 14
#define SENSOR_PLANT 17
2020-10-09 20:45:47 +02:00
2023-03-22 20:02:23 +01:00
VL53L0X tankSensor;
bool distanceReady = false;
2020-10-09 19:29:28 +02:00
2023-03-22 20:50:25 +01:00
void initializeTanksensor() {
2023-03-22 20:06:35 +01:00
Wire.begin(SENSOR_TANK_SDA, SENSOR_TANK_SCL, 100000UL /* 100kHz */);
tankSensor.setBus(&Wire);
2023-03-22 21:01:24 +01:00
delay(100);
tankSensor.setTimeout(500);
2023-03-22 20:02:23 +01:00
long start = millis();
while (start + 500 > millis())
{
if (tankSensor.init())
{
distanceReady = true;
break;
}
else
{
delay(20);
}
}
if ((distanceReady) && (!tankSensor.timeoutOccurred()))
2023-03-22 20:02:23 +01:00
{
Serial.println("Sensor init done");
tankSensor.setSignalRateLimit(0.1);
// increase laser pulse periods (defaults are 14 and 10 PCLKs)
tankSensor.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18);
tankSensor.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, 14);
tankSensor.setMeasurementTimingBudget(200000);
2023-03-22 20:50:25 +01:00
tankSensor.startContinuous();
2023-03-22 20:02:23 +01:00
} else {
Serial.println("Sensor init failed");
}
2021-01-30 23:28:15 +01:00
}
2020-10-09 19:29:28 +02:00
2023-03-22 20:50:25 +01:00
void setup()
{
Serial.begin(115200);
pinMode(OUTPUT_SENSOR, OUTPUT);
digitalWrite(OUTPUT_SENSOR, HIGH);
Serial.println("Nodemcu ESP32 Start done");
initializeTanksensor();
}
2021-02-04 23:52:54 +01:00
void loop() {
2021-07-21 21:23:58 +02:00
2023-03-22 20:02:23 +01:00
delay(500);
2021-07-21 21:23:58 +02:00
2023-03-22 20:02:23 +01:00
if ((distanceReady) && (!tankSensor.timeoutOccurred()))
2023-03-22 20:02:23 +01:00
{
uint16_t distance = tankSensor.readRangeSingleMillimeters();
2023-03-22 21:01:24 +01:00
if (distance > 8000) {
Serial.println("Reset due invalid distance: 8 meter");
2023-03-22 20:50:25 +01:00
Wire.end();
2023-03-22 21:01:24 +01:00
delay(1000);
2023-03-22 20:50:25 +01:00
initializeTanksensor();
} else {
Serial.print("Distance");
Serial.println(distance);
}
2023-03-22 20:21:10 +01:00
} else {
Serial.println("Timeout");
2023-03-22 20:50:25 +01:00
tankSensor.stopContinuous();
Wire.end();
delay(100);
initializeTanksensor();
2023-03-22 20:02:23 +01:00
}
}