0% found this document useful (0 votes)
32 views4 pages

IoT Health Monitoring with ESP32 Code

This code is for an ESP32-based health monitoring system that collects data from various sensors including MAX30102 for heart rate, DS18B20 for temperature, MPU6050 for acceleration, and AD8232 for ECG. It connects to WiFi and sends the collected data in JSON format to a Firebase database. The setup initializes the sensors and the loop continuously reads sensor data and updates Firebase every second.

Uploaded by

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

IoT Health Monitoring with ESP32 Code

This code is for an ESP32-based health monitoring system that collects data from various sensors including MAX30102 for heart rate, DS18B20 for temperature, MPU6050 for acceleration, and AD8232 for ECG. It connects to WiFi and sends the collected data in JSON format to a Firebase database. The setup initializes the sensors and the loop continuously reads sensor data and updates Firebase every second.

Uploaded by

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

COMPLETE CODE

#include <WiFi.h>

#include <HTTPClient.h>

#include <Wire.h>

// MAX30102

#include "MAX30105.h"

#include "heartRate.h"

MAX30105 particleSensor;

// DS18B20

#include <OneWire.h>

#include <DallasTemperature.h>

#define ONE_WIRE_BUS 4

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

// MPU6050

#include <MPU6050.h>

MPU6050 mpu;

// AD8232 ECG (analog)

#define ECG_PIN 36 // GPIO36 is VP (ADC1_CH0)

// WiFi & Firebase

const char* ssid = "YOUR_SSID";

const char* password = "YOUR_PASSWORD";

const char* firebaseHost = "[Link]

const String firebasePath = "/[Link]"; // Must end with .json


void setup() {

// Initialize Serial Monitor

[Link](115200);

// WiFi connection setup

[Link](ssid, password);

[Link]("Connecting to WiFi");

while ([Link]() != WL_CONNECTED) {

delay(500);

[Link](".");

[Link]("\nWiFi connected");

// Initialize I2C communication

[Link](); // Default 100kHz speed on ESP32 (standard mode)

// MAX30102 Sensor setup

if (![Link]()) { // Corrected initialization (no parameters)

[Link]("MAX30102 not found. Check wiring!");

} else {

[Link]();

[Link](0x0A); // Set red LED brightness

[Link](0x0A); // Set IR LED brightness

// DS18B20 Sensor setup

[Link](); // Initialize DS18B20 temperature sensor

// MPU6050 Sensor setup

[Link](); // Initialize MPU6050 accelerometer/gyroscope sensor

}
void loop() {

// MAX30102 readings

long irValue = [Link]();

int bpm = 0;

if (checkForBeat(irValue)) {

static uint32_t lastBeat = 0;

uint32_t now = millis();

bpm = 60000 / (now - lastBeat);

lastBeat = now;

// DS18B20 temperature

[Link]();

float temperature = [Link](0);

// MPU6050 readings

[Link]();

float ax = [Link]();

float ay = [Link]();

float az = [Link]();

// AD8232 ECG

int ecgValue = analogRead(ECG_PIN);

// Create JSON

String json = "{";

json += "\"bpm\":" + String(bpm) + ",";

json += "\"ir\":" + String(irValue) + ",";

json += "\"temp\":" + String(temperature) + ",";

json += "\"ecg\":" + String(ecgValue) + ",";


json += "\"accel\":{";

json += "\"x\":" + String(ax) + ",";

json += "\"y\":" + String(ay) + ",";

json += "\"z\":" + String(az) + "}";

json += "}";

[Link]("Sending to Firebase: " + json);

// Send to Firebase

if ([Link]() == WL_CONNECTED) {

HTTPClient http;

[Link](firebaseHost + firebasePath);

[Link]("Content-Type", "application/json");

int httpResponseCode = [Link](json);

[Link]("Firebase response: "); [Link](httpResponseCode);

[Link]();

delay(1000); // 1s update

Common questions

Powered by AI

Beats per minute (bpm) is calculated by detecting peaks in IR values using checkForBeat(irValue). It computes bpm using 60000 divided by the difference in milliseconds since the last detected beat, stored in lastBeat. Challenges with this method include sensitivity to noise and motion artifacts, potentially leading to inaccurate bpm calculations .

The delay(1000) function pauses the loop for 1 second between each iteration, setting the data collection frequency. Modifying this value alters the update rate, with shorter delays increasing data frequency but potentially leading to higher WiFi and processor usage, while longer delays could smooth data variations but reduce real-time responsiveness .

The program connects to a WiFi network by calling WiFi.begin(ssid, password), where 'ssid' and 'password' are the network SSID and password, respectively. It checks the connection status in a loop, waiting until WiFi.status() returns WL_CONNECTED, indicating a successful connection. Monitoring WiFi status ensures that data transmission to Firebase is conducted only when connected to the network, preventing errors in data upload operations .

ECG values are acquired using analogRead(ECG_PIN), where ECG_PIN is defined as GPIO36. AnalogRead() is appropriate because it converts the analog voltage signal representing the ECG waveform from the sensor into a digital value for processing, essential for extracting meaningful cardiac metrics .

The MAX30102 particle sensor is initialized with particleSensor.begin(). Upon successful initialization, its LED brightness is set using setPulseAmplitudeRed() and setPulseAmplitudeIR() functions, configuring red and IR LED brightness to 0x0A. This setup is crucial for detecting pulse signals effectively .

Challenges include ensuring connection reliability, handling HTTP request failures, and preserving data integrity during transmission. Mitigation strategies involve implementing retries, using HTTPS for secure communication, buffering data to resend upon failure, and maintaining a checksum or digital signature in data packets to verify integrity upon reception .

The OneWire library facilitates communication over a single data line required by the DS18B20, while the DallasTemperature library provides a higher-level interface for interacting with the sensor. Using these libraries, sensors.begin() initializes the communication, and sensors.requestTemperatures() retrieves the temperature, which is accessed with getTempCByIndex(0).

Hardcoding credentials like WiFi SSID, password, and Firebase URLs poses security risks, as they could be extracted from the code, compromising network and data security. Best practices include storing such information securely using encrypted storage solutions or secure OTA updates to manage credentials dynamically, reducing vulnerabilities .

The MPU6050 data is processed using mpu.update(), which refreshes sensor data, followed by accessing ax, ay, and az via mpu.getAccX(), getAccY(), and getAccZ(). This data can be used in applications such as motion tracking, fall detection, and activity monitoring, providing context to physiological data like heart rate and temperature .

The JSON string is constructed to encode sensor data in a structured format, making it suitable for transmission over HTTP to Firebase, which expects data in JSON format. Constructing this string involves embedding various sensor readings, such as bpm, ir, temperature, ecgValue, and accelerometer data, into a structured JSON object, ensuring the data is organized and accessible upon retrieval .

You might also like