// Shaiful Danish Bin Shaiful Razmiza, 52224122335.
// Dzul Danish Ar-Rahman Bin Khairulaswari, 52224122563.
// Define Blynk template information
#define BLYNK_TEMPLATE_ID "TMPL6DQyQJdru"
#define BLYNK_TEMPLATE_NAME "ps3"
#define BLYNK_AUTH_TOKEN "mNZCOLlq3UvZIBXDzy8_VCXCEAqaqgtc"
// Include necessary library files
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
// WiFi credentials
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "FreePalestine_2.4G"; // Enter your WIFI name
char pass[] = "BatmanCeria"; // Enter your WIFI password
// Define sensor and output pin connections
int vibrationSensor = D0;
int soundSensor = D1;
int buzzerOutput = D2;
int ldrSensor = A0;
int ledgreenOutput = D6;
int ledyellowOutput = D7;
int ledredOutput = D8;
// Setup function runs once at the beginning
void setup() {
// Initialize the Blynk library
Blynk.begin(auth, ssid, pass);
// Setup Sensors and Outputs
Serial.begin(9600);
pinMode(vibrationSensor, INPUT);
pinMode(soundSensor, INPUT);
pinMode(ldrSensor, INPUT);
pinMode(buzzerOutput, OUTPUT);
pinMode(ledgreenOutput, OUTPUT);
pinMode(ledyellowOutput, OUTPUT);
pinMode(ledredOutput, OUTPUT);
}
// Loop function runs continuously
void loop() {
// Run the Blynk library
Blynk.run();
// Read sensor values
int vibrationValue = digitalRead(vibrationSensor);
int soundValue = digitalRead(soundSensor);
int ldrValue = analogRead(ldrSensor);
// Update Blynk app with sensor values
Blynk.virtualWrite(V0, vibrationValue);
Blynk.virtualWrite(V1, soundValue);
Blynk.virtualWrite(V2, ldrValue);
Blynk.virtualWrite(V3, digitalRead(ledgreenOutput));
Blynk.virtualWrite(V4, digitalRead(ledredOutput));
// Print sensor readings to Serial Monitor
Serial.println(vibrationValue);
Serial.println(soundValue);
Serial.println(ldrValue);
Serial.println(digitalRead(ledgreenOutput));
Serial.println(digitalRead(ledredOutput));
delay(500);
// Check if any sensor is HIGH or LDR is more than 150
if (vibrationValue == 1 || soundValue == 1 || ldrValue > 150) {
// Call method to indicate the Washing Machine is in use.
washingMachineOn();
} else {
// Buzzer on, LED red lights off. Blinking Yellow LED and LED green on.
washingMachineOff();
}
}
// Function to indicate the washing machine is ON
void washingMachineOn() {
// Buzzer off, LED red lights up.
noTone(buzzerOutput);
digitalWrite(ledgreenOutput, LOW);
digitalWrite(ledyellowOutput, LOW);
digitalWrite(ledredOutput, HIGH);
}
// Function for blinking Yellow LED
void blinkingYellow() {
// For loop statement, Yellow LED blinking 5 times
for (int i = 0; i < 5; ++i) {
tone(buzzerOutput, 500);
digitalWrite(ledyellowOutput, HIGH);
delay(500);
noTone(buzzerOutput);
digitalWrite(ledyellowOutput, LOW);
delay(500);
}
// Turn off Blinking Yellow LED
noTone(buzzerOutput);
digitalWrite(ledyellowOutput, LOW);
delay(5000);
}
// Function to indicate the washing machine is OFF
void washingMachineOff() {
// Blinking Yellow LED
blinkingYellow();
// Buzzer off, LED red lights up, Green LED lights up.
digitalWrite(ledredOutput, LOW);
digitalWrite(ledyellowOutput, LOW);
digitalWrite(ledgreenOutput, HIGH);
delay(500);
}