#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
// Configuración de Red (Wokwi usa esta por defecto)
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// REEMPLAZA ESTA URL por la que te dé Ngrok
const char* serverUrl = "https://TU_URL_DE_NGROK.ngrok-free.app/api/hardware/update";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Conectando a WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\n¡Conectado!");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverUrl);
http.addHeader("Content-Type", "application/json");
// Simulamos lecturas de sensores
StaticJsonDocument<200> doc;
doc["ph"] = 6.4 + (random(-2, 3) / 10.0);
doc["temp"] = 24.5 + (random(-10, 11) / 10.0);
doc["tank"] = 85;
doc["wind"] = 12.5;
String requestBody;
serializeJson(doc, requestBody);
Serial.print("Enviando datos: ");
Serial.println(requestBody);
int httpResponseCode = http.POST(requestBody);
if (httpResponseCode > 0) {
Serial.print("Respuesta del servidor: ");
Serial.println(httpResponseCode);
} else {
Serial.print("Error en el envío: ");
Serial.println(http.errorToString(httpResponseCode).c_str());
}
http.end();
}
delay(5000); // Envía datos cada 5 segundos
}