#define BLYNK_TEMPLATE_ID "TMPL6Cs4KDOD8"
#define BLYNK_TEMPLATE_NAME "20234 FYP Afiq"
#define BLYNK_AUTH_TOKEN "Sv0ldDDixlorFoKQSUkrjhlXjY8FMEjl"

#define LDR_PIN 34
#define LED_PIN 2
#define DCMOTOR_PIN 0
#define POTENTIOMETER_PIN 32
#define SERVO_PIN 12
#define ADC_RESOLUTION 4095
#define DHTPIN 4
#define DHTTYPE DHT22

#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
#include "DHT.h"

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";

DHT dht(DHTPIN, DHTTYPE);
Servo myservo; 
BlynkTimer timer;

int sensorValue = 0;  // Value read from the ADC

void setup() {

  analogReadResolution(12); 
  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);

  pinMode(LED_PIN, OUTPUT);
  pinMode(DCMOTOR_PIN, OUTPUT);
  myservo.attach(SERVO_PIN);
  timer.setInterval(250L, InputOutputStatus); 
}

void InputOutputStatus() {
  sensorValue = analogRead(POTENTIOMETER_PIN);	// Read the analog in value:
  Serial.print("potValue = ");					// Print the results...
  Serial.println(sensorValue);					// ...to the serial monitor:
  Blynk.virtualWrite(V0, sensorValue);			// Send the results to Gauge Widget
  
  sensorValue = analogRead(LDR_PIN);			// Read the analog in value:
  Serial.print("ldrValue = ");					// Print the results...
  Serial.println(sensorValue);					// ...to the serial monitor:
  Blynk.virtualWrite(V1, sensorValue);			// Send the results to Gauge Widget
  
  sensorValue = dht.readTemperature();			// Read the analog in value:
  Serial.print("tempValue = ");					// Print the results...
  Serial.println(sensorValue);					// ...to the serial monitor:
  Blynk.virtualWrite(V2, sensorValue);			// Send the results to Gauge Widget
  
  sensorValue = 0;
}

/* BLYNK_WRITE(V3) // Executes when the value of virtual pin 0 changes
{
  if(param.asInt() == 1)
  {
    // execute this code if the switch widget is now ON
    myservo.write(angle);  // Set digital pin 2 HIGH
  }
  else
  {
    // execute this code if the switch widget is now OFF
    digitalWrite(LED_PIN, LOW);  // Set digital pin 2 LOW    
  }
} */

BLYNK_WRITE(V4) // Executes when the value of virtual pin 0 changes
{
  if(param.asInt() == 1)
  {
    // execute this code if the switch widget is now ON
    digitalWrite(LED_PIN, HIGH);  // Set digital pin 2 HIGH
  }
  else
  {
    // execute this code if the switch widget is now OFF
    digitalWrite(LED_PIN, LOW);  // Set digital pin 2 LOW    
  }
}

BLYNK_WRITE(V5) // Executes when the value of virtual pin 0 changes
{
  if(param.asInt() == 1)
  {
    // execute this code if the switch widget is now ON
    digitalWrite(DCMOTOR_PIN, HIGH);  // Set digital pin 2 HIGH
  }
  else
  {
    // execute this code if the switch widget is now OFF
    digitalWrite(DCMOTOR_PIN, LOW);  // Set digital pin 2 LOW    
  }
}

void loop() {

  Blynk.run();
  timer.run();

  int potValue = analogRead(POTENTIOMETER_PIN); 
  int angle = map(potValue, 0, ADC_RESOLUTION, 0, 180); 
  myservo.write(angle); 
  Serial.print("potValue: ");
  Serial.println(potValue);
  delay(1000); 

  int ldrValue = analogRead(LDR_PIN);
  if (ldrValue < 3600) { 
    digitalWrite(LED_PIN, LOW); 
  } 
  else {
    digitalWrite(LED_PIN, HIGH);
  }
  Serial.print("ldrValue: ");
  Serial.println(ldrValue); 
  delay(1000); 

  float tempValue = dht.readTemperature();
  if (tempValue > 50) {
    digitalWrite(DCMOTOR_PIN, HIGH);
  } 
  else {
    digitalWrite(DCMOTOR_PIN, LOW);
  }
  Serial.print("tempValue: ");
  Serial.print(tempValue);
  Serial.println(" Celcius");
  delay(1000);

}
$abcdeabcde151015202530354045505560fghijfghij