#include <Wire.h>
#include <WiFi.h>
#include <DHT.h>
#include <PubSubClient.h>
#include <LiquidCrystal_I2C.h>
// Define pin connections
#define DHT_PIN 14 // Pin where the DHT22 is connected
#define DHTTYPE DHT22 // DHT22 sensor type
#define BUZZER_PIN 25 // Pin where the buzzer is connected
#define RED_LED_PIN 26 // Pin where the red LED is connected
#define PULSE_SENSOR_PIN 34 // Pin where the pulse sensor is connected
#define MQTT_SERVER "broker.emqx.io"
#define MQTT_PORT 1883
const char *ssid = "Wokwi-GUEST";
const char *password = "";
#define MQTT_TOPIC_HR "/heartRate"
#define MQTT_TOPIC_TEMP "/tempValue"
#define MQTT_TOPIC_HUM "/humValue"
// Create instances of the sensors and MQTT client
DHT dht(DHT_PIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 for a 16 chars and 2 line display
WiFiClient espClient;
PubSubClient client(espClient);
// Variables for pulse sensor
int pulseValue = 0;
int threshold = 600; // Example threshold value, you might need to calibrate it
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize the sensors
dht.begin();
// Initialize the LCD
lcd.init();
lcd.backlight();
// Initialize the buzzer and LED pins
pinMode(BUZZER_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
// Initialize the pulse sensor pin
pinMode(PULSE_SENSOR_PIN, INPUT);
// Initialize WiFi and MQTT
connectToWiFi();
client.setServer(MQTT_SERVER, MQTT_PORT);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// Read temperature and humidity from DHT22 sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Read the pulse value from the pulse sensor
pulseValue = analogRead(PULSE_SENSOR_PIN);
// Display the temperature, humidity, and pulse rate on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Hum: ");
lcd.print(humidity);
lcd.print("%");
delay(2000); // Wait for 2 seconds
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pulse: ");
lcd.print(pulseValue);
// Check conditions for asthma warning
if (temperature >= 30.0 && humidity >= 70.0 && pulseValue > threshold) {
// Trigger the buzzer and red LED if conditions are met
digitalWrite(BUZZER_PIN, HIGH);
digitalWrite(RED_LED_PIN, HIGH);
lcd.setCursor(0, 1);
lcd.print("Patient Not Safe");
} else {
// Turn off the buzzer and red LED if conditions are not met
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(RED_LED_PIN, LOW);
lcd.setCursor(0, 1);
lcd.print("Patient Safe");
}
delay(2000); // Wait for 2 seconds before the next loop
// Publish sensor data to MQTT
client.publish(MQTT_TOPIC_HR, String(pulseValue).c_str());
client.publish(MQTT_TOPIC_TEMP, String(temperature).c_str());
client.publish(MQTT_TOPIC_HUM, String(humidity).c_str());
}
void connectToWiFi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void reconnect() {
while (!client.connected()) {
if (client.connect("esp32_health_monitor")) {
Serial.println("Connected to MQTT");
} else {
Serial.print("Failed, rc=");
Serial.print(client.state());
Serial.println(" Retrying in 5 seconds...");
delay(5000);
}
}
}