Arduino to thingspeak , Stops reading Values

Hello fellow programmers ,
I am trying to upload the findings of my max30100 sensor to thingspeak.
While i have a working example who is not connected on thingspeak , when i try and connect it to thing speak i get 0 readings for both Bpm and Spo2 .
My code is here and i believe it has to do something with me trying to pass a float value to thingspeak,
/*
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;
float spo2 = pox.getSpO2();
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)
{
Blynk.notify("Your BpM is Quite High,I am Sending Data To you Doctor ");
}
else if ( o<90 & o>80)
{
Blynk.notify("Your Spo2 is Quite Low,I am Sending Data To your Doctor ");
}
else if (h < 40)
{
Blynk.notify("Your BpM is Quite Low,I am Sending Data To you Doctor ");
}
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.");
}
}
// 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);
}
void loop()
{
Blynk.run();
timer.run();
float x= ThingSpeak.writeField(myChannelNumber, 1, spo2, myWriteAPIKey);
// 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();
}
}
It connects to Thingspeak , i just get 0 value for the spo2 ,but when i remove thingspeak code it works

11 Comments

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();
}
}
I'm not sure the interaction between the timer and the networking stack that is required to send the data to ThingSpeak.
I would recommend you modify the sketch to be
if (millis() - tsLastReport > REPORTING_PERIOD_MS){
//
//existing code here
//
ThingSpeak.writeField(myChannelNumber, 1, pox.getSpO2(), myWriteAPIKey);
tsLastReport = millis();
}
Note that REPORTING_PERIOD_MS must be at least 15000 if you have a free license of ThingSpeak. If you are updating the ThingSpeak channel every second a free account, your requests are likely being throttled for attempting to do something more frequently than your license type allows.
Place this where? Whatever i place inside loop it just gives me 0 values in my spo2 and bpm .
So i made Tswrite function and i call it every 15sec since i dont have a license .
Program works fine for 15sec and then freezes when my function ThingSpeak.writeField(myChannelNumber, 1, pox.getSpO2(), myWriteAPIKey); is called
>> Whatever i place inside loop it just gives me 0 values in my spo2 and bpm .
Sounds like you might need to read up some more to understand the capabilities of the library and why it does not return good values if called within the loop.
I'm not familiar with the pox library, or have the hardware, to be able to provide deeper insights. If I were to guess, the combination of use of timer interrupts to test and reset the WiFi connection in the loop, the not resetting of ThingSpeak.begin() when the WiFi connection is reset, and the pox library behavior have something to do with your issue.
Hmmm, wifi connect is a fucntion outside of loop called every 1 sec , but indeed i am not resetting Thingspeak when i re-connect(didnt happen to reconnect) so i guess it has to do with the pox library and the thingspeak.
Both Sad and encouraging that i cant find a similar project :P
There are a couple of other things that I am not sure of -- Does the Blynk library do connection management? The fact that you need to provide it your SSID and password in Blynk.begin(auth, ssid, pass) and that you also have the wifi_connect() function that references a WiFi object that isn't declared anywhere in your code are some flags when I look at this code.
Good point , i checked it and yes Blynk.begin connects me to wifi , so the function wifi_connect is useless.
I removed everything from the loop except from this code.
I made a function Getrating for this and i call it every 1sec and it still freezes everything, but whenever i delete this line
int x = ThingSpeak.writeField(myChannelNumber, 1, pox.getSpO2(), myWriteAPIKey);
Everything Works perfect.
Blynk.run();
timer.run();
// Make sure to call update as fast as possible
pox.update();
How about splitting that line into 2:
float spo2;
uint32_t tsLastReport = millis();
void setup()
{
// existing setup() code
}
void loop() {
// existing code here ...
if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
spo2 = pox.getSpO2();
ThingSpeak.writeField(myChannelNumber, 1, spo2, myWriteAPIKey);
tsLastReport = millis();
}
}
Yea like this the program doesnt even begin , because we call the TsWritefield asap and it freezes it for a weird reason.
I tried to print my writefield on serial monitor and i got a weird result that led me to think about pox.
why it gets both heart and spo2 rate when i am only using pox.getSpO2?
I dont know if thats the problem still but its weird
23:37:47.442 -> x = ThingSpeak.writeField x=200Heart rate:92.23bpm / SpO2:96%
23:37:48.544 -> Heart rate:92.23bpm / SpO2:96%
23:37:49.610 -> Heart rate:92.23bpm / SpO2:96%
23:37:50.682 -> Heart rate:92.23bpm / SpO2:96%
23:37:51.754 -> Heart rate:92.23bpm / SpO2:96%
23:37:52.820 -> Heart rate:92.23bpm / SpO2:96%
23:37:53.888 -> Heart rate:92.23bpm / SpO2:96%
23:37:54.959 -> Heart rate:92.23bpm / SpO2:96%
23:37:56.033 -> Heart rate:92.23bpm / SpO2:96%
23:37:57.103 -> Heart rate:92.23bpm / SpO2:96%
23:37:58.200 -> Heart rate:92.23bpm / SpO2:96%
23:37:59.265 -> Heart rate:92.23bpm / SpO2:96%
23:38:00.329 -> Heart rate:92.23bpm / SpO2:96%
23:38:01.399 -> Heart rate:92.23bpm / SpO2:96%
23:38:02.299 -> x = ThingSpeak.writeField x=-401Heart rate:92.23bpm / SpO2:96%
My guess is something about the pox library must be messing up interrupts and that interferes with the wifi connectivity used by ThingSpeak. A simple test will be run one test with the pox library code commented out but the ThingSpeak code in (send random numbers instead of real sensor data) and a separate run with the ThingSpeak code commented our and the pox library enabled. If each works independently, then you will have confirmed my hypothesis.
Its definetely Pox cuz they work seperately .
Thing is if you check documentation of max30100 ,there is no pox mention anywhere.
Will try and uppload it to my Works thingspeak with license and check again.

Sign in to comment.

Answers (1)

I tried coding above but an error like this happened. how can i fix it? please help me
C:\Users\asus\Documents\Arduino\libraries\WiFiNINA\src\utility\spi_drv.cpp: In static member function 'static void SpiDrv::begin()':
C:\Users\asus\Documents\Arduino\libraries\WiFiNINA\src\utility\spi_drv.cpp:87:24: error: 'PINS_COUNT' was not declared in this scope
if (SLAVERESET > PINS_COUNT) {
^
C:\Users\asus\Documents\Arduino\libraries\WiFiNINA\src\utility\spi_drv.cpp:97:15: error: 'NINA_GPIO0' was not declared in this scope
pinMode(NINA_GPIO0, OUTPUT);
^
C:\Users\asus\Documents\Arduino\libraries\WiFiNINA\src\utility\spi_drv.cpp: In static member function 'static int SpiDrv::available()':
C:\Users\asus\Documents\Arduino\libraries\WiFiNINA\src\utility\spi_drv.cpp:581:25: error: 'NINA_GPIO0' was not declared in this scope
return (digitalRead(NINA_GPIO0) != LOW);
^
exit status 1
Error compiling for board Generic ESP8266 Module.

Communities

More Answers in the  ThingSpeak Community

Categories

Find more on ThingSpeak in Help Center and File Exchange

Products

Asked:

on 22 Sep 2020

Answered:

on 30 Nov 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!