/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL6CU8DtccY"
#define BLYNK_TEMPLATE_NAME "Deviceku"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "DHT.h"
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "1HuhhaL9r701Etvw0dRhUnBBVprqVUil";  //Paste auth token you copied
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";  ///Enter your wifi name
char pass[] = "";                    // Enter wifi password
#define DHTPIN 32                   // What digital pin we're connected to select yours accordingly
#define pinLDR 33
// Uncomment whatever type you're using!
#define DHTTYPE DHT22  // DHT 11

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;


void sendSensor() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();  // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V1, h);  // select your virtual pins accordingly
  Blynk.virtualWrite(V0, t);  // select your virtual pins accordingly
}



void setup() {
  // Debug console
  Serial.begin(115200);
  delay(1000);
  Blynk.begin(auth, ssid, pass);
  dht.begin();
  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}
void loop() {
  Blynk.run();
  timer.run();
  SensorLDR();
}



int SensorLDR() {
  int nilaiAnalogLDR = analogRead(pinLDR);
  
  double Vout = nilaiAnalogLDR * 0.0008056640625;
  int lux = 500 / (10 * ((Vout) / (3.3 - Vout)));

  Serial.println("Lux Intensity= " + String(int(lux)));

  Blynk.virtualWrite(V2, lux); 
  
  return lux;

  
}