0% found this document useful (0 votes)
9 views11 pages

IoT Health & Environment Monitor Code

The document outlines the final integrated code for an IoT-based health and environment monitor using an ESP32 microcontroller and Blynk cloud platform. It includes configurations for various health and environmental sensors, alert mechanisms, and data transmission to Blynk. The code is structured to read sensor data, process it, and trigger alerts based on predefined thresholds for health and environmental parameters.

Uploaded by

nagetideepu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views11 pages

IoT Health & Environment Monitor Code

The document outlines the final integrated code for an IoT-based health and environment monitor using an ESP32 microcontroller and Blynk cloud platform. It includes configurations for various health and environmental sensors, alert mechanisms, and data transmission to Blynk. The code is structured to read sensor data, process it, and trigger alerts based on predefined thresholds for health and environmental parameters.

Uploaded by

nagetideepu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PROJECTCODE

// =================================================================
// Final Integrated Code: IoT-Based Health & Environment Monitor
// Board: ESP32
// Cloud Platform: Blynk
// =================================================================

// --- Device Credentials (from your Blynk Console) ---


#define BLYNK_TEMPLATE_ID "TMPL3Kxr8rqZh"
#define BLYNK_TEMPLATE_NAME "Health and Environment Monitor"
#define BLYNK_AUTH_TOKEN "WGhDU4WHs6hcHxaQFbwPn0WTL9hVT-tC"

#define BLYNK_PRINT Serial

// --- Include All Required Libraries ---


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Wire.h>
#include "MAX30100_PulseOximeter.h" // For MAX30100
#include <Adafruit_MPU6050.h> // For MPU6050
#include <Adafruit_Sensor.h> // For MPU6050 & DHT
#include <DHT.h> // For DHT22

// --- Wi-Fi Configuration ---


char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "realme 6";
char pass[] = "1234567890";

// --- Pin Definitions ---


// Health Sensors
#define LM35_PIN 34 // Analog pin for LM35 temperature sensor
#define GSR_PIN 35 // Analog pin for GSR sensor
#define AD8232_PIN 32 // Analog pin for AD8232 ECG sensor output
// Environmental Sensors
#define DHT_PIN 27 // Digital pin for DHT22
#define MQ2_PIN 33 // Analog pin for MQ-2 Gas sensor
#define LDR_PIN 36 // Analog pin for LDR
// Alerting Components
#define BUZZER_PIN 2 // Digital pin for the buzzer
#define ALERT_LED_PIN 25 // Digital pin for the Alert LED indicator

// --- Sensor & Object Initialization ---


#define DHT_TYPE DHT22
PulseOximeter pox;
DHT dht(DHT_PIN, DHT_TYPE);
Adafruit_MPU6050 mpu;
BlynkTimer timer;

// --- Alert Thresholds ---


#define HR_HIGH_THRESHOLD 100 // BPM
#define HR_LOW_THRESHOLD 60 // BPM
#define TEMP_HIGH_THRESHOLD 38.0 // Celsius
#define GSR_HIGH_THRESHOLD 70 // Stress level percentage
#define GAS_HIGH_THRESHOLD 60 // Gas level percentage
#define FALL_THRESHOLD 25.0 // MPU6050 fall detection threshold

// This function runs every second to read all sensors and send data
void sendSensorData() {
// --- Read Health Sensors ---
[Link]();
float heartRate = [Link]();
float spO2 = pox.getSpO2();
int tempAnalogVal = analogRead(LM35_PIN);
float bodyTemperature = (tempAnalogVal / 4095.0) * 3300 / 10.0;
int gsrAnalogVal = analogRead(GSR_PIN);
int gsrLevel = map(gsrAnalogVal, 0, 4095, 0, 100);
int ecgSignal = analogRead(AD8232_PIN);

// --- Read Environmental Sensors ---


float airHumidity = [Link]();
float airTemperature = [Link]();
int gasAnalogVal = analogRead(MQ2_PIN);
int gasLevel = map(gasAnalogVal, 0, 4095, 0, 100);
int ldrAnalogVal = analogRead(LDR_PIN);
int lightLevel = map(ldrAnalogVal, 0, 4095, 100, 0); // Invert for intuitive reading (dark=0,
bright=100)

// MPU6050 Fall Detection


sensors_event_t a, g, temp;
[Link](&a, &g, &temp);
float totalAccel = sqrt(pow([Link].x, 2) + pow([Link].y, 2) +
pow([Link].z, 2));
bool motionDetected = (totalAccel > FALL_THRESHOLD);

// --- Print values to Serial Monitor for debugging ---


[Link]("HR:%.1f|SpO2:%.1f|BTemp:%.1f|GSR:%d|ECG:%d|ATemp:%.1f|Hum:%.1f|Gas:
%d|Light:%d|Fall:%d\n",
heartRate, spO2, bodyTemperature, gsrLevel, ecgSignal, airTemperature, airHumidity,
gasLevel, lightLevel, motionDetected);

// --- Send data to Blynk Virtual Pins ---


[Link](V0, heartRate);
[Link](V1, spO2);
[Link](V2, bodyTemperature);
[Link](V4, ecgSignal);
[Link](V5, gsrLevel);
[Link](V6, airTemperature);
[Link](V7, airHumidity);
[Link](V8, gasLevel);
[Link](V9, lightLevel);
[Link](V10, motionDetected ? 255 : 0); // Send 255 for ON, 0 for OFF to an LED
widget

// --- Expanded Alert Logic ---


if (heartRate > HR_HIGH_THRESHOLD || heartRate < HR_LOW_THRESHOLD ||
bodyTemperature > TEMP_HIGH_THRESHOLD || gsrLevel > GSR_HIGH_THRESHOLD ||
gasLevel > GAS_HIGH_THRESHOLD || motionDetected) {
digitalWrite(BUZZER_PIN, HIGH);
digitalWrite(ALERT_LED_PIN, HIGH);
[Link](V3, 1); // Turn ON virtual LED in Blynk
[Link]("health_alert", "Critical Health or Environment Alert!");
} else {
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(ALERT_LED_PIN, LOW);
[Link](V3, 0); // Turn OFF virtual LED
}
}

void setup() {
[Link](115200);
[Link]("Initializing Full Health & Environment Monitor...");

// Initialize Pins
pinMode(BUZZER_PIN, OUTPUT);
pinMode(ALERT_LED_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(ALERT_LED_PIN, LOW);

// Initialize Sensors
[Link]();
if (![Link]()) {
[Link]("ERROR: Failed to initialize pulse oximeter");
while (1);
}
if (![Link]()) {
[Link]("ERROR: Failed to find MPU6050 chip");
while (1);
}
[Link](MPU6050_RANGE_8_G);
[Link](MPU6050_RANGE_500_DEG);
[Link](MPU6050_BAND_21_HZ);

// Connect to Wi-Fi and Blynk


[Link](auth, ssid, pass, "[Link]", 80);

// Setup a timer to call sendSensorData() every second (1000L ms)


[Link](1000L, sendSensorData);
}

void loop() {
[Link]();
[Link]();
}

Category Component Purpose in Project

Microcontroller ESP32 The main brain of the project; it reads all sensors,
Category Component Purpose in Project

processes data, and connects to Wi-Fi to send


information to Blynk.

Measures heart rate (BPM) and blood oxygen


Health Sensors MAX30100
saturation (SpO2).

A 6-axis accelerometer & gyroscope used for fall


MPU6050
detection.

LM35 An analog sensor for measuring body temperature.

Measures Galvanic Skin Response to estimate stress


GSR Sensor
levels.

An ECG (Electrocardiogram) module to read the


AD8232
heart's electrical signal.

A digital sensor for measuring ambient air


Environmental Sensors DHT22
temperature and humidity.

An analog sensor to detect smoke and flammable


MQ-2
gases.

A Light Dependent Resistor used to measure ambient


LDR
light levels.

0.96" I2C OLED Provides a small screen for displaying real-time


Output Devices
Display sensor data locally.

Creates an audible alarm when any sensor reading


Piezo Buzzer
crosses a critical threshold.

LED Provides a visual alert light that turns on during an


Category Component Purpose in Project

alarm condition.

Hart rate
#include <Wire.h>
#include "MAX30100_PulseOximeter.h" // Use the library by OXullo Intersecans

// Define the custom I2C pins you are using


#define I2C_SDA 25
#define I2C_SCL 26

// Create a PulseOximeter object from the library


PulseOximeter pox;

// This variable stores the time of the last report


uint32_t tsLastReport = 0;

// This is a "callback" function. It gets called automatically by the library when a heartbeat
is detected.
void onBeatDetected()
{
// We can leave this empty or print something simple like "Beat!"
// [Link]("Beat!");
}

void setup()
{
[Link](115200);

// Initialize I2C communication on your custom pins


[Link](I2C_SDA, I2C_SCL);

[Link]("Initializing pulse oximeter...");

// Initialize the sensor


if (![Link]()) {
[Link]("FAILED");
// If the sensor fails to start, halt the program
for(;;);
} else {
[Link]("SUCCESS");
}

// --- THIS IS THE FIX ---


// Set the LED brightness to a lower value.
// This can significantly improve readings for some people.
[Link](MAX30100_LED_CURR_7_6MA);

// Register our onBeatDetected function to be called when a beat is found


[Link](onBeatDetected);
}

void loop()
{
// The library needs to be updated as fast as possible to collect data
[Link]();

// We will print the calculated heart rate and SpO2 values once per second.
if (millis() - tsLastReport > 1000) {
[Link]("Heart rate: ");
[Link]([Link]());
[Link]("bpm / SpO2: ");
[Link](pox.getSpO2());
[Link]("%");

// Update the timestamp of the last report


tsLastReport = millis();
}
}

You might also like