Monday, 20 July 2026

AI-Based Smart Medicine Reminder and Health Monitoring System

Yes. Below is a PHP-file-based project structure for the complete AI-Based Smart Medicine Reminder and Health Monitoring System. Because the complete system is large, it is best organized into multiple PHP files rather than putting everything into one extremely large file. 1. Complete PHP Project Folder Structure smart_medicine_system/ │ ├── config/ │ ├── database.php │ ├── config.php │ └── security.php │ ├── api/ │ ├── esp32_data.php │ ├── medicine_reminder.php │ ├── medicine_confirm.php │ ├── health_data.php │ ├── power_data.php │ ├── ai_analysis.php │ └── telegram_alert.php │ ├── admin/ │ ├── login.php │ ├── dashboard.php │ ├── add_medicine.php │ ├── edit_medicine.php │ ├── delete_medicine.php │ └── logout.php │ ├── dashboard/ │ ├── index.php │ ├── health_chart.php │ ├── medicine_status.php │ ├── power_prediction.php │ └── alerts.php │ ├── includes/ │ ├── header.php │ ├── footer.php │ ├── navbar.php │ └── auth.php │ ├── n8n/ │ └── workflow.json │ ├── database/ │ └── smart_medicine.sql │ ├── index.php ├── README.php └── style.css 2. Database SQL File database/smart_medicine.sql 3. Database Connection config/database.php connect_error) { die( "Database Connection Failed: " . $conn->connect_error ); } $conn->set_charset("utf8mb4"); ?> 4. Main Configuration File config/config.php 5. Security File config/security.php 6. Main Home Page index.php AI Smart Medicine System

AI-Based Smart Medicine Reminder and Health Monitoring System

ESP32 + IoT + AI Agent + n8n + Telegram + Google Sheets + ThingSpeak

Admin Login

Open IoT Dashboard
7. Admin Login admin/login.php prepare($sql); $stmt->bind_param( "s", $username ); $stmt->execute(); $result = $stmt->get_result(); if ( $result->num_rows > 0 ) { $user = $result->fetch_assoc(); if ( password_verify( $password, $user["password"] ) ) { $_SESSION[ "admin_logged_in" ] = true; $_SESSION["username"] = $user["username"]; header( "Location: dashboard.php" ); exit; } } $message = "Invalid username or password"; } ?> Admin Login

Admin Login





8. Admin Dashboard admin/dashboard.php Admin Dashboard

AI Smart Medicine Admin Dashboard

Add Medicine

Health Monitoring Dashboard

Logout 9. Add Medicine admin/add_medicine.php prepare($sql); $stmt->bind_param( "sss", $medicine_name, $dosage, $reminder_time ); if ($stmt->execute()) { $message = "Medicine added successfully"; } } ?> Add Medicine

Add Medicine Schedule










10. ESP32 API Endpoint api/esp32_data.php This file receives data from ESP32. "error", "message" => "Invalid JSON data" ] ); exit; } $device_id = $data["device_id"] ?? ""; $temperature = $data["temperature"] ?? 0; $heart_rate = $data["heart_rate"] ?? 0; $spo2 = $data["spo2"] ?? 0; $humidity = $data["humidity"] ?? 0; $battery_voltage = $data["battery_voltage"] ?? 0; $power_consumption = $data["power_consumption"] ?? 0; $sql = "INSERT INTO health_data ( device_id, temperature, heart_rate, spo2, humidity, battery_voltage, power_consumption ) VALUES (?, ?, ?, ?, ?, ?, ?)"; $stmt = $conn->prepare($sql); $stmt->bind_param( "sdddddd", $device_id, $temperature, $heart_rate, $spo2, $humidity, $battery_voltage, $power_consumption ); if ($stmt->execute()) { echo json_encode( [ "status" => "success", "message" => "Health data saved" ] ); } else { echo json_encode( [ "status" => "error", "message" => "Database error" ] ); } ?> 11. Medicine Reminder API api/medicine_reminder.php prepare($sql); $stmt->bind_param( "ss", $current_time, $current_time ); $stmt->execute(); $result = $stmt->get_result(); $medicines = []; while ( $row = $result->fetch_assoc() ) { $medicines[] = $row; } echo json_encode( [ "status" => "success", "current_time" => $current_time, "medicines" => $medicines ] ); ?> 12. Medicine Confirmation API api/medicine_confirm.php prepare($sql); $stmt->bind_param( "iss", $medicine_id, $medicine_name, $status ); if ($stmt->execute()) { echo json_encode( [ "status" => "success", "message" => "Medicine confirmation saved" ] ); } ?> 13. Health Data API api/health_data.php query($sql); $data = $result->fetch_assoc(); echo json_encode( [ "status" => "success", "data" => $data ] ); ?> 14. PHP Health Dashboard dashboard/index.php query($sql); $data = $result->fetch_assoc(); ?> Smart Health Dashboard

AI Smart Medicine Health Dashboard


Latest Health Data

Temperature: °C

Heart Rate: BPM

SpO2: %

Humidity: %

Battery Voltage: V

Power Consumption: W

Last Updated:

15. AI Analysis PHP File api/ai_analysis.php query($sql); $data = $result->fetch_assoc(); $temperature = $data["temperature"]; $heart_rate = $data["heart_rate"]; $spo2 = $data["spo2"]; $status = "NORMAL"; $message = "Health parameters are within normal monitoring limits."; if ( $temperature > 38 ) { $status = "WARNING"; $message = "High temperature detected."; } if ( $spo2 > 0 && $spo2 < 94 ) { $status = "WARNING"; $message = "Low oxygen saturation detected."; } if ( $heart_rate > 120 || ( $heart_rate > 0 && $heart_rate < 50 ) ) { $status = "WARNING"; $message = "Abnormal heart rate detected."; } echo json_encode( [ "status" => $status, "message" => $message, "temperature" => $temperature, "heart_rate" => $heart_rate, "spo2" => $spo2 ] ); ?> These thresholds are only example monitoring rules and should not be treated as medical diagnosis. 16. Telegram Alert PHP File api/telegram_alert.php TELEGRAM_CHAT_ID, "text" => $message ]; $ch = curl_init($url); curl_setopt( $ch, CURLOPT_POST, true ); curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_data ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); $response = curl_exec($ch); curl_close($ch); echo $response; ?> 17. Power Consumption PHP File api/power_data.php prepare($sql); $stmt->bind_param( "dddd", $voltage, $current_amp, $power_watt, $battery_percentage ); $stmt->execute(); echo json_encode( [ "status" => "success", "voltage" => $voltage, "current" => $current_amp, "power" => $power_watt, "battery" => $battery_percentage ] ); ?> 18. Power Prediction PHP File dashboard/power_prediction.php = DATE_SUB( NOW(), INTERVAL 24 HOUR )"; $result = $conn->query($sql); $row = $result->fetch_assoc(); $average_power = $row["average_power"]; if ( $average_power > 2 ) { $prediction = "HIGH POWER CONSUMPTION"; } else if ( $average_power > 1 ) { $prediction = "MEDIUM POWER CONSUMPTION"; } else { $prediction = "LOW POWER CONSUMPTION"; } ?> Power Prediction

AI Power Consumption Prediction

Average Power: Watts

Prediction:

19. Alerts Dashboard dashboard/alerts.php query($sql); ?> Health Alerts

System Alerts

fetch_assoc() ) { ?>
Type Message Level Date
20. PHP-to-n8n Webhook Integration api/send_to_n8n.php $_POST["device_id"] ?? "MEDESP32_001", "temperature" => $_POST["temperature"] ?? 0, "heart_rate" => $_POST["heart_rate"] ?? 0, "spo2" => $_POST["spo2"] ?? 0, "medicine_status" => $_POST["medicine_status"] ?? "PENDING" ]; $ch = curl_init( N8N_WEBHOOK_URL ); curl_setopt( $ch, CURLOPT_POST, true ); curl_setopt( $ch, CURLOPT_HTTPHEADER, [ "Content-Type: application/json" ] ); curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data) ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); $response = curl_exec($ch); curl_close($ch); echo $response; ?> 21. Google Sheets Integration Through n8n The PHP system sends JSON to n8n: "MEDESP32_001", "temperature" => 36.8, "heart_rate" => 78, "spo2" => 97, "medicine_status" => "TAKEN" ]; ?> The n8n flow is: PHP / ESP32 | v Webhook | v Data Processing | v AI Analysis | v Google Sheets 22. n8n Workflow JSON File n8n/workflow.json { "name": "Smart Medicine AI Automation", "nodes": [ { "parameters": { "httpMethod": "POST", "path": "medicine-data", "responseMode": "onReceived" }, "name": "ESP32 Webhook", "type": "n8n-nodes-base.webhook", "typeVersion": 2, "position": [ 200, 300 ] }, { "parameters": { "jsCode": "const data = $json.body || $json;\n\nlet status = 'NORMAL';\nlet message = 'Health readings are normal.';\n\nif (data.temperature > 38) {\n status = 'WARNING';\n message = 'High temperature detected.';\n}\n\nif (data.spo2 && data.spo2 < 94) {\n status = 'WARNING';\n message = 'Low SpO2 detected.';\n}\n\nif (data.heart_rate && (data.heart_rate < 50 || data.heart_rate > 120)) {\n status = 'WARNING';\n message = 'Abnormal heart rate detected.';\n}\n\nreturn [{json: {...data, status, message}}];" }, "name": "AI Health Analysis", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 450, 300 ] }, { "parameters": { "conditions": { "string": [ { "value1": "={{$json.status}}", "operation": "equal", "value2": "WARNING" } ] } }, "name": "Alert Decision", "type": "n8n-nodes-base.if", "typeVersion": 2, "position": [ 700, 300 ] }, { "parameters": { "chatId": "YOUR_CHAT_ID", "text": "🚨 SMART MEDICINE ALERT\n\n{{$json.message}}" }, "name": "Telegram Alert", "type": "n8n-nodes-base.telegram", "typeVersion": 1, "position": [ 950, 200 ] }, { "parameters": {}, "name": "Google Sheets", "type": "n8n-nodes-base.googleSheets", "typeVersion": 4, "position": [ 950, 400 ] } ], "connections": { "ESP32 Webhook": { "main": [ [ { "node": "AI Health Analysis", "type": "main", "index": 0 } ] ] }, "AI Health Analysis": { "main": [ [ { "node": "Alert Decision", "type": "main", "index": 0 } ] ] }, "Alert Decision": { "main": [ [ { "node": "Telegram Alert", "type": "main", "index": 0 } ], [ { "node": "Google Sheets", "type": "main", "index": 0 } ] ] } } } 23. PHP Flowchart Representation README.php 24. Important Architecture ┌──────────────────────────┐ │ ESP32 │ │ │ │ RTC + Sensors + Buzzer │ │ Servo + OLED + Wi-Fi │ └────────────┬─────────────┘ │ ▼ ┌──────────────────────────┐ │ PHP API │ │ esp32_data.php │ └────────────┬─────────────┘ │ ▼ ┌──────────────────────────┐ │ MySQL Database │ └────────────┬─────────────┘ │ ▼ ┌──────────────────────────┐ │ PHP Dashboard │ └──────────────────────────┘ │ ▼ ┌──────────────────────────┐ │ n8n │ │ AI Automation │ └────────────┬─────────────┘ │ ┌─────┼──────┐ ▼ ▼ ▼ Telegram Google ThingSpeak Alert Sheets Cloud 25. Final Deployment Steps 1. Install XAMPP 2. Start Apache 3. Start MySQL 4. Create smart_medicine database 5. Import smart_medicine.sql 6. Copy project folder to: C:/xampp/htdocs/ 7. Configure database.php 8. Configure config.php 9. Configure Telegram Bot Token 10. Configure n8n Webhook URL 11. Configure ThingSpeak API Key 12. Upload ESP32 Code 13. Connect Sensors 14. Test Medicine Reminder 15. Test Health Monitoring 16. Test Telegram Alert 17. Test Google Sheets 18. Test ThingSpeak 19. Open PHP Dashboard Final Project Execution ESP32 ↓ Medicine Reminder ↓ Health Sensors ↓ Wi-Fi ↓ PHP API ↓ MySQL Database ↓ n8n Automation ↓ AI Agent ↓ Telegram Text Alert ↓ Telegram Voice Alert ↓ Google Sheets ↓ ThingSpeak Cloud ↓ PHP Web Dashboard This gives you the complete PHP-based web application structure, database design, REST APIs, dashboard, AI analysis logic, n8n integration, Telegram alert system, power-monitoring module, and cloud-monitoring architecture for the project.

No comments:

Post a Comment