#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* api_key = "IMTUT5U03E23RA03";
#define DHTPIN 14
#define DHTTYPE DHT22
#define BUZZER 2
#define LED_RED 4
#define HEART_SENSOR 34
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
pinMode(BUZZER, OUTPUT);
pinMode(LED_RED, OUTPUT);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected!");
}
void loop() {
int bpm = map(analogRead(HEART_SENSOR), 0, 4095, 40, 160);
float temp = dht.readTemperature();
if (isnan(temp)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.printf("BPM: %d | Temp: %.2fC\n", bpm, temp);
if(bpm > 100 || bpm < 50 || temp > 38.0) {
digitalWrite(BUZZER, HIGH);
digitalWrite(LED_RED, HIGH);
Serial.println("!!! ALERT: PATIENT IN DANGER !!!");
} else {
digitalWrite(BUZZER, LOW);
digitalWrite(LED_RED, LOW);
}
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin("https://api.favoriot.com/v1/streams");
http.addHeader("Content-Type", "application/json");
http.addHeader("apikey", api_token);
String jsonPayload = "{";
jsonPayload += "\"device_developer_id\":\"" + String(device_developer_id) + "\",";
jsonPayload += "\"data\":{";
jsonPayload += "\"bpm\":\"" + String(bpm) + "\",";
jsonPayload += "\"temperature\":\"" + String(temp) + "\"";
jsonPayload += "}}";
int httpResponseCode = http.POST(jsonPayload);
if (httpResponseCode > 0) {
Serial.print("Favoriot Response: ");
Serial.println(httpResponseCode);
} else {
Serial.print("Error sending POST: ");
Serial.println(httpResponseCode);
}
http.end();
}
delay(5000);
}