#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL5A5IKGiWu"
#define BLYNK_TEMPLATE_NAME "IOT Based Health Monitoring System Using ESP32"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
char auth[] = "dPwaOMahpx1KvmMqtQQOg15ZGHRzQDhd";
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// DHT22
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// LDR simulates heart rate
const int ldrPin = 34;
// Fall button
const int fallPin = 0;
// LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
float tempC;
int heartRate;
int spo2;
bool fallDetected;
// thresholds
float TEMP_HIGH = 37.5;
int HR_LOW = 60;
int HR_HIGH = 100;
int SPO2_LOW = 92;
void setup() {
Serial.begin(115200);
pinMode(fallPin, INPUT_PULLUP);
dht.begin();
lcd.init();
lcd.backlight();
Blynk.begin(auth, ssid, pass);
}
void loop() {
Blynk.run();
// Read temperature
tempC = dht.readTemperature();
if (isnan(tempC)) tempC = 36.5;
// LDR → heart rate simulation
int lightLevel = analogRead(ldrPin);
heartRate = map(lightLevel, 0, 4095, 60, 110);
// SpO2 simulation
spo2 = random(90, 100);
// Fall detection
fallDetected = (digitalRead(fallPin) == LOW);
Serial.print("Temp: "); Serial.print(tempC);
Serial.print(" HR: "); Serial.print(heartRate);
Serial.print(" SpO2: "); Serial.print(spo2);
Serial.print(" FALL: "); Serial.println(fallDetected);
// Send to Blynk
Blynk.virtualWrite(V1, tempC);
Blynk.virtualWrite(V2, heartRate);
Blynk.virtualWrite(V3, spo2);
Blynk.virtualWrite(V4, fallDetected ? 1 : 0);
// LCD Display
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(tempC);
lcd.print(" H:");
lcd.print(heartRate);
lcd.setCursor(0, 1);
lcd.print("O2:");
lcd.print(spo2);
lcd.print("% F:");
lcd.print(fallDetected ? "YES" : "NO ");
// Alert conditions
if (tempC > TEMP_HIGH || heartRate < HR_LOW || heartRate > HR_HIGH ||
spo2 < SPO2_LOW || fallDetected)
{
Blynk.logEvent("alert", "⚠️ Patient abnormal vitals or FALL detected!");
}
delay(1500);
}