The hardware I am using is ESP32 with MAX30102, MLX90614 and MPU6050 sensors and Using Blynk to show my data from the sensors. The issue I am facing is when I ran the code with all three sensors only the MAX30102 sensor doesn't show data.
I tried troubleshooting with only two sensors at a time and when the MAX30102 sensor is involve I only can get the other sensors data. I tried troubleshooting with only the MAX30102 sensor only and it works I don't understand what is going on.
#include <WiFi.h> // include the Arduino WiFi library
#include <Wire.h>
#include <math.h>
#include <MAX30105.h>
#include <heartRate.h>
#include <Adafruit_MLX90614.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Blynk.h>
#include <BlynkSimpleEsp32.h>
#define BLYNK_PRINT Serial
Adafruit_MLX90614 mlx = Adafruit_MLX90614(); //0x5A
MAX30105 particleSensor;
char auth[] = "0GUsnEJQJCDHF2HsLzYscYcDgXoe4KFg"; // Blynk code
const char *ssid = "Get your own Wifi"; // WiFi access name
const char *pass = "w1frjoyf00bar"; // WiFi password
const byte RATE_SIZE = 4; // Settings for Max 30102
byte rates[RATE_SIZE];
byte rateSpot = 0;
long lastBeat = 0;
float beatsPerMinute = 0;
float beatAvg = 0;
const int MPU_addr = 0x68; // I2C address of the MPU-6050
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
float ax = 0, ay = 0, az = 0, gx = 0, gy = 0, gz = 0;
void setup() {
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
particleSensor.begin(Wire, I2C_SPEED_FAST);
particleSensor.setup();
particleSensor.setPulseAmplitudeRed(0x0A); // Turn Red LED to low to indicate sensor is running
mlx.begin(); // Start MLX sensor
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("."); // print … till not connected
}
Serial.println("");
Serial.println("WiFi connected");
}
void mpu_read() {
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr, 14, true); // request a total of 14 registers
AcX = Wire.read() << 8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY = Wire.read() << 8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ = Wire.read() << 8 | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp = Wire.read() << 8 | Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX = Wire.read() << 8 | Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY = Wire.read() << 8 | Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ = Wire.read() << 8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
}
void loop() {
Blynk.run();
long irValue = particleSensor.getIR();
if (checkForBeat(irValue) == true) //If a heart beat is detected
{
Serial.println("BPM");
Serial.println(beatAvg);
Serial.println();
delay(100);
long delta = millis() - lastBeat; //Measure duration between two beats
lastBeat = millis();
beatsPerMinute = 60 / (delta / 1000.0); //Calculating the BPM
if (beatsPerMinute < 255 && beatsPerMinute > 20) //To calculate the average we strore some values (4) then do some math to calculate the average
{
rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
rateSpot %= RATE_SIZE; //Wrap variable
beatAvg = 0; //Take average of readings
for (byte x = 0 ; x < RATE_SIZE ; x++)
beatAvg += rates[x];
beatAvg /= RATE_SIZE;
}
// Serial.println("");
// Serial.println("BPM: ");
// Serial.println(beatAvg);
}
delay(100);
mpu_read();
ax = (AcX + 1704) / 16384.00;
ay = (AcY - 412) / 16384.00;
az = (AcZ + 1765) / 16384.00;
gx = (GyX + 82) / 131.07;
gy = (GyY + 6) / 131.07;
gz = (GyZ + 32) / 131.07;
// calculating Amplitute vactor for 3 axis
float Raw_Amp = pow(pow(ax, 2) + pow(ay, 2) + pow(az, 2), 0.5);
int Amp = Raw_Amp * 10; // Mulitiplied by 10 bcz values are between 0 to 1
float roll = 57.2883514 * atan(ay / sqrt(ax * ax + az * az));
Serial.print("\nAMP:");
Serial.print(Amp);
Serial.print(" ");
Serial.print(", Roll:");
Serial.println(roll);
Serial.print("BPM = ");
Serial.print(beatsPerMinute);
Serial.print(", Avg BPM = ");
Serial.print(beatAvg);
Serial.println();
Serial.print("Ambient Temp = ");
Serial.print(mlx.readAmbientTempC());
Serial.print("\n Object Temp = ");
Serial.print(mlx.readObjectTempC());
Serial.print(" ");
Serial.print(" ");
alertBPM();
Serial.print(" ");
alertTemp();
Serial.print("\n-----------------------------------------------------------------");
Blynk.virtualWrite(V1, beatAvg);
Blynk.virtualWrite(V2, mlx.readObjectTempC());
}
void alertBPM() { // Alert Trigger for Heart rate
// BPM alert
if (beatAvg >= 60 && beatAvg <= 110) {
Serial.println("Heart rate normal");
}
if (beatAvg < 60 && beatAvg > 110) {
Serial.println("Heart Rate Abnormal"); // Need to change the HR to hhr
Blynk.logEvent("HR", "Heart Rate Abnormal");
}
}
void alertTemp() { // Alert Trigger for Body Temp
// Body and ambient temp alert
if (mlx.readObjectTempC() >= 35 && mlx.readObjectTempC() <= 39) {
Serial.println("Body temp normal");
}
if (mlx.readObjectTempC() < 35 && mlx.readObjectTempC() > 39 ) {
Serial.println("Body temp Abnormal");
Blynk.logEvent("BT", "Body temp Abnormal"); // Need to change BT to hbt
}
}
The output is this
-----------------------------------------------------------------
AMP:11 , Roll:-2.12
BPM = 0.00, Avg BPM = 0.00
Ambient Temp = 30.75
Object Temp = 36.07 Body temp normal
-----------------------------------------------------------------
AMP:10 , Roll:3.90
BPM = 0.00, Avg BPM = 0.00
Ambient Temp = 30.77
Object Temp = 36.07 Body temp normal
-----------------------------------------------------------------
AMP:11 , Roll:-0.15
BPM = 0.00, Avg BPM = 0.00
Ambient Temp = 30.79
Object Temp = 35.99 Body temp normal
-----------------------------------------------------------------