#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h> // Correct library for ESP32
// Blynk Credentials
#define BLYNK_TEMPLATE_ID "TMPL3zDc7_SN-"
#define BLYNK_TEMPLATE_NAME "personalized scent"
#define BLYNK_AUTH_TOKEN "JjkQhl06FsDdm6LGbjX5yn2FLxRFmE_o" // Keep only the auth token
#define PUMP1_PIN 4
#define PUMP2_PIN 5
#define PUMP3_PIN 18
#define SENSOR_PIN 34
#define SERVO_PIN 13
// Blynk Virtual Pin Assignments
#define VIRTUAL_SENSOR V1
#define VIRTUAL_PUMP1 V2
#define VIRTUAL_PUMP2 V3
#define VIRTUAL_PUMP3 V4
Servo servoMotor;
int sensorValue = 0;
char ssid[] = "Wokwi-GUEST"; // Wi-Fi credentials
char pass[] = "";
void setup() {
// Serial Monitor for debugging
Serial.begin(115200);
// Pin setup
pinMode(PUMP1_PIN, OUTPUT);
pinMode(PUMP2_PIN, OUTPUT);
pinMode(PUMP3_PIN, OUTPUT);
pinMode(SENSOR_PIN, INPUT);
// Attach servo to a specific pin
servoMotor.attach(SERVO_PIN);
servoMotor.write(0); // Servo start position
// Connect to Wi-Fi
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
// Initialize Blynk
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
Serial.println("System Initialized!");
}
BLYNK_WRITE(VIRTUAL_PUMP1) {
int state = param.asInt();
if (state == 1) activatePump(1);
}
BLYNK_WRITE(VIRTUAL_PUMP2) {
int state = param.asInt();
if (state == 1) activatePump(2);
}
BLYNK_WRITE(VIRTUAL_PUMP3) {
int state = param.asInt();
if (state == 1) activatePump(3);
}
void loop() {
// Read the sensor value
sensorValue = analogRead(SENSOR_PIN);
Serial.println("Sensor Value: " + String(sensorValue));
// Send sensor value to Blynk app
Blynk.virtualWrite(VIRTUAL_SENSOR, sensorValue);
// Run Blynk commands
Blynk.run();
delay(100);
}
void activatePump(int pumpNumber) {
// Activate pumps and servo based on pump number
if (pumpNumber == 1) {
digitalWrite(PUMP1_PIN, HIGH); // Turn pump 1 on
servoMotor.write(90); // Move servo to trigger the pump
delay(500); // Wait for the pump to activate
digitalWrite(PUMP1_PIN, LOW); // Turn pump 1 off
servoMotor.write(0); // Return servo to initial position
} else if (pumpNumber == 2) {
digitalWrite(PUMP2_PIN, HIGH); // Turn pump 2 on
servoMotor.write(90); // Move servo to trigger the pump
delay(500); // Wait for the pump to activate
digitalWrite(PUMP2_PIN, LOW); // Turn pump 2 off
servoMotor.write(0); // Return servo to initial position
} else if (pumpNumber == 3) {
digitalWrite(PUMP3_PIN, HIGH); // Turn pump 3 on
servoMotor.write(90); // Move servo to trigger the pump
delay(500); // Wait for the pump to activate
digitalWrite(PUMP3_PIN, LOW); // Turn pump 3 off
servoMotor.write(0); // Return servo to initial position
}
}