#include <WiFi.h>
#include <HTTPClient.h>
// Define normal ranges for peak flow, oxygen saturation, and flow sensor
struct AgeGroupThresholds {
int peak_flow_min;
int peak_flow_max;
int oxygen_min;
int oxygen_max;
int flow_sensor_min;
};
// Age group thresholds
AgeGroupThresholds children = {200, 400, 93, 100, 100}; // For Children
AgeGroupThresholds adults = {400, 600, 95, 100, 200}; // For Adults
AgeGroupThresholds elderly = {300, 500, 90, 100, 150}; // For Elderly
AgeGroupThresholds ageGroups[] = {children, adults, elderly};
const char* ageGroupNames[] = {"Children", "Adults", "Elderly"};
const int peak_flow_pin = 34; // ADC pin for peak flow
const int oxygen_level_pin = 35; // ADC pin for oxygen level
const int flow_sensor_pin = 33; // ADC pin for flow sensor
const int led_pin = 26; // Pin for LED alerts
const char* ssid = "Wokwi-GUEST"; // WiFi SSID
const char* password = ""; // WiFi Password
const char* thingSpeakApiKey = "86RTOTCA38C93Z4O"; // ThingSpeak API Key
const char* thingSpeakHost = "api.thingspeak.com";
void setup() {
Serial.begin(115200); // Initialize serial communication
pinMode(led_pin, OUTPUT); // Set LED pin as output
digitalWrite(led_pin, LOW); // Ensure LED is off at the start
// Connect to WiFi
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" Connected!");
}
void loop() {
for (int i = 0; i < 3; i++) { // Loop through age groups
// Simulate sensor data
int peak_flow = analogRead(peak_flow_pin);
int oxygen_level = analogRead(oxygen_level_pin);
int flow_sensor = analogRead(flow_sensor_pin);
// Scale readings to appropriate ranges
peak_flow = map(peak_flow, 0, 4095, 300, 700);
oxygen_level = map(oxygen_level, 0, 4095, 90, 100);
flow_sensor = map(flow_sensor, 0, 4095, 100, 400);
// Print simulated sensor data
Serial.print("\nAge Group: ");
Serial.println(ageGroupNames[i]);
Serial.print("Simulated Peak Flow: ");
Serial.print(peak_flow);
Serial.println(" L/min");
Serial.print("Simulated Oxygen Level: ");
Serial.print(oxygen_level);
Serial.println("%");
Serial.print("Simulated Inhalation Flow: ");
Serial.print(flow_sensor);
Serial.println(" L/min");
// Evaluate the patient's condition
String condition_message = evaluate_condition(peak_flow, oxygen_level, flow_sensor, ageGroups[i]);
Serial.println(condition_message);
// Send data to ThingSpeak
sendToThingSpeak(peak_flow, oxygen_level, flow_sensor);
delay(5000); // Wait 5 seconds before next iteration
}
delay(20000); // Wait before repeating the loop
}
String evaluate_condition(int peak_flow, int oxygen_level, int flow_sensor, AgeGroupThresholds thresholds) {
String low_factors = "";
// Check if peak flow is below normal range
if (peak_flow < thresholds.peak_flow_min) {
low_factors += "Peak Flow is below normal! ";
}
// Check if oxygen level is below normal range
if (oxygen_level < thresholds.oxygen_min) {
low_factors += "Oxygen Level is below normal! ";
}
// Check if inhalation flow is below normal range
if (flow_sensor < thresholds.flow_sensor_min) {
low_factors += "Inhalation Flow is below normal! ";
}
// Determine the condition message based on the low factors found
if (low_factors.length() > 0) {
digitalWrite(led_pin, HIGH); // Turn on LED
return "ALERT: Your condition is unstable!\n" + low_factors;
} else {
digitalWrite(led_pin, LOW); // Turn off LED when condition is stable
return "Condition is stable: Peak Flow, Oxygen levels, and inhaler use are within normal range.";
}
}
void sendToThingSpeak(int peak_flow, int oxygen_level, int flow_sensor) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Construct the URL for the ThingSpeak API
String url = String("https://") + thingSpeakHost + "/update?api_key=" + thingSpeakApiKey +
"&field1=" + String(peak_flow) +
"&field2=" + String(oxygen_level) +
"&field3=" + String(flow_sensor);
Serial.println("Sending data to ThingSpeak: " + url); // Print the URL for debugging
http.begin(url); // Start the connection
int httpCode = http.GET(); // Send the request
// Check for the returning code
if (httpCode > 0) {
String payload = http.getString(); // Get the request response payload
Serial.println("ThingSpeak response: " + payload);
} else {
Serial.println("Error on HTTP request: " + String(httpCode));
}
http.end(); // Free resources
} else {
Serial.println("WiFi not connected");
}
}