#define BLYNK_TEMPLATE_ID "TMPL3Ikv59TXh"
#define BLYNK_TEMPLATE_NAME "SMART MEDICINE DISPENSER"
#define BLYNK_AUTH_TOKEN "WazMMwSp-W_OHpDvGvYfbzQ4r6ztCezf"
//Initialization of header file
#include <WiFi.h>
#include <LiquidCrystal_I2C.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
// Initialization of header file
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo medicineserve;
// SSID and password of WiFi
char ssid[] = "Wokwi-GUEST";
char password[] = "";
const int buzz = 34;
const int led = 12;
const int servopin = 25;
const int button = 35;
bool lastButtonState = HIGH;
float level = 100;
const float dispense = 5; // debounce delay time in ms
void setup() {
Serial.begin(115200);
pinMode(buzz, OUTPUT);
pinMode(led, OUTPUT);
pinMode(button, INPUT_PULLUP);
lcd.init();
lcd.backlight();
// Connecting to WiFi
Serial.println("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Attempting to connect WiFi");
}
Serial.println("Connected to WiFi");
// Connecting to Blynk
Serial.println("Connecting to Blynk");
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, password);
while (!Blynk.connected()) {
delay(1000);
Serial.println("Attempting to connect Blynk");
}
Serial.println("Connected to Blynk");
medicineserve.attach(servopin);
medicineserve.write(0);
lcd.setCursor(0, 0);
lcd.println("Medicine Level:");
lcd.setCursor(0, 1);
lcd.println(level);
}
void loop() {
Blynk.run();
medicine();
}
void medicine() {
// Read the button state directly
bool reading = digitalRead(button);
// Check if the button is pressed
if (reading == LOW && lastButtonState == HIGH) {
// Button pressed, dispense medicine
if (level >= dispense) {
medicineserve.write(90); // Open servo
delay(1000);
medicineserve.write(0); // Close servo
level -= dispense;
// Update Blynk and LCD
Blynk.virtualWrite(V0, level);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Medicine Level:");
lcd.setCursor(0, 1);
lcd.print(level);
// LED and buzzer feedback
digitalWrite(led, HIGH);
tone(buzz, 1000); // Play tone at 1000 Hz
delay(200);
noTone(buzz);
digitalWrite(led, LOW);
} else {
// Alert for low medicine level
Blynk.logEvent("ALERT!", "Medicine level too low");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Medicine Level:");
lcd.setCursor(0, 1);
lcd.print("Too Low");
}
}
lastButtonState = reading; // Save the state for next loop
}