#include <WiFi.h>
#include <HTTPClient.h>
#include "DHTesp.h"
WiFiClient client;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const int DHT_PIN = 15;
DHTesp dhtSensor;
String patient_username = ""; // Variable to store patient's username
void setup() {
Serial.begin(9600);
WiFi.disconnect();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
// Prompt user to enter patient's username
Serial.println("Please enter the patient's username:");
}
void loop() {
if (Serial.available() > 0) {
char c = Serial.read();
if (c != '\n') {
patient_username += c;
} else {
Serial.println("Patient username entered: " + patient_username);
send_data_to_api();
patient_username = ""; // Reset the variable for the next input
Serial.println("Please enter the patient's username:");
}
}
}
void send_data_to_api() {
// Read sensor data (replace with actual sensor readings)
int systolic_pressure = random(70, 90);
int diastolic_pressure = random(110, 150);
int blood_oxygen = random(85, 100);
float temperature = random(360, 380) / 10.0;
int heart_rate = random(50, 100);
unsigned long timestamp = millis(); // Current time in milliseconds
// Construct payload
String payload = "{\"patient_username\":\"" + patient_username + "\"" +
",\"timestamp\":\"" + String(timestamp) + "\"" +
",\"blood_pressure\":\"" + String(systolic_pressure) + "/" + String(diastolic_pressure) + "\"" +
",\"heart_rate\":\"" + String(heart_rate) + "\"" +
",\"o2\":\"" + String(blood_oxygen) + "\"" +
",\"temperature\":\"" + String(temperature) + "\"}";
// Print sensor data and timestamp
Serial.print("Sending data to API: ");
Serial.println("https://digicarebackend.onrender.com/api/record-data/" + patient_username);
Serial.print("Payload: ");
Serial.println(payload);
// Send HTTP request to API
HTTPClient http;
String api_url = "https://digicarebackend.onrender.com/api/record-data/" + patient_username;
http.begin(api_url);
http.addHeader("Content-Type", "application/json");
int httpCode = http.POST(payload);
// Check HTTP response
if (httpCode > 0) {
Serial.print("HTTP response code: ");
Serial.println(httpCode);
String response = http.getString();
Serial.println("Response:");
Serial.println(response);
} else {
Serial.print("HTTP request failed with error code: ");
Serial.println(httpCode);
// Print error details
String error = http.errorToString(httpCode);
Serial.print("Error: ");
Serial.println(error);
}
http.end();
}