When I written code for post data into the database that time I am getting sensor reading properly on serial monitor but in the database sensor values become 0.
Esp32 code is below*
#include <Wire.h>
#include "MAX30100_PulseOximeter.h"
#include <WiFi.h>
#include <OneWire.h>
#include <HTTPClient.h>
#include <WiFiClient.h>
unsigned long lastTime = 0;
unsigned long timerDelay = 5000; // Set timer to 5 seconds (5000)
MAX30100 maxim;
PulseOximeter pox;
const char* ssid = "naimuddin";
const char* password = "User@123";
const char* servername = "http://192.168.198.1:8080/test_project/Call.php";
String apiKeyValue = "tPmAT5Ab3j7F9";
void initpox() // Init POX => MAX30100
{
if (!pox.begin()) // Initialize sensor
{
Serial.println("FAILED");
for (;;);
}
else
{
Serial.println("SUCCESS");
}
pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA); // Configure sensor to use 7.6mA for LED drive
}
// Initialize WiFi
void initWiFi()
{
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
}
void setup()
{
Serial.begin(115200);
initWiFi();
initpox();
}
void postData(float value4, float value5) // This is the main method where data gets pushed to the local server
{
WiFiClient client; // Creates a client that can connect to to a specified internet IP address and port as defined in client.connect().
HTTPClient http; //HttpClient is a library to make it easier to interact with web servers from Arduino.
http.begin(client, servername); // Your Domain name with URL path or IP address with path
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); // Specify content-type header
String httpRequestData = "api_key=" + apiKeyValue + "&BPM=" + String(value4) + "&SpO2=" + String(value5) + "";
Serial.print("httpRequestData: ");
Serial.println(httpRequestData);
int httpResponseCode = http.POST(httpRequestData); // Send HTTP POST requests
Serial.println(httpResponseCode);
maxim.resetFifo();
if (httpResponseCode > 0)
{
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
}
else
{
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end(); // Free resources
}
void loop()
{
// Read from the sensor
pox.update();
// Grab the updated heart rate and SpO2 levels
if ((millis() - lastTime) > timerDelay)
{
float A = pox.getHeartRate();
float B = pox.getSpO2();
Serial.print("bpm: ");
Serial.println(A);
Serial.print("SpO2: ");
Serial.println(B);
postData(A, B);
lastTime = millis();
}
}
****Serial monitor output
*PHP mysql code
<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "iot_test"; $api_key_value = "tPmAT5Ab3j7F9"; $api_key = $value4 = $value5 = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { $api_key = test_input($_POST["api_key"]); if($api_key == $api_key_value) { $value4 = test_input($_POST["value4"]); $value5 = test_input($_POST["value5"]); // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "INSERT INTO test_db (Heart_Rate, Blood_Oxygen) VALUES ('" . $value4 . "', '" . $value5 . "')"; if ($conn->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "" . $conn->error; } $conn->close(); } else { echo "Wrong API Key provided."; } } else { echo "No data posted with HTTP POST."; } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?>
******Database output
1 post - 1 participant