#include <Arduino.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <math.h>
#include <WiFi.h>
#include <time.h>
// WiFi SimulatorCredentials
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define DATABASE_URL "https://upcpre202401si572sandbox-default-rtdb.firebaseio.com/.json"
#define HRS_PIN 33
#define BT_PIN 32
#define OL_PIN 35
#define BR_PIN 34
// Create HTTP Client
HTTPClient client;
// Display Configuration
// Operation Variables
String payload = "";
String sensorID = "HC001";
char timeStringBuffer[20];
void setup() {
// Setup code to run once
Serial.begin(115200);
pinMode(HRS_PIN, INPUT);
pinMode(BT_PIN, INPUT);
pinMode(OL_PIN, INPUT);
pinMode(BR_PIN, INPUT);
// WiFi setup
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
delay(500);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
// Perform connection
client.begin(DATABASE_URL);
int httpResponseCode = client.GET();
// Timer Client Configuration
configTime(0, 0, "pool.ntp.org");
}
void loop() {
// Simulated sensor values
int HRS_ANALOG_X = analogRead(HRS_PIN);
int HRS_scaledValue = map(HRS_ANALOG_X, 0, 4095, 0, 200);
int BT_ANALOG_X = analogRead(BT_PIN);
float BT_scaledValue = (BT_ANALOG_X / 4095.0) * (45.0 - 20.0) + 20.0;
int OL_ANALOG_X = analogRead(OL_PIN);
int OL_scaledValue = map(OL_ANALOG_X, 0, 4095, 0, 100);
int BR_ANALOG_X = analogRead(BR_PIN);
int BR_scaledValue = map(BR_ANALOG_X, 0, 4095, 0, 30);
float bodyTemperature = ((int)(BT_scaledValue * 100 + .5) / 100.0); // Example value
int breathRate = BR_scaledValue; // Example value
int heartRate = HRS_scaledValue;
int oxygenLevel = OL_scaledValue; // Example value
int sensorValue = analogRead(HRS_PIN);
Serial.println(sensorValue);
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Time Error");
return;
}
strftime(timeStringBuffer, sizeof(timeStringBuffer), "%d/%m/%Y %H:%M", &timeinfo);
Serial.println(String(timeStringBuffer));
// Create JSON object
StaticJsonDocument<200> jsonDoc;
JsonObject healthData = jsonDoc.createNestedObject("HealthData");
//healthData["time"] = String(timeStringBuffer);
healthData["bodyTemperature"] = bodyTemperature;
healthData["breathRate"] = breathRate;
healthData["heartRate"] = heartRate;
healthData["oxygenLevel"] = oxygenLevel;
String jsonData;
serializeJson(jsonDoc, jsonData);
// Send information to Database
client.begin(DATABASE_URL);
client.addHeader("Content-Type", "application/json");
int httpResponseCode = client.PATCH(jsonData);
if (httpResponseCode > 0) {
String response = client.getString();
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
Serial.print("Response from server: ");
Serial.println(response);
} else {
Serial.print("Error on sending PATCH: ");
Serial.println(httpResponseCode);
Serial.print("Error details: ");
Serial.println(client.errorToString(httpResponseCode).c_str());
}
client.end(); // Free resources
delay(1000); // Send data every minute
}