Inspiration

Watching our grandparents struggle with complex medication schedules was a wake-up call. Despite having multiple pill organizers and smartphone reminders, they would still miss doses or take the wrong medicines. The real problem hit home when a family member was hospitalized due to medication non-adherence, something that affects nearly 50% of chronic disease patients worldwide according to WHO.

We realized that existing solutions were either too complex (requiring smartphone literacy) or too passive (simple alarms that could be ignored). What elderly patients and their caregivers truly needed was a bridge—a system that combined the simplicity of a physical device with the intelligence of cloud connectivity and AI-powered insights.

The vision was clear: create a smart medicine box that doesn't just remind patients to take their medication, but actively logs adherence, enables real-time caregiver supervision, and provides intelligent analytics, all while being simple enough for anyone to use, regardless of their tech-savviness.

What it does

The Smart Medicine Box is an IoT-powered medication adherence system with three core components:

🔷 Patient Device (ESP32-based Hardware)

  • Displays medication reminders on a 16×2 LCD screen at scheduled times
  • Provides audio alerts through a piezo buzzer with distinct patterns for reminders vs. emergencies
  • Allows one button confirmation when medicine is taken (left button)
  • Includes an emergency alert button (right button) for urgent situations
  • Shows real-time messages sent by caregivers
  • Automatically logs every interaction with timestamp to the cloud

🔷 Caregiver Web Portal (Next.js Dashboard)

  • Multi-patient monitoring: View and manage multiple devices from one dashboard
  • Real-time event logs: Complete history of medicine intake and emergency alerts
  • Visual analytics: Interactive charts showing adherence trends, daily breakdowns, and patterns
  • Remote reminder scheduling: Set medication schedules with custom times, days, and repeat cycles
  • Messaging: Send messages directly to the patient's LCD screen
  • AI-powered insights: Automated summaries and conversational queries about patient adherence

🔷 AI Assistant (Grok Integration)

  • Analyzes patient logs and generates natural language summaries
  • Answers caregiver queries like "How many times did my mother miss her evening dose this month?"
  • Identifies concerning patterns and trends in medication adherence
  • Provides actionable recommendations for intervention

The system achieved 100% data synchronization accuracy and 100% reminder delivery rate with only 4 second latency in our testing.

How we built it

Hardware Layer:

  • Assembled the circuit using ESP32 DevKit v1 as the brain, chosen for its dual-core processor, built-in Wi-Fi, and low cost
  • Connected a 16×2 I2C LCD for visual feedback, reducing pin usage compared to parallel displays
  • Integrated two tactile push buttons with software debouncing for medicine confirmation and emergency alerts
  • Added a piezo buzzer with programmatic pattern generation (3 pulses for reminders, 20 pulses for emergencies)
  • Powered the system via USB (5V), with onboard regulation to 3.3V for stable operation

Embedded Firmware (Arduino/C++):

  • Implemented NTP time synchronization to ensure reminders trigger at exact scheduled times
  • Built a local reminder caching system that polls Firebase every 5 seconds for updates and refreshes the cache every 5 minutes
  • Created Firebase anonymous authentication for secure device-cloud communication
  • Developed intelligent message display logic with 30 minute timeouts for reminders and button-dismissible alerts
  • Added fail-safe mechanisms with retry logic for Wi-Fi disconnections and Firebase timeouts

Cloud Infrastructure (Firebase):

  • Designed a hierarchical NoSQL schema with four main branches:
    • /devices - Device metadata and last-seen timestamps
    • /events - Timestamped logs of all patient interactions
    • /messages - Bidirectional caregiver-patient communication
    • /reminders - Medication schedules with day-of-week flags
  • Configured security rules restricting devices to read/write only their own data
  • Implemented Google OAuth authentication with email whitelisting for caregiver access

Web Application (Next.js + Material-UI):

  • Built a responsive server-side rendered (SSR) dashboard using Next.js 15.5.2
  • Integrated Recharts for interactive time-series visualizations of adherence data
  • Implemented real-time listeners using Firebase SDK for instant log updates without polling
  • Designed a dark-themed UI optimized for clinical environments and long monitoring sessions
  • Created reusable React components for scalability across multiple pages

AI Integration (OpenRouter + Grok):

  • Developed two RESTful API routes:
    • /api/ai-summary - Batch processes logs and generates adherence summaries
    • /api/ai-chat - Handles conversational queries with context persistence
  • Injected today's date into system prompts to ensure accurate temporal reasoning
  • Implemented date normalization to convert various timestamp formats into consistent "DD Month YYYY" format
  • Achieved 7 second average response time through efficient prompt engineering

Challenges we ran into

🔴 Time Synchronization Hell: Early builds had reminders firing at random times because the ESP32's internal clock drifted without NTP sync. The solution required configuring configTime() with proper timezone offsets and implementing a 15-second wait for time sync on boot—without this, Firebase TLS connections would fail cryptically.

🔴 Firebase Polling vs. Real-time Dilemma: ESP32 doesn't support Firebase's real-time streaming SDK, forcing us to implement polling. We had to balance responsiveness (checking every second would overwhelm the device) with user experience. Our final 5-second polling interval with 5-minute cache refresh proved optimal through trial and error.

🔴 AI Date Hallucinations: Initially, Grok would invent dates or misinterpret relative time queries ("today", "this week"). We solved this by:

  1. Injecting the current date into every AI request's system prompt
  2. Pre-processing logs to normalize all timestamps to "DD Month YYYY" format
  3. Explicitly instructing the model to never guess dates

🔴 Button Debouncing Issues: Without debouncing, a single button press would register 5-10 times due to mechanical bounce. We implemented 50ms software debouncing and added "wait for release" logic to prevent accidental multi-triggers.

🔴 Authentication Complexity: Balancing security with ease of use was tricky. Doctors needed secure access, but devices couldn't handle OAuth flows. Our solution: anonymous auth for devices (using MAC address as ID) and whitelisted Google OAuth for caregivers with Firebase security rules enforcing device-level access isolation.

🔴 LCD Message Truncation: 16 characters per line is limiting. We built a scrolling text engine for longer messages and implemented smart truncation with ellipses for overflows.

Accomplishments that we're proud of

System Reliability: Achieving 100% data synchronization across hundreds of test events with zero data loss

Low Latency: Maintaining 4-second end-to-end latency from button press to caregiver notification—fast enough to feel instant

AI Accuracy: Our prompt engineering resulted in coherent, medically-relevant summaries with zero hallucinated facts in testing

Cost Efficiency: Building a complete IoT health solution for just ₹1,150 (~$14 USD) in hardware costs, making it accessible to low-income families

Code Quality: Writing production-grade firmware with proper error handling, retry logic, and graceful degradation when Firebase is unreachable

What we learned

Technical Skills:

  • IoT Architecture: Designing fault-tolerant embedded systems with cloud connectivity
  • Real-time Databases: Understanding NoSQL data modeling for time-series event logging
  • State Management: Building local caching systems on resource-constrained devices
  • AI Integration: Prompt engineering for medical domain accuracy and temporal reasoning
  • Full-stack Development: Connecting hardware, cloud, and web layers seamlessly

Practical Insights:

  • User-Centric Design: Elderly users need simple, tactile interfaces; touchscreens and apps often introduce friction
  • Reliability > Features: A device that works 100% of the time with basic features beats a feature-rich device that works 80% of the time
  • Offline Resilience: Always cache critical data locally; network failures shouldn't prevent core functionality

Healthcare Domain Knowledge:

  • Medication adherence is a behavioral problem and not just a reminder problem; patients need accountability
  • Caregivers value trend data over raw logs—"she took 85% of doses this week" is more useful than 100 individual timestamps
  • Emergency alerts need to be unmistakable; visual + audio + cloud notification ensures something gets through

What's next for SmartMedicineBox

Phase 1 (Next 3 Months):

  • 📱 Mobile App Development: Native iOS/Android app for caregivers on-the-go
  • 📧 SMS/Email Alerts: Integrate Twilio/SendGrid for critical emergency notifications
  • 🔋 Battery Backup: Add UPS module for 8-hour operation during power outages

Phase 2 (6-12 Months):

  • 💊 Automated Pill Dispensing: Servo-controlled dispenser that releases exact doses
  • ⚖️ Weight Sensors: Detect if pills are actually removed from compartments

Phase 3 (Long-term Vision):

  • 💼 Commercial Deployment: Launch as a SaaS platform with subscription model for clinics and senior care facilities

Research Extensions:

  • Publish findings in IEEE/ACM conferences
  • Develop SDK for third-party developers to build custom medication tracking apps

We believe this technology can genuinely save lives by turning medication adherence from a forgotten daily task into an actively monitored, AI-enhanced healthcare touchpoint. Our mission is to ensure no patient ever misses a critical dose again.

Built With

  • arduino-ide-2.3.6
  • c++
  • esp32client
  • espressif
  • firebase
  • firebase-rtdb
  • google-oauth-2.0
  • grok
  • html
  • javascript
  • liquidcrystal-i2c
  • material-ui-v7.3.2
  • nextjs
  • ntp
  • openrouter
  • react-19.1.0
  • recharts-3.1.2
  • swiper-11.2.10
  • tailwind-css-4.1.14
  • typescript
  • visual-studio-code
Share this project:

Updates