// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID "TMPLcsTEe6uD"
#define BLYNK_DEVICE_NAME "IOT SMART HOME"
#define BLYNK_AUTH_TOKEN "0i6ILQXN8UToKiJ_0lSsafzKBEckxZLE"

#define BLYNK_PRINT Serial

#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = BLYNK_AUTH_TOKEN;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";

// The below are constants, which cannot be changed
#define LIGHT_SENSOR_PIN  33  // ESP32 pin GIOP36 (ADC0) connected to light sensor
#define LED_PIN           13  // ESP32 pin GIOP22 connected to LED
#define ANALOG_THRESHOLD  500
#define echoPin 4 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 19  //attach pin D3 Arduino to pin Trig of HC-SR04

// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement

LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);

BlynkTimer timer;

WidgetLED led3(V3);
boolean LEDState = false;

BLYNK_CONNECTED ()
{
   Blynk.syncVirtual (V3); 
}

void sendSensor()
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)

  int LDRValue = analogRead(LIGHT_SENSOR_PIN); // read the value on analog pin

  if (LDRValue < ANALOG_THRESHOLD)
    digitalWrite(LED_PIN, HIGH); // turn on LED
  else
    digitalWrite(LED_PIN, LOW);  // turn off LED
  
   LCD.setCursor(0,0);
   LCD.print("Distance: ");
   LCD.print(distance);
   LCD.println(" cm");
   LCD.setCursor(0,1);
   LCD.print("LDRValue: ");
   LCD.println(LDRValue);

  Serial.print("Distance: ");
  Serial.println(distance); 
  Serial.print("LDRValue: ");
  Serial.println(LDRValue);
  delay(2000);
  
  Blynk.virtualWrite(V1, distance);
  Blynk.virtualWrite(V2, LDRValue);

}


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

  pinMode(LED_PIN, OUTPUT); // set ESP32 pin to output mode
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT

  LCD.init();
  LCD.backlight();
  LCD.setCursor(1, 0);
  LCD.print("IOT SMART HOME");
  LCD.setCursor(3, 1);
  LCD.print("MK CHANNEL");
  delay(5000);
  LCD.clear();

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

void loop() {
  Blynk.run();
  timer.run();
}