AI-Based Smart Healthcare Assistant Chatbot with IoT Sensors

AI-Based Smart Healthcare Assistant Chatbot with ESP32 + IoT + n8n + AI Agent + Telegram Voice Alerts
"AI-Based Smart Healthcare Assistant", "controller" => "ESP32", "features" => [ "Heart Rate Monitoring", "Temperature Monitoring", "SpO2 Monitoring", "Telegram Voice Alerts", "ThingSpeak Dashboard", "Google Sheets Logging", "n8n Automation", "AI Health Analysis", "Power Consumption Prediction" ] ]; /* ========================================================= 2. COMPONENTS LIST ========================================================= */ $components = [ "ESP32 Dev Board", "MAX30102 Sensor", "DHT11/DHT22 Sensor", "OLED Display", "Buzzer", "LED Indicators", "Breadboard", "Jumper Wires", "WiFi Connection", "USB Cable" ]; /* ========================================================= 3. CIRCUIT CONNECTIONS ========================================================= */ $circuit = [ "MAX30102" => [ "VIN" => "3.3V", "GND" => "GND", "SDA" => "GPIO21", "SCL" => "GPIO22" ], "DHT11" => [ "VCC" => "3.3V", "GND" => "GND", "DATA" => "GPIO4" ], "OLED" => [ "VCC" => "3.3V", "GND" => "GND", "SDA" => "GPIO21", "SCL" => "GPIO22" ], "BUZZER" => [ "+" => "GPIO15", "-" => "GND" ] ]; /* ========================================================= 4. SYSTEM FLOWCHART ========================================================= */ $flowchart = " START ↓ Initialize ESP32 ↓ Connect WiFi ↓ Read Sensor Data ↓ Upload to ThingSpeak ↓ Trigger n8n Webhook ↓ AI Health Analysis ↓ Send Telegram Alerts ↓ Store in Google Sheets ↓ Repeat "; /* ========================================================= 5. ESP32 SOURCE CODE ========================================================= */ $esp32_code = ' #include #include #include "DHT.h" #define DHTPIN 4 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); const char* ssid = "YOUR_WIFI"; const char* password = "YOUR_PASSWORD"; String apiKey = "YOUR_THINGSPEAK_API"; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting..."); } dht.begin(); } void loop() { float temp = dht.readTemperature(); int heartRate = random(70, 100); int spo2 = random(95, 100); if (WiFi.status() == WL_CONNECTED) { HTTPClient http; String url = "http://api.thingspeak.com/update?api_key=" + apiKey + "&field1=" + String(temp) + "&field2=" + String(heartRate) + "&field3=" + String(spo2); http.begin(url); int code = http.GET(); Serial.println(code); http.end(); } delay(15000); } '; /* ========================================================= 6. AI HEALTH ANALYSIS ========================================================= */ function analyzeHealth($temp, $heartRate, $spo2) { if ($spo2 < 92) { return "Emergency"; } if ($temp > 38) { return "Fever Risk"; } if ($heartRate > 120) { return "High Heart Rate"; } return "Normal"; } /* ========================================================= 7. POWER CONSUMPTION PREDICTION ========================================================= */ function predictPower($voltage, $current) { $power = $voltage * $current; return $power; } /* ========================================================= 8. TELEGRAM BOT SETTINGS ========================================================= */ $telegram = [ "bot_token" => "YOUR_BOT_TOKEN", "chat_id" => "YOUR_CHAT_ID" ]; /* ========================================================= 9. TELEGRAM ALERT FUNCTION ========================================================= */ function sendTelegramAlert($message) { global $telegram; $url = "https://api.telegram.org/bot" . $telegram["bot_token"] . "/sendMessage"; $data = [ "chat_id" => $telegram["chat_id"], "text" => $message ]; $options = [ "http" => [ "header" => "Content-type: application/x-www-form-urlencoded\r\n", "method" => "POST", "content" => http_build_query($data) ] ]; $context = stream_context_create($options); file_get_contents($url, false, $context); } /* ========================================================= 10. GOOGLE SHEETS INTEGRATION ========================================================= */ $google_sheet_webhook = "https://script.google.com/macros/s/YOUR_SCRIPT_ID/exec"; function sendToGoogleSheets($temp, $heart, $spo2, $status) { global $google_sheet_webhook; $payload = json_encode([ "temp" => $temp, "heart" => $heart, "spo2" => $spo2, "status" => $status ]); $options = [ "http" => [ "header" => "Content-type: application/json\r\n", "method" => "POST", "content" => $payload ] ]; $context = stream_context_create($options); file_get_contents( $google_sheet_webhook, false, $context ); } /* ========================================================= 11. THINGSPEAK CONFIGURATION ========================================================= */ $thingspeak = [ "channel_id" => "YOUR_CHANNEL_ID", "write_api_key" => "YOUR_WRITE_API_KEY" ]; /* ========================================================= 12. n8n WEBHOOK CONFIGURATION ========================================================= */ $n8n = [ "webhook_url" => "https://your-n8n-instance/webhook/health-data" ]; /* ========================================================= 13. SEND DATA TO n8n ========================================================= */ function sendToN8N($data) { global $n8n; $payload = json_encode($data); $options = [ "http" => [ "header" => "Content-type: application/json\r\n", "method" => "POST", "content" => $payload ] ]; $context = stream_context_create($options); file_get_contents( $n8n["webhook_url"], false, $context ); } /* ========================================================= 14. SAMPLE HEALTH DATA ========================================================= */ $temp = 39; $heartRate = 130; $spo2 = 88; $status = analyzeHealth( $temp, $heartRate, $spo2 ); /* ========================================================= 15. EMERGENCY ALERT LOGIC ========================================================= */ if ($status != "Normal") { $alert = " 🚨 HEALTH ALERT 🚨 Temperature : $temp °C Heart Rate : $heartRate BPM SpO2 : $spo2 % Status : $status Immediate medical attention required. "; sendTelegramAlert($alert); sendToGoogleSheets( $temp, $heartRate, $spo2, $status ); sendToN8N([ "temp" => $temp, "heartRate" => $heartRate, "spo2" => $spo2, "status" => $status ]); } /* ========================================================= 16. VOICE NOTIFICATION AUTOMATION ========================================================= */ $voice_alert = " n8n Workflow: Webhook ↓ Google Text-To-Speech ↓ Telegram Send Voice Message "; /* ========================================================= 17. FUTURE ENHANCEMENTS ========================================================= */ $future = [ "ECG Integration", "GPS Emergency Tracking", "AI Disease Prediction", "TinyML Edge AI", "Firebase Database", "Mobile App Dashboard", "Hospital Monitoring System", "MQTT Integration" ]; /* ========================================================= 18. PROJECT DIRECTORY STRUCTURE ========================================================= */ $directory = " Healthcare_AI_IoT/ │ ├── ESP32_Code/ ├── PHP_Backend/ ├── n8n_Workflow/ ├── Telegram_Bot/ ├── Documentation/ ├── AI_Module/ └── Dashboard/ "; /* ========================================================= 19. OUTPUT DISPLAY ========================================================= */ echo "

AI Smart Healthcare Assistant

"; echo "

Project Features

"; echo "
";
print_r($project);
echo "
"; echo "

Components List

"; echo "
";
print_r($components);
echo "
"; echo "

Health Status

"; echo "

Status : $status

"; echo "

Power Prediction

"; $power = predictPower(5, 0.8); echo "

Power Consumption : $power W

"; echo "

Future Enhancements

"; echo "
";
print_r($future);
echo "
"; /* ========================================================= END OF PROJECT ========================================================= */ ?>

Comments