Serial communication (arduino to nodemcu) and max30100 problem

Hello,
I'm using max30100 pule oximeter sensor with temperature sensor ds18b20 with arduino nano. I want it to send these data (heartrate, spO2 , and temperature ) to nodemcu. Then I want to publish these data on thingspeak. How can I read these separately and store in variables in order to publish on cloud?

Note : I'm using arduino nano because I tried the same code on nodemcu it did not work properly.

#include <Wire.h>
#include "MAX30100_PulseOximeter.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SoftwareSerial.h>

#define REPORTING_PERIOD_MS     3000
#define ONE_WIRE_BUS 2
String str;
String str1;
String str2;

void(* resetFunc) (void) = 0;
int t=0;
float h,s,t1; 
PulseOximeter pox;
uint32_t tsLastReport = 0;

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
SoftwareSerial  espSerial(5, 6);//rx,tx

void onBeatDetected()
{
    Serial.println("Beat!");
   
}
 
void setup()
{
    Serial.begin(115200);
    espSerial.begin(115200);
    Serial.print("Initializing pulse oximeter..");
    sensors.begin();
    // Initialize the PulseOximeter instance
    // Failures are generally due to an improper I2C wiring, missing power supply
    // or wrong target chip
    if (!pox.begin()) {
        Serial.println("FAILED");
        for(;;);
    } else {
        Serial.println("SUCCESS");
    }
     pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
 
   
    pox.setOnBeatDetectedCallback(onBeatDetected);
  
   
}




void loop()
{
    // Make sure to call update as fast as possible

       
    
  pox.update();
    if (millis() - tsLastReport > REPORTING_PERIOD_MS) {

        h=pox.getHeartRate();
        s=pox.getSpO2();
        
        Serial.print("Heart rate:");
        Serial.print(h);
        Serial.print("bpm / SpO2:");
        Serial.print(s);
        Serial.println("%");
        

         sensors.requestTemperatures();
      // Fetch the temperature in degrees Celsius for device index:
        float tempC = sensors.getTempCByIndex(0); // the index 0 refers to the first device
        Serial.print("Temperature: ");
        Serial.println(tempC);
        Serial.print(" C");
    
        str =String("coming from arduino: ")+String("Heartrate")+String(h);
        espSerial.println(str);
        str1 = String("SpO2= ")+String(s);
        espSerial.println(str1);
        str2 = String("temp=")+String(tempC);
        espSerial.println(str2);
         tsLastReport = millis();

         t++;
        if(t>8){
          resetFunc();
        }
 
}


}

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation of your ask).

1 Like

What does the improperly posted code actually do? How is that different from what you want?

I would suggest that you try again to make the code work on the NodeMCU by itself. Post the code that did not work, tell us what the problem(s) were.

In any case, please post a schematic of your wiring.

Software serial will not work at 115200 baud (no matter what the reference says). 38400 is the fastest that I have been able to get SoftwareSerial to work reliably.

This the code for nodemcu, Output always shows zero for both parameters (heartrate and spO2), when connected to wifi and thingspeak. Wiring is simple SDA of max30100 to sda of nodemcu, same for scl, Vin of sensor to 3.3 v, ground to ground. Even though I excluded the temperature sensor , output is always zero.

If I don't connect it to the wifi/thingspeak it works.

#include <Wire.h>
#include "MAX30100_PulseOximeter.h"
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <ThingSpeak.h>


 
#define REPORTING_PERIOD_MS     1000


float h,s;

PulseOximeter pox;
uint32_t tsLastReport = 0;
WiFiClient client;

long myChannelNumber = xxxxxxx;
const char myWriteAPIKey[] = "xxxxxxxxxxxx";


void onBeatDetected()
{
    Serial.println("Beat!");
  
}
 
void setup()
{
    Serial.begin(9600);
    Serial.print("Initializing pulse oximeter..");
    WiFi.begin("ssid", "xxxxxxxxx");
    while(WiFi.status() != WL_CONNECTED)
  {
    delay(1000);
    Serial.print("..");
  }
  Serial.println();
  Serial.println("NodeMCU is connected!");
  Serial.println(WiFi.localIP());
  
  ThingSpeak.begin(client);
  
    Serial.print("Initializing pulse oximeter..");
    
    // Initialize the PulseOximeter instance
    // Failures are generally due to an improper I2C wiring, missing power supply
    // or wrong target chip
    if (!pox.begin()) {
        Serial.println("FAILED");
        for(;;);
    } else {
        Serial.println("SUCCESS");
    }
     pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
 
    // Register a callback for the beat detection
    pox.setOnBeatDetectedCallback(onBeatDetected);

  


}




void loop(

    pox.update();
    if (millis() - tsLastReport > REPORTING_PERIOD_MS) {

        h=pox.getHeartRate();
        s=pox.getSpO2();
        
        Serial.print("Heart rate:");
        Serial.print(h);
        Serial.print("bpm / SpO2:");
        Serial.print(s);
        Serial.println("%");

        tsLastReport = millis();

 
  ThingSpeak.writeField(myChannelNumber, 1, h, myWriteAPIKey);
  ThingSpeak.writeField(myChannelNumber, 2, s, myWriteAPIKey);
        
    }

}

This code does work on nodemcu,



#include <Wire.h>
#include "MAX30100_PulseOximeter.h"




 
#define REPORTING_PERIOD_MS     1000



float h,s; 
PulseOximeter pox;
uint32_t tsLastReport = 0;



void onBeatDetected()
{
    Serial.println("Beat!");
   
}
 
void setup()
{
    Serial.begin(9600);
    Serial.print("Initializing pulse oximeter..");

    // Initialize the PulseOximeter instance
    // Failures are generally due to an improper I2C wiring, missing power supply
    // or wrong target chip
    if (!pox.begin()) {
        Serial.println("FAILED");
        for(;;);
    } else {
        Serial.println("SUCCESS");
    }
     pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
 
    // Register a callback for the beat detection
    pox.setOnBeatDetectedCallback(onBeatDetected);
  
   
}




void loop()
{
    // Make sure to call update as fast as possible

       
    
  pox.update();
    if (millis() - tsLastReport > REPORTING_PERIOD_MS) {

        h=pox.getHeartRate();
        s=pox.getSpO2();
        
        Serial.print("Heart rate:");
        Serial.print(h);
        Serial.print("bpm / SpO2:");
        Serial.print(s);
        Serial.println("%");
        

     
         tsLastReport = millis();

       
 
}


}

    

Try the code in this project Arduino to Arduino via Serial for send data to an ESP8266 for display on a web page.

Although if you can get it all running on the nodemcu all the better.

Can you successfully send something other than the MAX30100 data to ThingSpeak?

yeah on cayenne iot platform... but as soon I added temperature sensor it stoped working .....