#define BLYNK_TEMPLATE_ID "TMPL6SdC4-AQx"
#define BLYNK_TEMPLATE_NAME "l298n dht11 button"
#define BLYNK_AUTH_TOKEN "JPUy_4BqnQRWleaSDC8-nlExNblZt1D5"

#include <Arduino.h>
#include <DHT.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

#define DHTPIN 18         // Pin tempat DHT11 terhubung
#define DHTTYPE DHT22     // Tipe sensor DHT
#define BUTTON_PIN 4      // Pin push button

DHT dht(DHTPIN, DHTTYPE);
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";

// Motor A
int motor1Pin1 = 27; 
int motor1Pin2 = 26; 
int enable1Pin = 14; 

// Setting PWM properties
const int freq = 30000;
const int pwmChannel = 0;
const int resolution = 8;
int dutyCycle = 200;

int switchState = LOW;  // Global state for the Blynk switch

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);

  pinMode(BUTTON_PIN, INPUT);
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(enable1Pin, OUTPUT);
  ledcSetup(pwmChannel, freq, resolution);
  ledcAttachPin(enable1Pin, pwmChannel);

  dht.begin();
}

BLYNK_WRITE(V0) { // Assuming V0 is the virtual pin connected to the switch widget
  switchState = param.asInt();  // Set switch state to the value received from Blynk App
}

void loop() {
  Blynk.run();
  
  float temperatureCelsius = dht.readTemperature(); // Suhu dalam Celsius
  float humidity = dht.readHumidity(); // Kelembaban
  int buttonState = digitalRead(BUTTON_PIN);

  Serial.print("Suhu: ");
  Serial.print(temperatureCelsius);
  Serial.print("C, Kelembaban: ");
  Serial.print(humidity);
  Serial.println("%");

  Blynk.virtualWrite(V2, temperatureCelsius); // Suhu dalam Celsius
  Blynk.virtualWrite(V1, humidity); // Kelembaban

  // Membaca status pin motor1Pin1
  bool motorStatus = digitalRead(motor1Pin1);
  Serial.print("Motor1Pin1 status: ");
  Serial.println(motorStatus ? "HIGH" : "LOW");
  Blynk.virtualWrite(V3, motorStatus);  // Kirim status pin ke V3

  // Mengatur motor DC berdasarkan kondisi suhu dan tombol/switch
  if (temperatureCelsius > 30) {
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
    ledcWrite(pwmChannel, dutyCycle);
  } else if (buttonState == HIGH || switchState == HIGH) {
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
    ledcWrite(pwmChannel, dutyCycle);
  } else {
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, LOW);
    ledcWrite(pwmChannel, 0);
  }

  delay(1000); // Mengurangi delay untuk responsivitas yang lebih baik
}

$abcdeabcde151015202530354045505560fghijfghij
esp:0
esp:2
esp:4
esp:5
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:21
esp:22
esp:23
esp:25
esp:26
esp:27
esp:32
esp:33
esp:34
esp:35
esp:3V3
esp:EN
esp:VP
esp:VN
esp:GND.1
esp:D2
esp:D3
esp:CMD
esp:5V
esp:GND.2
esp:TX
esp:RX
esp:GND.3
esp:D1
esp:D0
esp:CLK
L298N Breakout
chip1:EN A
chip1:IN1
chip1:IN2
chip1:IN3
chip1:IN4
chip1:EN B
chip1:OUT1
chip1:OUT2
chip1:OUT3
chip1:OUT4
chip1:5V
chip1:GND
chip1:12V
chip1:GND2
vcc1:VCC
gnd1:GND
led1:A
led1:C
r1:1
r1:2
dht1:VCC
dht1:SDA
dht1:NC
dht1:GND
sw1:1
sw1:2
sw1:3