Arduino Mega + Esp8266-01 + MAX30100

hello guys
this is my first post on this awesome community i have a single issue which is every time i connect to Blynk or i use the Blynk libraries the whole project keeps working but the MAX30100 stops reading BPM & SPO2 when i comment every line of the blynk library it works any suggestions
thank you


#include "MAX30100_PulseOximeter.h"
#define REPORTING_PERIOD_MS 1000
#include <MFRC522.h>
#include <SPI.h>
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#define RST_PIN         8
#define SS_PIN          53

char auth[] = "";
char ssid[] = "";
char pass[] = "";
#define EspSerial Serial1

MFRC522 mfrc522(SS_PIN, RST_PIN);

String read_rfid;
//

String ok_rfid1 = "3fc2ad29";
//String ok_rfid1 = "31c2ad29";

PulseOximeter pox;

uint32_t tsLastReport = 0;

#define REPORTING_PERIOD_MS 1000

int mic = 5;
int led1 = 3;
int led2 = 4;
int micValue;
int fsrSensor = A0;
int fsrValue;
int temperatureSensor = A1;
int tempValue ;
int tempInCelisus;
int count = 0;
void onBeatDetected()
{
  Serial.println("Beat!");
}
ESP8266 wifi(&EspSerial);
#define ESP8266_BAUD 115200
BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  //Blynk.virtualWrite(V5, millis() / 1000);
}

void setup() {
  // put your setup code here, to run once:

  Serial.begin(115200);
  SPI.begin();
  mfrc522.PCD_Init();
  pinMode(mic, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  digitalWrite(led2, HIGH);
  

  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8442);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
  Serial.print("Initializing pulse oximeter..");
  if (!pox.begin()) {
    Serial.println("FAILED");
    for (;;);
  } else {
    Serial.println("SUCCESS");
  }
  pox.setOnBeatDetectedCallback(onBeatDetected);


}
void dump_byte_array(byte * buffer, byte bufferSize) {
  read_rfid = "";
  for (byte i = 0; i < bufferSize; i++) {
    read_rfid = read_rfid + String(buffer[i], HEX);

  }
}

void loop() {

  pox.update();
  Blynk.run();
  timer.run();

  if (millis() - tsLastReport > REPORTING_PERIOD_MS) {

    Serial.print("Heart rate:");
    Serial.print(pox.getHeartRate());
    Serial.print("bpm / SpO2:");
    Serial.print(pox.getSpO2());
    Serial.println("%");
    Blynk.virtualWrite(V6, pox.getHeartRate());
    Blynk.virtualWrite(V7, pox.getSpO2());
    Blynk.virtualWrite(V8, tempInCelisus);

    if (fsrValue < 900 && fsrValue > 850)
    {
      digitalWrite(led1, HIGH);
      digitalWrite(led2, LOW);
    } else
    {
      digitalWrite(led1, LOW);
      digitalWrite(led2, HIGH);
    }

    tempInCelisus = tempValue * 0.4885;
    tsLastReport = millis();

  }
  micValue = digitalRead(mic);
  fsrValue = analogRead(fsrSensor);
  tempValue = analogRead(temperatureSensor);


  if ( ! mfrc522.PICC_IsNewCardPresent())
  {
    return;
  }


  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return;
  }

  dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);

  if (read_rfid == ok_rfid1)
  {
    // Blynk.notify("DR Enterd");
    digitalWrite(led2, LOW);
    digitalWrite(led1, HIGH);
    delay(500);
    digitalWrite(led1, LOW);
    delay(500);
    digitalWrite(led2, HIGH);
  } else {
    digitalWrite(led1, LOW);
    for (count = 0; count < 5; count++)
    {
      digitalWrite(led2, HIGH);
      delay(100);
      digitalWrite(led2, LOW);
      delay(100);

    }
    count = 0;
  }





}

@aliSamhat Your loop is quite loaded. Keep loop() simple having just blynk.run() and timer.run(). Move everything else into routines triggerred by timers.

1 Like

thanks for your reply mohan but do you mean to make functions and call them

Make them functions and call them using the timer.setInterval call.

1 Like

sorry im new to blynk can i have example please

void setup() {
  // put your setup code here, to run once:

  Serial.begin(115200);
  SPI.begin();
  mfrc522.PCD_Init();
  pinMode(mic, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  digitalWrite(led2, HIGH);
  

  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8442);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
  timer.setInterval(100L, report_update);

  Serial.print("Initializing pulse oximeter..");
  if (!pox.begin()) {
    Serial.println("FAILED");
    for (;;);
  } else {
    Serial.println("SUCCESS");
  }
  pox.setOnBeatDetectedCallback(onBeatDetected);


}
void dump_byte_array(byte * buffer, byte bufferSize) {
  read_rfid = "";
  for (byte i = 0; i < bufferSize; i++) {
    read_rfid = read_rfid + String(buffer[i], HEX);

  }
}

void loop() {
  Blynk.run();
  timer.run();
}

report_update()
{
  pox.update();
  if (millis() - tsLastReport &gt; REPORTING_PERIOD_MS) {

    Serial.print("Heart rate:");
    Serial.print(pox.getHeartRate());
    Serial.print("bpm / SpO2:");
    Serial.print(pox.getSpO2());
    Serial.println("%");
    Blynk.virtualWrite(V6, pox.getHeartRate());
    Blynk.virtualWrite(V7, pox.getSpO2());
    Blynk.virtualWrite(V8, tempInCelisus);

    if (fsrValue &lt; 900 && fsrValue &gt; 850)
    {
      digitalWrite(led1, HIGH);
      digitalWrite(led2, LOW);
    } else
    {
      digitalWrite(led1, LOW);
      digitalWrite(led2, HIGH);
    }

    tempInCelisus = tempValue * 0.4885;
    tsLastReport = millis();

  }
  micValue = digitalRead(mic);
  fsrValue = analogRead(fsrSensor);
  tempValue = analogRead(temperatureSensor);

  if ( ! mfrc522.PICC_IsNewCardPresent())
  {
    return;
  }

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return;
  }

  dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);

  if (read_rfid == ok_rfid1)
  {
    // Blynk.notify("DR Enterd");
    digitalWrite(led2, LOW);
    digitalWrite(led1, HIGH);
    delay(500);
    digitalWrite(led1, LOW);
    delay(500);
    digitalWrite(led2, HIGH);
  } else {
    digitalWrite(led1, LOW);
    for (count = 0; count &lt; 5; count++)
    {
      digitalWrite(led2, HIGH);
      delay(100);
      digitalWrite(led2, LOW);
      delay(100);

    }
    count = 0;
  }

}

This is just an example. Have not tested this. Please run it and test it to see if it does what you want.

thank you Mohan i have tried the code but still same issue as before

this is an image shows that the spo2 & heart rate still not calculating

What issue?? All I see is you dumped messy code in your first post. EDIt. sorry, I see it now… I will clean up the post :slight_smile:

@Gunner sorry but this is my first time here posting

regarding the issue is that when ever i add blynk code to the sketch the MAX30100 stops reading heart rate pulses and spo2

Blynk is basically a IoT library with GUI App, thus due the whole internet aspect of IoT, it needs to keep in relatively constant communication with the server. It is this need for timing that usually conflicts with “normal” use of dumpling all your code into the void loop() that bogs things down.

With Blynk (and any other IoT based setup I guess) you need to move all your sensor scanning and other functions out of the main loop and into their own independent routines, that can call and be called by timers or other processes… leaving the basic commands like Blynk.run() and timer.run() in the loop where they maintain order.

The Documents, Help Center and Example Builder (links all at the top of this page) are recommended places for new users to start reading and experimenting… you will learn more there then asking questions alone.

@Gunner thank you very much i have read the docs and did the same way using timer intervals but still didn’t succeeded

so i have done the same code with the sensor but with node MCU it worked fine with no issues so i compared the only difference is the <ESP8266_lib> library on arduino and <ESP8266WiFi.h> library on node MCU.

so is there any way to make MAX30100 work with ESP8266_lib.h thanks in advance

But there are other hardware differences between the Mega and the NodeMCU, the main one in this case being the much faster processor in the NodeMCU.

If it works within the smaller, lighter, cheaper, more powerful NodeMCU then why not stick with it?

Pete.

@PeteKnight what i meant that only the max30100 worked which the only thing that was not working with the mega 2560 so i searched a lot but with no success any suggestions regrding the max30100 with arduino would be helpful thank you

That sensor is rated for 3.3v… so probably not 5v tolerant, which on an Arduino Mega you are possibly getting 5v signal back on the i2C even if powering the module on 3.3v.

@Gunner but still it works without the blynk library and give the right readings

Sorry, I thought you were saying it now worked with everything BUT the Mega :stuck_out_tongue:

1 Like