#include <DHT.h>
#define DHTPIN 4 // DHT22 data pin connected to GPIO 4
#define DHTTYPE DHT22 // DHT 22 (AM2302)
#define BUZZER_PIN 18 // Buzzer connected to GPIO 18
#define BUTTON_PIN 27 // Pushbutton connected to GPIO 27
DHT dht(DHTPIN, DHTTYPE);
// Thresholds for emergency (similar to ML model but simplified)
#define CRITICAL_HEART_RATE 100 // Above 100 bpm is critical
#define CRITICAL_TEMP 38.0 // Above 38°C is critical
void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP); // Button pin as input with pull-up
pinMode(BUZZER_PIN, OUTPUT); // Buzzer pin as output
dht.begin(); // Initialize DHT22 sensor
Serial.println("System initialized...");
}
void loop() {
// Check if the button is pressed
bool buttonPressed = (digitalRead(BUTTON_PIN) == LOW);
if (buttonPressed) {
Serial.println("Button pressed, checking conditions...");
// Collect sensor data
float temp = dht.readTemperature();
if (isnan(temp)) {
Serial.println("Failed to read from DHT22 sensor!");
return;
}
int heartRate = random(60, 130); // Simulate heart rate data for testing
// Log data to serial monitor
Serial.print("Heart Rate: ");
Serial.println(heartRate);
Serial.print("Body Temperature: ");
Serial.println(temp);
// Decision-making based on thresholds (replacing ML model)
bool emergency = checkEmergency(heartRate, temp);
if (emergency) {
// If it's an emergency, activate buzzer and display warning
Serial.println("Emergency detected! Activating buzzer...");
activateEmergencySequence(heartRate, temp);
} else {
// If conditions are normal
Serial.println("I am looking, you are all good.");
}
}
delay(100);
}
bool checkEmergency(int heartRate, float temperature) {
// Simple threshold-based check
if (heartRate > CRITICAL_HEART_RATE || temperature > CRITICAL_TEMP) {
return true; // It's an emergency
}
return false; // Normal condition
}
void activateEmergencySequence(int heartRate, float temperature) {
// Activate buzzer and display emergency information
digitalWrite(BUZZER_PIN, HIGH);
delay(10000); // Activate buzzer for 10 seconds (simulation)
digitalWrite(BUZZER_PIN, LOW);
// Log the emergency message (simulate notification)
Serial.println("Emergency Message:");
Serial.println("Hello user, your family member Mr. Ansh has stucked in an emergency situation..");
Serial.println("Location: Gota, Ahmedabad"); // Static location for simulation
Serial.print("Heart rate: ");
Serial.print(heartRate);
Serial.println(" bpm");
Serial.print("Skin temperature: ");
Serial.print(temperature);
Serial.println(" °C");
}