MAX30102,Esp8266 and ThingSpeak server code is not working

325 Views Asked by At

I wrote this code to read data from a MAX30102 Pulse Oximeter sensor, which measures heart rate and blood oxygen saturation (SpO2), and display it on serial. It then sends this data to the ThingSpeak platform using Wi-Fi communication with an ESP8266 module. but it is not working and I can't find the error,please help me ?????? the code :

#include <Wire.h>
#include "MAX30102_PulseOximeter.h"
#include <ESP8266WiFi.h>

// Define your WiFi credentials
const char *ssid = "myssid";
const char *password = "mypassword ";


// Define your ThingSpeak API key
const String apiKey = "myapikey";

// ThingSpeak Server parameters
const char *HOST = "api.thingspeak.com";
const int PORT = 80;

// Pulse oximeter variables
#define REPORTING_PERIOD_MS 1000 // 1 seconds
PulseOximeter pox;
uint32_t tsLastReport = 0;
float heartRate = 0;
float spo2 = 0;

// Function prototypes
void sendtoServer(float heartRate, float spo2);
void sendCommand(String command, int maxTime, char readReplay[]);

void setup()
{
  Serial.begin(9600); // Use Serial for debugging (make sure it matches your board's UART pins)

  // Connect to WiFi
  Serial.println("Connecting to WiFi...");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.println(".");
  }
  Serial.println("WiFi connected");

  // Initialize pulse oximeter
  Serial.println("Initializing pulse oximeter...");
  if (!pox.begin())
  {
    Serial.println("FAILED");
    while (1)
      ;
  }
  else
  {
    Serial.println("SUCCESS");
  }
}

void loop()
{
  // Update pulse oximeter data
  pox.update();

  // Get the latest heart rate and SpO2 values
  //heartRate = pox.getHeartRate();
  //spo2 = pox.getSpO2();
 // Send data to server
if (millis() - tsLastReport >= REPORTING_PERIOD_MS)
{
  // Print heart rate and SpO2 values to Serial Monitor
  Serial.print("Heart Rate: ");
  Serial.print(pox.getHeartRate());
  Serial.print(" bpm | SpO2: ");
  Serial.print( pox.getSpO2()); // Print SpO2 value with 2 decimal places
  Serial.println(" %");

  sendtoServer(pox.getHeartRate(), pox.getSpO2());
   tsLastReport = millis();
}  
}

void sendtoServer(float heartRate, float spo2)
{
  String getData = "GET /update?api_key=" + apiKey + "&field2=" + String(heartRate) + "&field3=" + String(spo2);

  // Open a connection to the server
  WiFiClient client;
  if (client.connect(HOST, PORT))
  {
    // Make an HTTP request
    client.println(getData);

    // Wait for a response (optional)
    while (client.available())
    {
      String response = client.readStringUntil('\r');
      Serial.println(response);
    }

    // Close the connection
    client.stop();
  }
  else
  {
    Serial.println("Connection to server failed.");
  }
}



NOTE: millis(): This function returns the number of milliseconds that have elapsed since the Arduino board started running its current program. It's a continuously increasing value, so it serves as a simple way to track time.

tsLastReport: This variable holds the value of the last time the data was sent to the ThingSpeak server. It is initialized to 0 at the beginning of the program.

REPORTING_PERIOD_MS: This constant specifies the reporting interval, i.e., the time in milliseconds between successive data reports to the server. For example, if REPORTING_PERIOD_MS is set to 1000 (1 second), the data will be sent to the server once every second.

-nothing is sent to the ThingSpeak -the results on serial are :

17:02:54.111 -> .
17:02:54.111 -> .
17:02:54.111 -> WiFi connected
17:02:54.111 -> Initializing pulse oximeter...
17:02:54.112 -> SUCCESS
17:03:34.692 -> Heart Rate: 0.00 bpm | SpO2: 0 %
17:03:39.311 -> Heart Rate: 0.00 bpm | SpO2: 0 %
17:03:40.707 -> Heart Rate: 0.00 bpm | SpO2: 0 %
17:03:42.207 -> Heart Rate: 0.00 bpm | SpO2: 0 %
17:03:43.781 -> Heart Rate: 0.00 bpm | SpO2: 0 %
17:03:45.239 -> Heart Rate: 0.00 bpm | SpO2: 0 %
17:03:48.976 -> Heart Rate: 0.00 bpm | SpO2: 0 %
17:03:50.812 -> Heart Rate: 0.00 bpm | SpO2: 0 %
0

There are 0 best solutions below