#include <Arduino.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <WiFi.h>
#include <time.h>
// WiFi SimulatorCredentials
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define DATABASE_URL "https://healthguard-wokwi-default-rtdb.firebaseio.com/.json"
#define HRS_PIN 13
// 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);
// LCD setup
pinMode(HRS_PIN, INPUT);
// WiFi setup
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
delay(500);
lcd.setCursor(0, 0);
lcd.print("Connecting to");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
// Status Database Connectivity
lcd.clear();
lcd.setCursor(0, 0);
lcd.println("WiFi Connected");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.println("Connecting to");
lcd.setCursor(0, 1);
lcd.println("Firebase...");
// Perform connection
client.begin(DATABASE_URL);
int httpResponseCode = client.GET();
if (httpResponseCode > 0) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.println("Connected");
payload = client.getString();
}
// Timer Client Configuration
configTime(0, 0, "pool.ntp.org");
}
void loop() {
// Simulated sensor values
float bodyTemperature = 36.5; // Example value
int breathRate = 19; // Example value
int heartRate = 100;
int oxygenLevel = 100; // Example value
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
// Display data on LCD
delay(6000); // Send data every minute
}