#define BLYNK_TEMPLATE_ID "TMPL63PasRq3g"
#define BLYNK_TEMPLATE_NAME "Smart Home"
#define BLYNK_AUTH_TOKEN "qLfIBqz1aZroUonegtKGpIF3EIu4vQyK"
// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define DHTPIN 26
#define DHTTYPE DHT22
DHT dht(DHTPIN,DHTTYPE);
char auth[] = BLYNK_AUTH_TOKEN;
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
#define pir_red 27
#define pir_blue 15
#define buzzer 18
#define button_door 19
#define LampuBiru 25
#define LampuMerah 5
#define Fan 2
#define Door 23
int pir_blue_State = 0;
int pir_red_State = 0;
int blue_state = 0;
int red_state = 0;
int fan_state = 0;
int door_state = 0;
int gerak = 0;
//Change the virtual pins according the rooms
#define pir_vblue V0
#define pir_vred V1
#define button_vdoor V3
#define temp V4
BLYNK_CONNECTED(){
Blynk.syncVirtual(pir_vblue);
Blynk.syncVirtual(pir_vred );
Blynk.syncVirtual(button_vdoor);
Blynk.syncVirtual(temp);
}
// This function is called every time the Virtual Pin state change
//i.e when web push switch from Blynk App or Web Dashboard
BLYNK_WRITE(pir_vblue) {
blue_state = param.asInt();
digitalWrite(LampuBiru, blue_state);
}
BLYNK_WRITE(pir_vred) {
red_state = param.asInt();
digitalWrite(LampuMerah, red_state);
}
BLYNK_WRITE(button_vdoor) {
door_state = param.asInt();
digitalWrite(Door, door_state);
tone(buzzer, 30);
delay(500);
noTone(buzzer);
delay(200);
}
void setup() {
// Debug Console
Serial.begin(115200);
dht.begin();
timer.setInterval(2500L, sendSensor);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Connecting to ");
lcd.setCursor(0, 1);
lcd.print("WiFi . . . ");
pinMode(pir_blue, INPUT_PULLUP);
pinMode(pir_red, INPUT_PULLUP);
pinMode(button_door, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
pinMode(LampuBiru, OUTPUT);
pinMode(LampuMerah, OUTPUT);
pinMode(Fan, OUTPUT);
pinMode(Door, OUTPUT);
//During Starting all Relays should TURN OFF
digitalWrite(buzzer, HIGH);
digitalWrite(LampuBiru, HIGH);
digitalWrite(LampuMerah, HIGH);
digitalWrite(Fan, HIGH);
digitalWrite(Door, HIGH);
Blynk.begin(auth, ssid, pass);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" WiFi ");
lcd.setCursor(0, 1);
lcd.print(" Connected");
delay(3000);
tone(buzzer, 30);
delay(500);
noTone(buzzer);
delay(200);
lcd.clear();
}
void loop() {
Blynk.run();
timer.run();
listen_pir();
selenoid_door ();
}
void listen_pir(){
int blue = digitalRead(pir_blue); // read input value
int red = digitalRead(pir_red);
blue_state == blue ;
pir_red == red ;
if (blue == HIGH && red == LOW) { // check if the input is HIGH
if(blue_state == LOW && red_state == LOW){
digitalWrite(LampuBiru, HIGH); // turn LED ON
digitalWrite(LampuMerah, LOW);
}
Blynk.virtualWrite(pir_vblue, blue);
Blynk.virtualWrite(pir_vred, red); //update button state
if(blue_state == LOW){
digitalWrite(LampuBiru, LOW);
}
if(red_state == HIGH && blue_state == HIGH ){
digitalWrite(LampuMerah,HIGH);
digitalWrite(LampuBiru, HIGH);
}
if (pir_blue_State == LOW) {
// we have just turned on
Serial.println("Ada Gerakan!");
// We only want to print on the output change, not state
pir_blue_State = HIGH;
if (pir_blue_State == HIGH){
lcd.clear();
Serial.print("Lampu 1 Menyala ");
lcd.setCursor (0,0);
lcd.println (" Lampu 1");
lcd.setCursor(0, 1);
lcd.println(" ON");
}
}
else {
if (pir_blue_State == HIGH) {
// we have just turned of
Serial.println("Gerakan Berhenti!");
// We only want to print on the output change, not state
pir_blue_State = LOW;
}
}
}
if (blue == LOW && red == HIGH) { // check if the input is HIGH
if(blue_state == LOW && red_state == LOW){
digitalWrite(LampuBiru, LOW); // turn LED ON
digitalWrite(LampuMerah, HIGH);
}
Blynk.virtualWrite(pir_vblue, blue);
Blynk.virtualWrite(pir_vred, red); //update button state
if(blue_state == HIGH){
digitalWrite(LampuBiru, HIGH);
}
if(red_state == LOW){
digitalWrite(LampuMerah,LOW);
}
if (pir_red_State == LOW) {
// we have just turned on
Serial.println("Ada Gerakan!");
// We only want to print on the output change, not state
pir_red_State = HIGH;
if(pir_red_State == HIGH){
lcd.clear();
Serial.print("Lampu 2 Menyala ");
lcd.setCursor (0,0);
lcd.println (" Lampu 2");
lcd.setCursor(0, 1);
lcd.println(" ON");
}
}
else {
if (pir_red_State == HIGH) {
// we have just turned of
Serial.println("Gerakan Berhenti!");
// We only want to print on the output change, not state
pir_red_State = LOW;
}
}
}
if (blue == HIGH && red == HIGH) { // check if the input is HIGH
digitalWrite(LampuBiru, HIGH); // turn LED ON
digitalWrite(LampuMerah, HIGH);
Blynk.virtualWrite(pir_vblue, blue );
Blynk.virtualWrite(pir_vred, red); //update button state
if(blue == LOW){
digitalWrite(LampuBiru, LOW);
}
if(red == LOW){
digitalWrite(LampuMerah,LOW);
}
}
if (blue == LOW && red == LOW) { // check if the input is HIGH
if(blue_state == LOW && red_state == LOW){
digitalWrite(LampuBiru, LOW); // turn LED ON
digitalWrite(LampuMerah, LOW);
}
if(blue_state == HIGH && red_state == LOW){
digitalWrite(LampuBiru, HIGH);
digitalWrite(LampuMerah, LOW);
}
if(red_state == HIGH && blue_state == LOW ){
digitalWrite(LampuMerah,HIGH);
digitalWrite(LampuBiru, LOW);
}
if(red_state == HIGH && blue_state == HIGH ){
digitalWrite(LampuMerah,HIGH);
digitalWrite(LampuBiru, HIGH);
}
Blynk.virtualWrite(pir_vblue, blue_state);
Blynk.virtualWrite(pir_vred, red_state);
}
}
void selenoid_door (){
if(digitalRead(button_door) == LOW){
tone(buzzer, 30);
delay(500);
noTone(buzzer);
delay(200);
control_relay(3);
Blynk.virtualWrite(button_vdoor, door_state) ; //update button state
}
}
void control_relay(int relay){
if(relay == 3){
lcd.clear();
door_state = !door_state;
digitalWrite(Door, door_state);
Serial.print("Kondisi = ");
Serial.println(door_state);
lcd.setCursor (0,0);
lcd.println (" Door");
lcd.setCursor(0, 1);
if(door_state == HIGH){
lcd.println(" ON");
}
else if(door_state == LOW){
lcd.println(" OFF");
}
delay(50);
}
}
void sendSensor(){
float t = dht.readTemperature();
if (t >= 30){
digitalWrite(Fan, HIGH);
}
else if (t < 30) {
//&& (fan_state == LOW)||(fan_state == HIGH)){
digitalWrite(Fan, LOW);
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temperature");
lcd.setCursor(0,1);
lcd.print("= ");
lcd.print(t);
lcd.print (" *C");
delay(1000);
Blynk.virtualWrite(temp, t);
}