#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
#include <Blynk.h>
#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);
BlynkTimer timer;
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 buzzerState = 0;
int lastBuzzerState = 0;
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
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 buzzerState() {
/* Uncomment if nana Esp32
Blynk.run();
printLocalTime();
*/
buzzerState = digitalRead(buzzerPin);
if(buzzerState != lastBuzzerState){
if (buzzerState == HIGH) {
Blynk.logEvent("timed_pill_notification");
}
delay(50);
}
lastBuzzerState = buzzerState;
//Serial.println(buzzerState);
}
void buttonState() {
/* Uncomment if nana Esp32
Blynk.run();
printLocalTime();
*/
buttonState = digitalRead(buttonPin);
if(buttonState != lastButtonState){
if (buttonState == HIGH) {
rotateServo();
Blynk.logEvent("confirmed_pill_dispensed");
}
delay(50);
}
if(buttonState != lastButtonState); {
if (buttonState == LOW) {
Blynk.logEvent("pill_not_dispensed"); //mga after 5 minutes guro kay munotif siya di ko kibaw unsaon ang code ani huehue
}
delay(50);
}
else if(buttonState != lastButtonState); {
if (buttonState == HIGH) {
rotateServo();
Blynk.logEvent("pill_has_been_taken");
}
delay(50);
}
lastButtonState = buttonState;
//Serial.println(buttonState);
}
/* 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();
}
*/
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("HELL-O");
pinMode(buttonPin, INPUT);
myServo.attach(servoPin);
myServo.write(0);
timer.setInterval(2000L, rotateServo);
}
void loop() {
blynk.run();
timer.run();
}