/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "ESP32 Blynk"
	- Source Code NOT compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-06-30 18:06:04

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* If water level < 0, print empty; If water level > */
	/* 16, print full; else print water level */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include <Ultrasonic.h>  //https://github.com/ErickSimoes/Ultrasonic
#include <WiFi.h>        // Built-in library for WiFi on ESP32
#include <BlynkSimpleEsp32.h>  // Blynk library for ESP32

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t Level_HC_SR04_Echo_PIN_D13 = 13;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t Level_HC_SR04_Trigger_PIN_D4 = 4;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool Level_HC_SR04_Trigger_PIN_D4_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float Level_HC_SR04_Trigger_PIN_D4_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
Ultrasonic ultrasonic(Level_HC_SR04_Trigger_PIN_D4, Level_HC_SR04_Echo_PIN_D13);  // Initialize Ultrasonic object with trigger and echo pins

char auth[] = "YourAuthToken";  // Blynk authentication token
char ssid[] = "YourNetworkName";  // WiFi SSID
char pass[] = "YourPassword";  // WiFi Password

BlynkTimer timer;  // Blynk Timer object

void myTimerEvent() {
  Blynk.virtualWrite(V5, millis() / 1000);  // Send uptime to Blynk app
}

void setup(void)
{
    // put your setup code here, to run once:
    Serial.begin(9600);  // Initialize serial communication for debugging
    pinMode(Level_HC_SR04_Echo_PIN_D13, INPUT);
    pinMode(Level_HC_SR04_Trigger_PIN_D4, OUTPUT);

    // Initialize Blynk
    Blynk.begin(auth, ssid, pass);

    // Call myTimerEvent every second
    timer.setInterval(1000L, myTimerEvent);
}

void loop(void)
{
    // put your main code here, to run repeatedly:
    Blynk.run();  // Run Blynk
    timer.run();  // Run Blynk timer
    updateOutputs();  // Refresh output data
    delay(1000);  // Wait for 1 second before next measurement
}

void updateOutputs()
{
    // Trigger the ultrasonic sensor and read the distance
    Level_HC_SR04_Trigger_PIN_D4_phyData = ultrasonic.read();

    // Print the distance to the Serial Monitor
    Serial.print("Distance in CM: ");
    Serial.println(Level_HC_SR04_Trigger_PIN_D4_phyData);

    // Update the raw data output
    Level_HC_SR04_Trigger_PIN_D4_rawData = (Level_HC_SR04_Trigger_PIN_D4_phyData < 10);  // Example condition

    // Write the raw data to the trigger pin
    digitalWrite(Level_HC_SR04_Trigger_PIN_D4, Level_HC_SR04_Trigger_PIN_D4_rawData);

    // System requirement: Print water level status
    if (Level_HC_SR04_Trigger_PIN_D4_phyData < 0) {
        Serial.println("Water level: Empty");
    } else if (Level_HC_SR04_Trigger_PIN_D4_phyData > 16) {
        Serial.println("Water level: Full");
    } else {
        Serial.print("Water level: ");
        Serial.println(Level_HC_SR04_Trigger_PIN_D4_phyData);
    }
}

/* END CODE */