I tried to save reading data to firebase and 000webhost database. So I could not to save exact readings to database.
#include <HTTPClient.h>
#include <WiFi.h>
#include "MAX30100_PulseOximeter.h"
#include "DHT.h"
#define DHTTYPE DHT11
#define DHTPIN 18
#define ADC_VREF_mV 3300.0 // in millivolt
#define ADC_RESOLUTION 4096.0
#define PIN_LM35 36 // ESP32 pin GIOP36 (ADC0) connected to LM35
#define POX_REPORTING_PERIOD_MS 1000
DHT dht(DHTPIN, DHTTYPE);
// Update HOST URL here
#define HOST "https://braveheartsz.000webhostapp.com" // Enter HOST URL without "http:// " and "/" at the end of URL
#define WIFI_SSID "HUAWEI-F50B" // WIFI SSID here
#define WIFI_PASSWORD "00493700" // WIFI password here
PulseOximeter pox;
uint32_t poxLastReport = 0;
// Declare global variables which will be uploaded to server
float temperature, humidity, BPM, SpO2, bodytemperature;
String sendval, sendval2, sendval3, sendval4, postData;
void onBeatDetected()
{
Serial.println("Beat!");
}
void setup() {
Serial.begin(115200);
Serial.println("Communication Started \n\n");
Serial.println("DHT11 Output!");
dht.begin();
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); //try to connect with wifi
Serial.print("Connecting to ");
Serial.print(WIFI_SSID);
while (WiFi.status() != WL_CONNECTED)
{ Serial.print(".");
delay(2000);
}
Serial.println();
Serial.print("Connected to ");
Serial.println(WIFI_SSID);
Serial.print("IP Address is : ");
Serial.println(WiFi.localIP()); //print local IP address
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);
}
void loop() {
HTTPClient http; // http object of clas HTTPClient
WiFiClient wclient; // wclient object of clas HTTPClient
pox.update();
float temp = dht.readTemperature();
float hum = dht.readHumidity();
// read the ADC value from the temperature sensor
int adcVal = analogRead(PIN_LM35);
// convert the ADC value to voltage in millivolt
float milliVolt = adcVal * (ADC_VREF_mV / ADC_RESOLUTION);
// convert the voltage to the temperature in °C
float tempC = milliVolt / 10;
BPM = pox.getHeartRate();
SpO2 = pox.getSpO2();
if (millis() - poxLastReport > POX_REPORTING_PERIOD_MS)
{
BPM= round(pox.getHeartRate());
SpO2= round(pox.getSpO2());
Serial.print("BPM: ");
Serial.println(BPM);
Serial.print("SpO2: ");
Serial.print(SpO2);
Serial.println("%");
if(isnan(temp) || isnan(hum)){
Serial.println("Failed to read DHT11");
}
else{
Serial.print("Humidity: ");
Serial.print(hum);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temp);
Serial.println(" *C");
}
Serial.print("Temperature: ");
Serial.print(tempC); // print the temperature in °C
Serial.println("°C");
Serial.println("*********************************");
Serial.println();
poxLastReport = millis();
}
delay(1000);
// Convert integer variables to string
sendval = String(temp);
sendval2 = String(hum);
sendval3 = String(pox.getSpO2());
sendval4 = String(pox.getHeartRate());
postData = "sendval=" + sendval + "&sendval2=" + sendval2 + "&sendval3=" + sendval3 + "&sendval4=" + sendval4 ;
// We can post values to PHP files as example.com/dbwrite.php?name1=val1&name2=val2&name3=val3
// Hence created variable postDAta and stored our variables in it in desired format
// For more detials, refer:- https://www.tutorialspoint.com/php/php_get_post.htm
// Update Host URL here:-
http.begin("https://braveheartsz.000webhostapp.com/weather/insert.php"); // Connect to host where MySQL databse is hosted
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
int httpCode = http.POST(postData); // Send POST request to php file and store server response code in variable named httpCode
Serial.println("Values are, sendval = " + sendval + " and sendval2 = "+sendval2 + " and sendval3 = "+sendval3 + "and sendval4 = "+sendval4 );
delay(1000);
// if connection eatablished then do this
if (httpCode == 200) { Serial.println("Values uploaded successfully."); Serial.println(httpCode);
String webpage = http.getString(); // Get html webpage output and store it in a string
Serial.println(webpage + "\n");
}
// if failed to connect then return and restart
else {
Serial.println(httpCode);
Serial.println("Failed to upload values. \n");
http.end();
return;
}
// temp+=1; hum=10; // Incrementing value of variables
delay(1000);
}