#define BLYNK_TEMPLATE_ID "TMPL6fiXhZelW"
#define BLYNK_TEMPLATE_NAME "ESP32 Alarm and Pill notification system"
#define BLYNK_AUTH_TOKEN "Kax6SSYKwx7520OQkMZpig0_VYGhWHxV"
#define BLYNK_PRINT Serial
#define VIRTUAL_PIN_1 V1
#include<LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
/* Pwede nani e uncomment if nana mo ESP32
#include <WiFi.h>
#include "time.h"
#include "sntp.h"
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASS";
const char* auth = "rzVnhnNdU9pAgSrR0Kfx-3-TOOvziDLm";
const char* ntpServer1 = "pool.ntp.org";
const char* ntpServer2 = "time.nist.gov";
const long gmtOffset_sec = 28800;
const int daylightOffset_sec = 0;
*/
LiquidCrystal_I2C lcd(0x27,16,2);
Servo myServo;
const int buttonPin = 2; // Pin connected to the pushbutton
const int servoPin = 18; // Pin connected to the servo motor
const int buzzerPin = 4;
int buttonState = 0; // Variable for reading the pushbutton status
int lastButtonState = 0; // Variable to store the previous pushbutton state
int servoPosition = 0; // Variable to store the current servo position
int buzzerState = 0;
unsigned long lastBuzzerState = 0;
const unsigned long BUZZER_TIMEOUT = 300000; // 5 minutes in milliseconds
bool pillReminderSent = false;
bool alarmTriggered = false; // Flag to track if the alarm is triggered
void setup() {
Serial.begin(115200);
/* Uncomment if nana Esp32
Blynk.begin(auth,ssid,password,"blynk.cloud",80);
sntp_set_time_sync_notification_cb( timeavailable );
sntp_servermode_dhcp(1);
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2);
//connect to WiFi
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" CONNECTED");
*/
lcd.init();
lcd.backlight();
lcd.print("Hello");
pinMode(buttonPin, INPUT);
myServo.attach(servoPin);
myServo.write(0);
pinMode (buzzerPin, OUTPUT);
pinMode (V1, INPUT);
}
void loop() {
/* Uncomment if nana Esp32
Blynk.run();
printLocalTime();
*/
triggerAlarm();
turnOffAlarm();
if (!alarmTriggered) {
// Check if it's time to trigger the alarm (e.g., every 10 seconds)
if (V1 == HIGH) {
triggerAlarm();
}
} else { // If alarm is triggered
// Check if the button is pressed to turn off the alarm
if (digitalRead(buttonPin) && V1 == LOW) {
turnOffAlarm();
}
}
buttonState = digitalRead(buttonPin);
if(buttonState != lastButtonState){
if (buttonState == HIGH) {
Blynk.logEvent("confirmed_pill_dispensed"); //ako ra ni gi insert gikan sa tutorial
rotateServo();
}
delay(50);
}
lastButtonState = buttonState;
//Serial.println(buttonState);
// Check button state
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
// Button pressed, clear pill reminder flag
pillReminderSent = false;
}
if ( buzzerState && buttonState == HIGH) {
// Check if 5 minutes have passed since the last button press
if (millis() - lastBuzzerState >= BUZZER_TIMEOUT) {
// Send notification
Blynk.logEvent("pill_not_dispensed");
}
}
}
void rotateServo() {
myServo.write(90); // Rotate the servo to 90 degrees
delay(1000); // Wait for 1 second
myServo.write(0); // Return the servo to its initial position (0 degrees)
delay(1000); // Wait for 1 second
}
void triggerAlarm() {
alarmTriggered = true; // Set the alarm triggered flag
// Play the alarm tone sequence in a loop until turned off
while (alarmTriggered) {
// Play the alarm tone sequence
tone(buzzerPin, 1000); // Start with a tone
delay(200); // Delay for the tone duration
noTone(buzzerPin); // Turn off the tone
delay(200); // Delay between tones
tone(buzzerPin, 1500); // Second tone
delay(200); // Delay for the tone duration
noTone(buzzerPin); // Turn off the tone
delay(200); // Delay between tones
tone(buzzerPin, 2000); // Third tone
delay(200); // Delay for the tone duration
noTone(buzzerPin); // Turn off the tone
delay(200); // Delay between tones
tone(buzzerPin, 2500); // Fourth tone
delay(200); // Delay for the tone duration
noTone(buzzerPin); // Turn off the tone
delay(200); // Delay between tones
tone(buzzerPin, 3000); // Fifth tone
delay(200); // Delay for the tone duration
noTone(buzzerPin); // Turn off the tone
delay(200); // Delay between tones
// Check if the button is pressed to turn off the alarm
if (digitalRead(buttonPin) == LOW) {
turnOffAlarm();
}
}
}
void turnOffAlarm() {
alarmTriggered = false; // Reset the alarm triggered flag
}
/* Uncomment if nana Esp32
void printLocalTime()
{
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("No time available (yet)");
return;
}
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
lcd.clear();
lcd.setCursor(0,0);
lcd.print(&timeinfo,"%b %d %Y");
lcd.setCursor(0,1);
lcd.print(&timeinfo,"%a, %H:%M:%S");
}
// Callback function (get's called when time adjusts via NTP)
void timeavailable(struct timeval *t)
{
Serial.println("Got time adjustment from NTP!");
printLocalTime();
}
*/