#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL66DdIuC50"
#define BLYNK_TEMPLATE_NAME "Control Led"
#define BLYNK_AUTH_TOKEN "MPq9OLScrDo4hiYDEoGnIn1He4L8z8Uo"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHTesp.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
DHTesp dht;
BlynkTimer timer;
#define LED1 2
#define LED2 4
#define ldr 23
#define RELAY_PIN 12
const int DHT_PIN = 15;
int kondisi = 0;
int kondisi2 = 0;
int baca_ldr;
void sendSensor()
{
TempAndHumidity data = dht.getTempAndHumidity();
//menampilkan temperature pada Serial monitor
Serial.print("% Temperature: ");
Serial.print(data.temperature);
Serial.println("C ");
Serial.print("% Kelembaban: ");
Serial.print(data.humidity);
Serial.println("% ");
baca_ldr = digitalRead(ldr);
Blynk.virtualWrite(V3, data.temperature); //mengirimkan data temperatur ke Virtual pin V3 di Blynk Cloud
Blynk.virtualWrite(V5, baca_ldr);
Blynk.virtualWrite(V4, data.humidity); //mengirimkan data kelemaban ke Virtual pin V4 di Blynk Cloud
if (baca_ldr == 1) {
digitalWrite(RELAY_PIN, HIGH);
} else {
digitalWrite(RELAY_PIN, LOW);
}
}
BLYNK_WRITE (V0)
{
kondisi = param.asInt();
if (kondisi == 1)
{
digitalWrite(LED1, HIGH);
Serial.println("LED 1 ON");
Blynk.virtualWrite(V0, HIGH);
}
else
{
digitalWrite(LED1, LOW);
Serial.println("LED 1 OFF");
Blynk.virtualWrite(V0, LOW);
}
}
BLYNK_WRITE (V1)
{
kondisi2 = param.asInt();
if (kondisi2 == 1)
{
digitalWrite(LED2, HIGH);
Serial.println("LED 2 ON");
Blynk.virtualWrite(V1, HIGH);
}
else
{
digitalWrite(LED2, LOW);
Serial.println("LED 2 OFF");
Blynk.virtualWrite(V1, LOW);
}
}
void setup() {
// put your setup code here, to run once:
dht.setup(DHT_PIN, DHTesp::DHT22);
Serial.begin(9600);
Serial.println("Hello, ESP32!");
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(ldr, INPUT);
pinMode(RELAY_PIN, OUTPUT);
Blynk.begin(auth, ssid, pass);
timer.setInterval(1000, sendSensor);
}
void loop() {
Blynk.run();
timer.run();
}