Quartz Components
Published © GPL3+

Smart Hydration Tracker Bottle Using ESP32 & VL53L0X Sensor

Smart hydration tracker that uses ESP32 and VL53L0X ToF sensor to monitor water consumption and help users stay hydrated throughout the day.

BeginnerFull instructions provided2 hours69
Smart Hydration Tracker Bottle Using ESP32 & VL53L0X Sensor

Things used in this project

Story

Read more

Custom parts and enclosures

Github

Schematics

circuit diagram

Schematic diagram

Code

Code For es32-c3 mini

C/C++
#include <Wire.h>
#include "Adafruit_VL53L0X.h"
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <BLE2902.h>

// Must match the Android App exactly
#define SERVICE_UUID        "12345678-1234-1234-1234-123456789abc"
#define CHARACTERISTIC_UUID "abcd1234-5678-90ab-cdef-123456789abc"

Adafruit_VL53L0X lox = Adafruit_VL53L0X();
BLECharacteristic *pCharacteristic;
bool deviceConnected = false;

class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      deviceConnected = true;
      Serial.println(">> App Connected");
    };
    void onDisconnect(BLEServer* pServer) {
      deviceConnected = false;
      Serial.println(">> App Disconnected - Advertising again");
      pServer->getAdvertising()->start();
    }
};

void setup() {
  Serial.begin(115200);

  // For ESP32-C3 Super Mini, define SDA/SCL pins explicitly
  Wire.begin(8, 9);

  Serial.println("Smart Bottle Initialization...");

  if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while(1);
  }

  BLEDevice::init("SmartBot");
  BLEServer *pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  BLEService *pService = pServer->createService(SERVICE_UUID);

  pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID,
                      BLECharacteristic::PROPERTY_READ   |
                      BLECharacteristic::PROPERTY_NOTIFY
                    );

  // This descriptor is REQUIRED for Android notifications to work
  pCharacteristic->addDescriptor(new BLE2902());

  pService->start();

  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(true);
  BLEDevice::startAdvertising();

  Serial.println("Ready! Open the Android app to connect.");
}

void loop() {
  VL53L0X_RangingMeasurementData_t measure;

  lox.rangingTest(&measure, false);

  if (measure.RangeStatus != 4) {
    int distance = measure.RangeMilliMeter;

    String val = String(distance);

    Serial.print("Distance (mm): "); Serial.println(distance);

    if (deviceConnected) {
      pCharacteristic->setValue(val.c_str());
      pCharacteristic->notify(); // Push to phone
    }
  } else {
    Serial.println(" Out of range ");
  }

  // 200ms is a good balance for smooth UI updates and battery
  delay(200);
}

App code

Credits

Quartz Components
6 projects β€’ 5 followers
Quartz Components builds robotics, IoT, AI hardware, Arduino, ESP32, automation, sensors, and innovative DIY electronics projects.

Comments