#define BLYNK_TEMPLATE_ID "TMPL6Ew6dFFTR"
#define BLYNK_TEMPLATE_NAME "DrChopper Notification"
#define BLYNK_PRINT Serial
#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;
*/
unsigned long lastButtonPressTime = 0;
const unsigned long BUTTON_TIMEOUT = 5 * 60 * 1000; // 5 minutes in milliseconds
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 buzzer = 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
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);
}
void loop() {
/* Uncomment if nana Esp32
Blynk.run();
printLocalTime();
*/
buttonState = digitalRead(buttonPin);
if(buttonState != lastButtonState){
if (buttonState == HIGH) {
Blynk.logEvent("pill_management"); //ako ra ni gi insert gikan sa tutorial
rotateServo();
}
delay(50);
}
lastButtonState = buttonState;
//Serial.println(buttonState);
if (Blynk.connected) {
if (buttonPin) {
lastButtonPressTime = millis(); // Update the last button press time
}
// Check if 5 minutes have passed since the last button press
if (millis() - lastButtonPressTime >= BUTTON_TIMEOUT) {
// Send notification
Blynk.logEvent("pill_has_not_been_dispensed");
// You might also trigger other actions here
}
}
}
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
}
/* 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();
}
*/