UPDATE:
I manage to get it working by making TS write a function and call it from void setup() with time interval 15000.
Everything works but for 15sec , when my Setup uses the TS write function , everything freezes at the exact numbers they had at that time .
Please help i dont know whats causing that.
/*
Arduino-MAX30100 oximetry / heart rate integrated sensor library
Copyright (C) 2016 OXullo Intersecans <x@brainrapers.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Wire.h>
#include "ThingSpeak.h"
#include "MAX30100_PulseOximeter.h"
#include <WiFiNINA.h> /* wifi libaries to connect our device with BLYNK */
#include <BlynkSimpleWiFiNINA.h> /* libaries to connect our device with BLYNK */
#define BLYNK_PRINT Serial
#define REPORTING_PERIOD_MS 1000
#define BLYNK_MAX_SENDBYTES 256 // Default is 128
// PulseOximeter is the higher level interface to the sensor
// it offers:
// * beat detection reporting
// * heart rate calculation
// * SpO2 (oxidation level) calculation
PulseOximeter pox;
BlynkTimer timer;
WiFiClient client;
unsigned long myChannelNumber = ;
const char * myWriteAPIKey = "";
char auth[] = "";
// DONT STEAL THEM PLEASE
// xD
char ssid[] = "";
char pass[] = "";
uint32_t tsLastReport = 0;
//simple and smart function to ignore first readings which are false and then check if Heart rate or Spo2 levels are abnormal , we ll call it outside of the loop because the IoT server gonna get overflooded with DATA , so we ll call with interval Time
void sendMsg()
{
float h = pox.getHeartRate();
float o = pox.getSpO2();
if ( h >= 140 || h < 40 & h != 0)
{
Blynk.notify("Your BpM are in dangeours levels");
}
else if ( o<90 & o>80)
{
Blynk.notify("Your Spo2 is Quite Low .");
}
else if (h == 0 & o == 0)
{
Blynk.notify("Place your finger to the sensor");
}
}
void wifi_connect() {
if (WiFi.status() != WL_CONNECTED)
{
Serial.print("Attempting to connect to SSID: ");
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
}
void TSwrite()
{
int x = ThingSpeak.writeField(myChannelNumber, 1, pox.getSpO2(), myWriteAPIKey);
}
// Callback (registered below) fired when a pulse is detected
void onBeatDetected()
{
Serial.println("Beat!");
}
void setup()
{
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
ThingSpeak.begin(client); //Initialize ThingSpeak
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");
}
// The default current for the IR LED is 50mA and it could be changed
// by uncommenting the following line. Check MAX30100_Registers.h for all the
// available options.
// pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
// Register a callback for the beat detection
pox.setOnBeatDetectedCallback(onBeatDetected);
timer.setInterval(30000, sendMsg); //runs every half a min
timer.setInterval(1000, wifi_connect);
timer.setInterval(15000, TSwrite);
}
void loop()
{
Blynk.run();
timer.run();
// Make sure to call update as fast as possible
pox.update();
// Asynchronously dump heart rate and oxidation levels to the serial
// For both, a value of 0 means "invalid"
if (millis() - tsLastReport > REPORTING_PERIOD_MS)
{
Serial.print("Heart rate:");
Serial.print(pox.getHeartRate());
Serial.print("bpm / SpO2:");
Serial.print(pox.getSpO2());
Serial.println("%");
{
// here what i did , is i am converting DIgital and ANALOG outputs to VIRTUAL so i can display them
Blynk.virtualWrite(5, pox.getHeartRate());
Blynk.virtualWrite(4, pox.getSpO2());
}
tsLastReport = millis();
}
}