MAX30100 and ESP8266 NodeMCU Send to Database Error

This is my final year project and for some reason, the max30100 sensor defaults to static values once the HTTP post request has been performed.

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <Wire.h>
#include "MAX30100_PulseOximeter.h"
#include <SimpleTimer.h>

#define REPORTING_PERIOD_MS 1000

const char* ssid = "SDsouza";
const char* password = "dsouza@8104045917";
const char* serverName = "http://10.0.0.13/post-sensor-data.php";

String systolic_pressure;
String diastolic_pressure;
String pulse_rate;
String temperature_body;
String BPM;
String SpO2;

PulseOximeter pox;
uint32_t tsLastReport = 0;

SimpleTimer timer;

String sensor_data;
bool Sr;
String machineid = "MR1";
String room_number= "A1";
String bed_number= "1";

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(16,OUTPUT);
  WiFi.begin(ssid , password);
  Serial.print("Connecting to SSID:");
  Serial.print(ssid);
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(500);
  }
  Serial.println();
  Serial.print("Connected Successfully!!");
  Serial.println(WiFi.localIP());

  if(!pox.begin())
  {
    Serial.println("Failed");
    for(;;);
  }
  else
  {
    Serial.println("Pox Success");
  }
  pox.setOnBeatDetectedCallback(onBeatDetected);
  //pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
  timer.setInterval(1000L , getsendata);
}

void loop() {
  // put your main code here, to run repeatedly:
  pox.update();
  if(millis() - tsLastReport > REPORTING_PERIOD_MS)
  {
    BPM = pox.getHeartRate();
    SpO2 = pox.getSpO2();
    Serial.print("Bpm: ");
    Serial.println(BPM);
    Serial.print("SpO2: ");
    Serial.println(SpO2);
    tsLastReport = millis();
  }
  timer.run();
}

void onBeatDetected()
{
  Serial.println("Beat Detected");
}

void getsendata()
{
  while (Serial.available())
  {
    sensor_data = Serial.readString();
    Sr = true;
    
    int firstcommaindex = sensor_data.indexOf(",");
    int secondcommaindex = sensor_data.indexOf(",", firstcommaindex + 1);
    int thirdcommaindex = sensor_data.indexOf(",", secondcommaindex + 1);
    int fourthcommaindex = sensor_data.indexOf(",", thirdcommaindex + 1);

    systolic_pressure = sensor_data.substring(0, firstcommaindex);
    diastolic_pressure = sensor_data.substring(firstcommaindex + 1, secondcommaindex);
    pulse_rate = sensor_data.substring(secondcommaindex + 1, thirdcommaindex);
    temperature_body = sensor_data.substring(thirdcommaindex + 1);
    
    HTTPClient http;
    http.begin(serverName);
    http.addHeader("Content-Type" , "application/x-www-form-urlencoded");
    String httpRequestData = "machine_identifier=" + machineid + "&temp=" + temperature_body + "&hr=" + pulse_rate + "&sys_pressure=" + systolic_pressure + "&dias_pressure=" + diastolic_pressure + "&oxy_lvl=" + SpO2 + "&room_number=" + room_number + "&bed_number=" + bed_number + "";
    Serial.print("HTTP Request Data: ");
    Serial.println(httpRequestData);
    int httpResponseCode = http.POST(httpRequestData);

    if(httpResponseCode > 0)
    {
      Serial.print("HTTP Response Code: ");
      Serial.println(httpResponseCode);
    }
    else
    {
      Serial.print("Error Code: ");
      Serial.println(httpResponseCode);
    }
    http.end();
    
  }
}

The arduino mega is sending a string with the variable values set to 0 at startup. The MAX30100 sensor is doing its job of taking the readings however, as soon as the HTTP Post Request is done, the oximeter then defaults to it's static values and refuses to detect further readings.

Output:

This is before the HTTP Post request is done

Once the http post request on arduino startup is done, or if i am taking readings from arduino after startup, the max30100 oximeter stops working and refuses to take further readings or update the values further.

The data is sent to mysql db where i will bbe fetching it on the website using php js and html css. This issue of the oximeter has been bothering me since so many days now and my final year project is nearing. Could anyone please guide me as to how to solve this problem?

Here is the library im using for the MAX30100

I note you are writing to Serial in several functions which may be interrupting or running concurrently - can Serial support this? possibly can corrupt the stack and cause problems?
I would only write to Serial in loop() or functions called by loop() - if other functions wish to output information put data into volatile variables to be outputted in loop()

Well. When the system starts up, the max30100 is able to read the oxygen level properly. However, as soon as an http post request is made to my server, once that completes, the max30100 stops taking readings. The serial string is coming from the Arduino mega to the nodemcu. The nodemcu does the job of extracting the data and doing http post to my server locally. After this post request is when the max30100 stops working.

Well. Finally. Figured it out. After countless hours of googling and reading the issues on the github library, i just had to insert one line. resetFifo to reset the module after http post and re enable it to take readings normally.
Updated code:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <Wire.h>
#include "MAX30100.h"
#include "MAX30100_PulseOximeter.h"

#define REPORTING_PERIOD_MS 1000

MAX30100 maxim;
PulseOximeter pox;

const char* ssid = "SDsouza";
const char* password = "dsouza@8104045917";
const char* serverName = "http://10.0.0.13/post-sensor-data.php";

String systolic_pressure;
String diastolic_pressure;
String pulse_rate;
String temperature_body;
String BPM;
String SpO2;
    
uint32_t tsLastReport = 0;

String sensor_data;
bool Sr;
String machineid = "MR1";
String room_number= "A1";
String bed_number= "1";

HTTPClient http;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(16,OUTPUT);
  WiFi.begin(ssid , password);
  Serial.print("Connecting to SSID:");
  Serial.print(ssid);
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(500);
  }
  Serial.println();
  Serial.print("Connected Successfully!!");
  Serial.println(WiFi.localIP());
  if(!pox.begin())
  {
    Serial.print("Failed");
    for(;;);
  }
  else
  {
    Serial.println("Success");
  }
  pox.setIRLedCurrent(MAX30100_LED_CURR_50MA);
  http.begin(serverName);
}

void loop() {
  // put your main code here, to run repeatedly:
    pox.update();
    if(millis() - tsLastReport > REPORTING_PERIOD_MS)
    {
      BPM = pox.getHeartRate();
      SpO2 = pox.getSpO2();
      Serial.print("Bpm: ");
      Serial.println(BPM);
      Serial.print("SpO2: ");
      Serial.println(SpO2);
      tsLastReport = millis();
    }
    while (Serial.available())
    {
      sensor_data = Serial.readString();
      Sr = true;
    
      int firstcommaindex = sensor_data.indexOf(",");
      int secondcommaindex = sensor_data.indexOf(",", firstcommaindex + 1);
      int thirdcommaindex = sensor_data.indexOf(",", secondcommaindex + 1);
      int fourthcommaindex = sensor_data.indexOf(",", thirdcommaindex + 1);

      systolic_pressure = sensor_data.substring(0, firstcommaindex);
      diastolic_pressure = sensor_data.substring(firstcommaindex + 1, secondcommaindex);
      pulse_rate = sensor_data.substring(secondcommaindex + 1, thirdcommaindex);
      temperature_body = sensor_data.substring(thirdcommaindex + 1);

      String httpRequestData = "machine_identifier=" + machineid + "&temp=" + temperature_body + "&hr=" + pulse_rate + "&sys_pressure=" + systolic_pressure + "&dias_pressure=" + diastolic_pressure + "&oxy_lvl=" + SpO2 + "&room_number=" + room_number + "&bed_number=" + bed_number + "";
      Serial.println(httpRequestData);
      http.addHeader("Content-Type" , "application/x-www-form-urlencoded");
      Serial.print("HTTP Request Data: ");
      Serial.println(httpRequestData);
      http.POST(httpRequestData);

      maxim.resetFifo();
    }
}

This code works flawlessly and i can now serial read from arduino, read from the oximeter and http post it to the database normally and again repeat these steps without any errors.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.