You are on page 1of 6

//Viral Science www.youtube.com/c/viralscience www.viralsciencecreativity.

com
//Blynk Home Automation with Physical Button and ON OFF State Feedback
//Scan the QR Code to get the app with pre-installed settings

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266mDNS.h> // For OTA with ESP8266
#include <WiFiUdp.h> // For OTA
#include <ArduinoOTA.h> // For OTA

BlynkTimer timer;

void checkPhysicalButton();

int relay1State = LOW;


int pushButton1State = HIGH;

int relay2State = LOW;


int pushButton2State = HIGH;

int relay3State = LOW;


int pushButton3State = HIGH;

int relay4State = LOW;


int pushButton4State = HIGH;

#define AUTH "AXJzFs8ycUPCR5lqRxxj1QizoE8eixGz" // You should get Auth Token in


the Blynk App.
#define WIFI_SSID "TP-LINK_48814E" //Enter Wifi Name
#define WIFI_PASS "01033793" //Enter wifi Password

#define SERVER "blynk-cloud.com " // Comment-out if use Blynk hosted


cloud service
#define PORT 8442

#define RELAY_PIN_1 12 //D6


#define RELAY_PIN_2 16 //D0
#define RELAY_PIN_3 4 //D2
#define RELAY_PIN_4 5 //D1

#define PUSH_BUTTON_1 2 //D4


#define PUSH_BUTTON_2 14 //D5
#define PUSH_BUTTON_3 13 //D7
#define PUSH_BUTTON_4 1 //TX

#define VPIN_BUTTON_1 V12


#define VPIN_BUTTON_2 V13
#define VPIN_BUTTON_3 V14
#define VPIN_BUTTON_4 V15

#define OTA_HOSTNAME "Home_Automation"

BLYNK_CONNECTED() {

// Request the latest state from the server


Blynk.syncVirtual(VPIN_BUTTON_1);
Blynk.syncVirtual(VPIN_BUTTON_2);
Blynk.syncVirtual(VPIN_BUTTON_3);
Blynk.syncVirtual(VPIN_BUTTON_4);

// Alternatively, you could override server state using:


// Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
// Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
// Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
// Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);

// When App button is pushed - switch the state

BLYNK_WRITE(VPIN_BUTTON_1) {
relay1State = param.asInt();
digitalWrite(RELAY_PIN_1, relay1State);
}

BLYNK_WRITE(VPIN_BUTTON_2) {
relay2State = param.asInt();
digitalWrite(RELAY_PIN_2, relay2State);
}
BLYNK_WRITE(VPIN_BUTTON_3) {
relay3State = param.asInt();
digitalWrite(RELAY_PIN_3, relay3State);
}
BLYNK_WRITE(VPIN_BUTTON_4) {
relay4State = param.asInt();
digitalWrite(RELAY_PIN_4, relay4State);
}

void checkPhysicalButton()
{
if (digitalRead(PUSH_BUTTON_1) == LOW) {
// pushButton1State is used to avoid sequential toggles
if (pushButton1State != LOW) {

// Toggle Relay state


relay1State = !relay1State;
digitalWrite(RELAY_PIN_1, relay1State);

// Update Button Widget


Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
}
pushButton1State = LOW;
} else {
pushButton1State = HIGH;
}

if (digitalRead(PUSH_BUTTON_2) == LOW) {
// pushButton2State is used to avoid sequential toggles
if (pushButton2State != LOW) {

// Toggle Relay state


relay2State = !relay2State;
digitalWrite(RELAY_PIN_2, relay2State);
// Update Button Widget
Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
}
pushButton2State = LOW;
} else {
pushButton2State = HIGH;
}

if (digitalRead(PUSH_BUTTON_3) == LOW) {
// pushButton3State is used to avoid sequential toggles
if (pushButton3State != LOW) {

// Toggle Relay state


relay3State = !relay3State;
digitalWrite(RELAY_PIN_3, relay3State);

// Update Button Widget


Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
}
pushButton3State = LOW;
} else {
pushButton3State = HIGH;
}

if (digitalRead(PUSH_BUTTON_4) == LOW) {
// pushButton4State is used to avoid sequential toggles
if (pushButton4State != LOW) {

// Toggle Relay state


relay4State = !relay4State;
digitalWrite(RELAY_PIN_4, relay4State);

// Update Button Widget


Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);
}
pushButton4State = LOW;
} else {
pushButton4State = HIGH;
}
}
// BBBBBBBBBBBBBBBBBBBBBBBBBBBBbbuuuuuuuuuuuuuuuuuuuuuu
ccccccccccccccccccuuuuuuuuuuuu
//
//
//
#include "pitches.h"
int melody[] = {
NOTE_AS4, NOTE_AS4, NOTE_AS4, NOTE_AS4,
NOTE_AS4, NOTE_AS4, NOTE_AS4, NOTE_AS4,
NOTE_AS4, NOTE_AS4, NOTE_AS4, NOTE_AS4,
NOTE_AS4, NOTE_AS4, NOTE_AS4, NOTE_AS4,
NOTE_AS4, NOTE_AS4, NOTE_AS4, NOTE_AS4,
NOTE_D5, NOTE_D5, NOTE_D5, NOTE_D5,
NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5,
NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5,
NOTE_G5, NOTE_G5, NOTE_G5, NOTE_G5,
NOTE_G5, NOTE_G5, NOTE_G5, NOTE_G5,
NOTE_G5, NOTE_G5, NOTE_G5, NOTE_G5,
NOTE_C5, NOTE_AS4, NOTE_A4, NOTE_F4,
NOTE_G4, 0, NOTE_G4, NOTE_D5,
NOTE_C5, 0, NOTE_AS4, 0,
NOTE_A4, 0, NOTE_A4, NOTE_A4,
NOTE_C5, 0, NOTE_AS4, NOTE_A4,
NOTE_G4,0, NOTE_G4, NOTE_AS5,
NOTE_A5, NOTE_AS5, NOTE_A5, NOTE_AS5,
NOTE_G4,0, NOTE_G4, NOTE_AS5,
NOTE_A5, NOTE_AS5, NOTE_A5, NOTE_AS5,
NOTE_G4, 0, NOTE_G4, NOTE_D5,
NOTE_C5, 0, NOTE_AS4, 0,
NOTE_A4, 0, NOTE_A4, NOTE_A4,
NOTE_C5, 0, NOTE_AS4, NOTE_A4,
NOTE_G4,0, NOTE_G4, NOTE_AS5,
NOTE_A5, NOTE_AS5, NOTE_A5, NOTE_AS5,
NOTE_G4,0, NOTE_G4, NOTE_AS5,
NOTE_A5, NOTE_AS5, NOTE_A5, NOTE_AS5
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
};
// code pir
#define pirPin 2
#define tone1 15
// #define led_pin 0
// Create variables:
int val = 0;
bool motionState = false; // We start with no motion detected.
void setup()
{
// Configure the pins as input or output:
pinMode(tone1, OUTPUT);
pinMode(pirPin, INPUT);
// pinMode(led_pin, OUTPUT);
// Begin serial communication at a baud rate of 9600:
Serial.begin(9600);

Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS,"blynk-cloud.com", 8442);


ArduinoOTA.setHostname(OTA_HOSTNAME); // For OTA - Use your own device
identifying name
ArduinoOTA.begin(); // For OTA

pinMode(RELAY_PIN_1, OUTPUT);
pinMode(PUSH_BUTTON_1, INPUT_PULLUP);
digitalWrite(RELAY_PIN_1, relay1State);

pinMode(RELAY_PIN_2, OUTPUT);
pinMode(PUSH_BUTTON_2, INPUT_PULLUP);
digitalWrite(RELAY_PIN_2, relay2State);

pinMode(RELAY_PIN_3, OUTPUT);
pinMode(PUSH_BUTTON_3, INPUT_PULLUP);
digitalWrite(RELAY_PIN_3, relay3State);

pinMode(RELAY_PIN_4, OUTPUT);
pinMode(PUSH_BUTTON_4, INPUT_PULLUP);
digitalWrite(RELAY_PIN_4, relay4State);

// Setup a function to be called every 100 ms


timer.setInterval(500L, checkPhysicalButton);
}

void loop()
{
Blynk.run();
ArduinoOTA.handle(); // For OTA
timer.run();
// Read out the pirPin and store as val:

val = digitalRead(pirPin);
// If motion is detected (pirPin = HIGH), do the following:
if (val == HIGH) {
// digitalWrite(led_pin, HIGH);
// delay(1000);
Blynk.notify("Anh h�ng c� tr?m d?t nh?p ph�ng anh oi hihi");
// for (int thisNote = 0; thisNote < 112; thisNote++) {
int noteDuration = 750 / noteDurations[thisNote];
tone(15, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(15);
}
// Change the motion state to true (motion detected):
if (motionState == false) {
Serial.println("Motion detected!");
motionState = true;
}
}
// If no motion is detected (pirPin = LOW), do the following:
else {
// digitalWrite(led_pin, LOW);
noTone(15);
// Change the motion state to false (no motion):
if (motionState == true) {
Serial.println("Motion ended!");
motionState = false;
}
}//

}
}

You might also like