Skip to content

yking-ly/SmartMedicineBox

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

5 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿฅ Smart Medicine Box

Firebase Next.js ESP32

An AI-driven IoT healthcare solution that bridges the gap between medication adherence and remote clinical supervision through real-time monitoring, intelligent reminders, and caregiver connectivity.

Smart Medicine Box


๐Ÿ“‹ Table of Contents


๐ŸŒŸ Overview

Medication non-adherence affects nearly 50% of chronic disease patients globally, leading to treatment failures and preventable hospitalizations. The Smart Medicine Box addresses this critical healthcare challenge by combining:

  • IoT Hardware: ESP32-based device with push buttons, LCD display, and buzzer
  • Cloud Infrastructure: Firebase Realtime Database for secure data synchronization
  • AI Analytics: Grok-powered intelligent log analysis and conversational insights
  • Web Portal: Next.js caregiver dashboard for remote monitoring and communication

Key Statistics

  • โœ… 100% data synchronization accuracy
  • โœ… 100% successful reminder delivery (4-second latency)
  • โœ… 7 seconds average AI response time
  • โœ… 99% system reliability in experimental testing

โœจ Features

Patient-Side Features

  • ๐Ÿ”” Automated Medication Reminders: Visual (LCD) + audio (buzzer) alerts at scheduled times
  • โœ… Medicine Intake Confirmation: One-button confirmation with timestamp logging
  • ๐Ÿ†˜ Emergency Alert System: Instant emergency notification to caregivers
  • ๐Ÿ’ฌ Real-time Messaging: Receive messages from caregivers on the device screen
  • ๐Ÿ“Š Event Logging: Automatic cloud logging of all interactions

Caregiver Features

  • ๐Ÿ“ฑ Multi-Device Dashboard: Monitor multiple patients from a single interface
  • ๐Ÿ“ˆ Visual Analytics: Interactive charts showing adherence trends and patterns
  • โฐ Remote Reminder Scheduling: Configure medication schedules with custom repeat cycles
  • ๐Ÿ’ฌ Communication: Send messages directly to patient devices
  • ๐Ÿค– AI Assistant: Automated log summaries and conversational query interface
  • ๐Ÿ” Secure Authentication: Google OAuth with whitelisted access control

๐Ÿ— System Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                     CLOUD LAYER                             โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚         Firebase Realtime Database                    โ”‚  โ”‚
โ”‚  โ”‚  โ€ข /devices    โ€ข /events    โ€ข /messages              โ”‚  โ”‚
โ”‚  โ”‚  โ€ข /reminders  โ€ข /allowedDoctors                     โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
            โ–ฒ                              โ–ฒ
            โ”‚                              โ”‚
   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
   โ”‚  ESP32 DEVICE   โ”‚          โ”‚  WEB APPLICATION  โ”‚
   โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค          โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
   โ”‚ โ€ข 16ร—2 LCD      โ”‚          โ”‚ โ€ข Next.js 15.5.2  โ”‚
   โ”‚ โ€ข Push Buttons  โ”‚          โ”‚ โ€ข Material-UI v6  โ”‚
   โ”‚ โ€ข Piezo Buzzer  โ”‚          โ”‚ โ€ข Recharts        โ”‚
   โ”‚ โ€ข Wi-Fi Module  โ”‚          โ”‚ โ€ข AI Integration  โ”‚
   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ’ป Tech Stack

Hardware

  • Microcontroller: ESP32 DevKit v1
  • Display: 16ร—2 LCD with I2C backpack
  • Audio: Piezo buzzer (2-4 kHz)

Embedded Software

  • IDE: Arduino IDE 2.3.6
  • Libraries:
    • Firebase ESP32 Client
    • LiquidCrystal I2C
    • WiFi.h
    • time.h (NTP sync)

Web Application

  • Framework: Next.js 15.5.2 (React 19.1.0)
  • Styling: Material-UI v7.3.2, Tailwind CSS 4.1.14
  • Database: Firebase Realtime Database 12.4.0
  • Charts: Recharts 3.1.2
  • Carousel: Swiper 11.2.10

Cloud & AI

  • Backend: Firebase (Authentication, Realtime Database)
  • AI Model: Grok 4.1 Fast (via OpenRouter API)
  • Deployment: Vercel (Web), Firebase Hosting (Database)

๐Ÿš€ Installation

Prerequisites

  • Node.js 20.x or higher
  • Arduino IDE 2.x
  • Firebase project with Realtime Database enabled
  • OpenRouter API key

1. Hardware Setup

# Install Arduino ESP32 board support
# Tools โ†’ Board โ†’ Boards Manager โ†’ Search "esp32" โ†’ Install

# Install required libraries via Library Manager:
# - Firebase ESP32 Client by Mobizt
# - LiquidCrystal I2C by Frank de Brabander

2. Configure Firebase

  1. Create a Firebase project at console.firebase.google.com
  2. Enable Realtime Database
  3. Set database rules:
{
  "rules": {
    "devices": {
      "$deviceId": {
        ".read": "auth != null",
        ".write": "auth != null"
      }
    },
    "events": {
      "$deviceId": {
        ".read": "auth != null",
        ".write": "auth != null"
      }
    },
    "messages": {
      "$deviceId": {
        ".read": "auth != null",
        ".write": "auth != null"
      }
    },
    "reminders": {
      "$deviceId": {
        ".read": "auth != null",
        ".write": "auth != null"
      }
    },
    "allowedDoctors": {
      ".read": "auth != null"
    }
  }
}
  1. Add authorized caregivers to /allowedDoctors path (replace . with , in email)

3. ESP32 Configuration

  1. Open Arduino sketch
  2. Update Wi-Fi credentials:
#define WIFI_SSID "your-wifi-ssid"
#define WIFI_PASSWORD "your-wifi-password"
  1. Update Firebase credentials:
#define API_KEY "your-firebase-api-key"
#define DATABASE_URL "your-database-url" // e.g., https://project-id.firebaseio.com
  1. Upload to ESP32

4. Web Application Setup

# Clone repository
git clone https://github.com/yourusername/smart-medicine-box.git
cd smart-medicine-box

# Install dependencies
npm install

# Create .env.local file
cp .env.example .env.local

5. Configure Environment Variables

Edit .env.local:

OPENROUTER_API_KEY3=your_openrouter_api_key
NEXT_PUBLIC_FIREBASE_API_KEY=your_firebase_api_key
NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=your-project.firebaseapp.com
NEXT_PUBLIC_FIREBASE_DATABASE_URL=https://your-project.firebaseio.com
NEXT_PUBLIC_FIREBASE_PROJECT_ID=your-project-id
NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET=your-project.appspot.com
NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=your-sender-id
NEXT_PUBLIC_FIREBASE_APP_ID=your-app-id
NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID=G-XXXXXXXXXX

6. Run the Application

# Development mode
npm run dev

# Production build
npm run build
npm start

Open http://localhost:3000 in your browser.


๐Ÿ“– Usage

For Caregivers

  1. Login: Access /login and authenticate with Google
  2. Dashboard: View all registered devices
  3. Select Patient: Choose a device to monitor
  4. View Logs: See complete event history with timestamps
  5. Analyze Data: View charts and AI-generated summaries
  6. Send Messages: Communicate directly with the patient
  7. Set Reminders: Schedule medication alerts with custom repeat cycles

For Patients

  1. Power On: Connect the Smart Medicine Box to power
  2. Wait for Connection: LCD shows "Smart Medicine Box" โ†’ Wi-Fi connected
  3. Take Medicine: Press left button when taking medication
  4. Emergency: Press right button for urgent caregiver notification
  5. Follow Reminders: Respond to buzzer alerts and LCD messages

๐Ÿ“ Project Structure

smart-medicine-box/
โ”œโ”€โ”€ arduino/
โ”‚   โ””โ”€โ”€ smart_medicine_box.ino      # ESP32 firmware
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ app/
โ”‚   โ”‚   โ”œโ”€โ”€ api/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ ai-chat/route.ts    # Conversational AI endpoint
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ ai-summary/route.ts # Log summarization endpoint
โ”‚   โ”‚   โ”œโ”€โ”€ analyse/page.tsx        # Analytics dashboard
โ”‚   โ”‚   โ”œโ”€โ”€ dashboard/page.tsx      # Caregiver dashboard
โ”‚   โ”‚   โ”œโ”€โ”€ login/page.tsx          # Authentication page
โ”‚   โ”‚   โ”œโ”€โ”€ logs/page.tsx           # Event history viewer
โ”‚   โ”‚   โ”œโ”€โ”€ messages/page.tsx       # Messaging interface
โ”‚   โ”‚   โ”œโ”€โ”€ reminders/page.tsx      # Reminder scheduler
โ”‚   โ”‚   โ”œโ”€โ”€ layout.tsx              # Root layout
โ”‚   โ”‚   โ””โ”€โ”€ page.tsx                # Landing page
โ”‚   โ”œโ”€โ”€ components/
โ”‚   โ”‚   โ””โ”€โ”€ analyse/
โ”‚   โ”‚       โ””โ”€โ”€ AnalyseChat.tsx     # AI chat component
โ”‚   โ””โ”€โ”€ lib/
โ”‚       โ”œโ”€โ”€ firebase.ts             # Firebase configuration
โ”‚       โ””โ”€โ”€ theme.ts                # Material-UI theme
โ”œโ”€โ”€ .env.example                     # Environment template
โ”œโ”€โ”€ firebase.json                    # Firebase config
โ”œโ”€โ”€ next.config.ts                   # Next.js config
โ”œโ”€โ”€ package.json                     # Dependencies
โ””โ”€โ”€ README.md                        # This file

๐Ÿ”Œ API Endpoints

/api/ai-summary

Generates automated adherence summaries from patient logs.

Request:

{
  "timeRange": "Week",
  "medicineTaken": 42,
  "emergencies": 3,
  "dailyBreakdown": [...],
  "reminders": [...]
}

Response:

{
  "summary": "Patient showed 85% adherence this week with 3 emergency alerts..."
}

/api/ai-chat

Conversational AI for querying patient data.

Request:

{
  "messages": [
    {"role": "user", "content": "How many doses were missed this week?"}
  ],
  "stats": {...}
}

Response:

{
  "reply": "Based on the logs, the patient missed 6 doses this week..."
}

๐Ÿ”ฎ Future Enhancements

Short-term

  • SMS/Email notifications (Twilio/SendGrid integration)
  • Mobile application (React Native)
  • Battery backup system for power outages

Long-term

  • Automated pill dispensing mechanism
  • Weight sensors for pill counting
  • Fall detection and activity monitoring

๐Ÿ™ Acknowledgments

  • Firebase for cloud infrastructure and real-time database
  • OpenRouter for AI model access
  • Material-UI and Next.js communities for excellent documentation

โญ Star this repository if you find it helpful!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors