My ds18b20 (Temperature sensor), Max30100 (heart rate and pulse oximeter sensor), and SW-420 (vibration sensor) work fine and show the correct value in the OLED. But when I try to send the values to "ThingSpeak" ds18b20 works fine but max30100 only shows "0" in the OLED and ThingSpeak. Can anyone help me to solve this problem? I will be very grateful to you. My code is given below,
#include <Wire.h>
#include "MAX30100_PulseOximeter.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#include "ThingSpeak.h"
#include <ESP8266WiFi.h> // for wifi connection
String apiKey = "xx"; // Enter Write API key from ThingSpeak
const char *ssid = "xx"; // replace with wifi ssid and wpa2 key
const char *pass = "xx";
const char* server = "api.thingspeak.com";
#define REPORTING_PERIOD_MS 1000
#define DS18B20 14 //D4 pin= GPIO pin 2
#include <Adafruit_GFX.h>//////////////////////OLED SECTION
#include <Adafruit_SSD1306.h>
#define OLED_RESET -1//////////////////////////////OLED
#define WIDTH 128//////// the lower right corner is (X=128, Y=63)
#define HEIGHT 64
#define NUM_PAGE 8
#define NUMFLAKES 10
#define XPOS 0/////////////////Every pixel on the display has a coordinate that is specified with a X and //Y.
///////////////////The X increases from left to right and Y increases from top to the bottom. and
#define YPOS 0//////////////////The upper left corner of the screen is (X=0, Y=0)
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
//Adafruit_SSD1306 display(OLED_RESET);
Adafruit_SSD1306 display(WIDTH, HEIGHT, &Wire, OLED_RESET);
float bPM;
float sp02;
float tempF;
PulseOximeter pox;
#define VIBRATION_SENSOR_PIN 13
int motion_detected = LOW;
uint32_t tsLastReport = 0;
OneWire oneWire(DS18B20);
DallasTemperature sensors(&oneWire);
WiFiClient client; // for wifi
void setup() {
// Initialize serial and wait for port to open:
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);///// Call the begin function of the display object by passing our I2C address 0x3C
Serial.begin(9600);
sensors.begin();
pinMode(VIBRATION_SENSOR_PIN, INPUT);
// for wifi
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
display.clearDisplay();/////////////clears the display completely.
display.setTextSize(2);/////////accepts an integer number as a size. The greater the number, the bigger the text would be. Smallest size is 1 which is the default size //of texts.
display.setTextColor(WHITE);///////////////specify the color using display.setTextColor() WHITE/BLACK
display.setCursor(15, 8);//////////////////specify where on the display we're going to display the text.
display.println("CSE-499A");////////////We draw something on by calling on our (display) object,
display.setTextSize(1);
display.setCursor(0, 30);
display.println("Al Mehedi Hasan");
display.setTextSize(1);
display.setCursor(0, 40);
display.println("MD Maruf Chowdhury");
display.setTextSize(1);
display.setCursor(0, 50);
display.println("Isteak Ahmed Sheam");
display.display();//////////////////Call the display.display() function to make the actual drawing happen on the hardware level.
delay(1000);
Serial.print("Initializing Pulse Oximeter..");
if (!pox.begin())
{
Serial.println("FAILED");
for (;;);
}
else
{
Serial.println("SUCCESS");
pox.setOnBeatDetectedCallback(onBeatDetected);
}
pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
// display.clearDisplay();/////////////clears the display completely.
// sensors.requestTemperatures();
}
void loop() {
pox.update();
bPM = pox.getHeartRate();
sp02 = pox.getSpO2();
tempF = sensors.getTempFByIndex(0);
motion_detected = digitalRead(VIBRATION_SENSOR_PIN);
if (millis() - tsLastReport > REPORTING_PERIOD_MS)
{
Serial.print("Heart rate:");
Serial.print(bPM);
Serial.print(" bpm / SpO2:");
Serial.print(sp02);
Serial.println(" %");
Serial.print("Body Temperature: ");
Serial.print(tempF);
Serial.println("°F");
//////////////////TEMPERATURE///////////////////
display.clearDisplay();
display.setTextColor(WHITE);
display.setCursor(0, 5);
display.setTextSize(1);
display.print("Temperature:");
display.setTextSize(1);
display.setTextColor(WHITE, BLACK);
display.println(tempF);
display.drawCircle (105, 5, 2, WHITE);
display.drawCircle (105, 5, 2, WHITE);
display.setTextSize(1);
display.setTextColor(WHITE, BLACK);
display.setCursor(112, 5);
display.println("F");
////////////////Vibration Sensor///////////////
display.setTextColor(WHITE);
display.setCursor(0, 18);
display.setTextSize(1);
display.print("Cough:");
if (motion_detected == LOW)
{
display.setTextColor(WHITE);
display.setCursor(38, 18);
display.setTextSize(1);
//display.println("TEMPERATURE:");
display.print("No");
}
else
{
display.setTextColor(WHITE);
display.setCursor(38, 18);
display.setTextSize(1);
display.print("Yes");
}
////////////////Pulse Sensor//////////////////
display.setTextColor(WHITE);
display.setCursor(0, 30);
display.setTextSize(1);
display.print("BPM : ");
display.print(pox.getHeartRate());
display.setCursor(0, 40);
display.setTextSize(1);
display.print("Sp02: ");
display.print(pox.getSpO2());
display.print("%");
display.display();
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr +="&field1=";
postStr += String(tempF);
postStr +="&field2=";
postStr += String(bPM);
postStr +="&field3=";
postStr += String(sp02);
postStr +="&field4=";
postStr += String(motion_detected);
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
Serial.print("Temperature: ");
Serial.print(tempF);
Serial.print(" BPM: ");
Serial.print(bPM);
Serial.print(" Spo2: ");
Serial.print(sp02);
Serial.print("%");
Serial.print(" Cough: ");
Serial.print(motion_detected);
Serial.println(". Data Sent to Thingspeak.");
}
client.stop();
Serial.println("Waiting...");
// thingspeak needs minimum 15 sec delay between updates
delay(1000);
tsLastReport = millis();
sensors.setWaitForConversion(false);
sensors.requestTemperatures();
sensors.setWaitForConversion(true);
// }
}