Skip to content

Instantly share code, notes, and snippets.

@SimedruF
Last active December 4, 2022 10:26
Show Gist options
  • Save SimedruF/18b239011771b99e54b9ac262d1427a9 to your computer and use it in GitHub Desktop.
Save SimedruF/18b239011771b99e54b9ac262d1427a9 to your computer and use it in GitHub Desktop.
ESP32_uLIDAR_VL53L01XV2
/*
Florin Simedru
Complete project details at https://automatic-house.blogspot.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
/*
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[platformio]
default_envs = esp32doit-devkit-v1
[env]
monitor_port = COM7
[env:esp32doit-devkit-v1]
platform = espressif32
board = esp32doit-devkit-v1
framework = arduino
monitor_speed = 115200
upload_port = COM7
lib_deps =
mbed-components/[email protected]+sha.cf4d7779d9d6
pololu/VL53L0X@^1.3.1
*/
#include <Arduino.h>
#include "BluetoothSerial.h"
#include<Wire.h>
#include <VL53L0X.h>
// Check if Bluetooth configs are enabled
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
#define LED_PIN LED_BUILTIN
void blink_test();
// Bluetooth Serial object
BluetoothSerial SerialBT;
// Handle received and sent messages
String message = "";
String distanceString;
char incomingChar;
// Timer: auxiliar variables
unsigned long previousMillis = 0; // Stores last time temperature was published
const long interval = 10000; // interval at which to publish sensor readings
uint16_t Distance_ui16;
VL53L0X sensor;
void setup()
{
// Initialise I2C communication as MASTER
Wire.begin();
// Initialise serial communication, set baud rate = 115200
Serial.begin(115200);
// Bluetooth device name
SerialBT.begin("ESP32_DISTANCE_SENZOR");
/* Initialize Buildin led pin (D2) for output */
pinMode(LED_PIN,OUTPUT);
delay(300);
sensor.setTimeout(500);
if (!sensor.init())
{
Serial.println("Failed to detect and initialize sensor!");
while (1) {}
}
// 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)).
sensor.startContinuous();
}
void loop()
{
unsigned int data[2];
unsigned long currentMillis = millis();
Distance_ui16 = sensor.readRangeContinuousMillimeters() - 40;
// Output data to serial monitor
distanceString = "Distance : "+String(Distance_ui16)+" mm";
if(Distance_ui16 <100)
{
blink_test();
distanceString += " Warning !!";
}
Serial.println(distanceString);
// Send light luminance readings
if (currentMillis - previousMillis >= interval){
previousMillis = currentMillis;
SerialBT.println(distanceString);
}
// Read received messages (LED control command)
if (SerialBT.available()){
char incomingChar = SerialBT.read();
if (incomingChar != '\n'){
message += String(incomingChar);
}
else{
message = "";
}
}
if(message.isEmpty()==false)
{
// Check received message and control output accordingly
if (message.equals("led_on") && message.endsWith("n"))
{
digitalWrite(LED_PIN, HIGH);
Serial.println(message);
}
else if (message.equals("led_off")&& message.endsWith("f"))
{
digitalWrite(LED_PIN, LOW);
Serial.println(message);
}
}
delay(500);
}
void blink_test()
{
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment