Arduino to thingspeak , Stops reading Values
Show older comments
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
THEODOROS KOUKOUVES
on 22 Sep 2020
Vinod
on 22 Sep 2020
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.
THEODOROS KOUKOUVES
on 22 Sep 2020
Vinod
on 22 Sep 2020
>> 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.
THEODOROS KOUKOUVES
on 22 Sep 2020
Vinod
on 22 Sep 2020
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.
THEODOROS KOUKOUVES
on 22 Sep 2020
Vinod
on 22 Sep 2020
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();
}
}
THEODOROS KOUKOUVES
on 22 Sep 2020
Edited: THEODOROS KOUKOUVES
on 22 Sep 2020
Vinod
on 22 Sep 2020
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.
THEODOROS KOUKOUVES
on 22 Sep 2020
Answers (1)
Lika Fitranto
on 30 Nov 2020
0 votes
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
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!