#include <WiFi.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
#include "DHT.h"
#include <Adafruit_MPU6050.h>
#include <Wire.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* serverUrl = "https://scudo.vercel.app/api/suit-data";
#define DHTPIN 15
#define DHTTYPE DHT22
#define BUTTON_PIN 4
DHT dht(DHTPIN, DHTTYPE);
WiFiClientSecure client;
int sim_battery = 100;
float sim_o2 = 21.0;
void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP);
dht.begin();
WiFi.begin(ssid, password);
Serial.print("Conectando a WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConectado!");
client.setInsecure();
}
void loop() {
if (random(0, 100) < 30) {
sim_battery--;
}
if (sim_battery < 5) sim_battery = 100;
float fluctuacion = (random(-5, 5)) / 10.0;
sim_o2 = 21.0 + fluctuacion;
bool es_emergencia = (random(0, 100) < 15);
if (es_emergencia) {
sim_o2 = random(160, 190) / 10.0;
}
float t_int = dht.readTemperature();
float h_int = dht.readHumidity();
if (isnan(t_int)) t_int = es_emergencia ? 35.2 : 36.5;
if (isnan(h_int)) h_int = 45;
bool panic_btn = (digitalRead(BUTTON_PIN) == LOW);
float temp_ext = -22.5 + (random(-20, 20) / 10.0);
int bpm;
if (panic_btn) {
bpm = random(140, 170);
} else if (sim_o2 < 19.0) {
bpm = random(110, 130);
} else if (es_emergencia) {
bpm = random(100, 120);
} else {
bpm = random(65, 85);
}
bool man_down = false;
if (es_emergencia && random(0, 100) < 40) man_down = true;
if (panic_btn) man_down = true;
StaticJsonDocument<768> doc;
doc["device_id"] = "TRAJE_FROST_01";
doc["user_id"] = 5;
JsonObject telemetry = doc.createNestedObject("telemetry");
telemetry["temp_ext"] = temp_ext;
telemetry["temp_int"] = t_int;
telemetry["hum_int"] = (int)h_int;
telemetry["bpm"] = bpm;
telemetry["o2_level"] = sim_o2;
telemetry["battery"] = sim_battery;
JsonObject status = doc.createNestedObject("status");
status["man_down"] = man_down;
status["panic_btn"] = panic_btn;
status["connection"] = "wifi";
String jsonPayload;
serializeJson(doc, jsonPayload);
if(WiFi.status() == WL_CONNECTED){
HTTPClient http;
if (http.begin(client, serverUrl)) {
http.addHeader("Content-Type", "application/json");
int code = http.POST(jsonPayload);
if(code > 0) {
Serial.printf("Enviado! O2: %.1f%% | Bat: %d%% | Status: %s\n",
sim_o2, sim_battery, es_emergencia ? "EMERGENCIA" : "OK");
} else {
Serial.printf("Error HTTP: %s\n", http.errorToString(code).c_str());
}
http.end();
} else {
Serial.println("Error conectando al servidor...");
}
}
delay(500);
}