#include <WiFi.h>
#include <HTTPClient.h>
// ============================================
// EJERCICIO: ESP32 + WiFi + HTTP GET (BÁSICO)
//
// 1) Conecta a WiFi
// 2) Hace un GET a una web simple
// 3) Muestra en Serial el código HTTP y parte del texto
// ============================================
// WiFi (Wokwi)
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Cada cuánto pedir (ms)
const unsigned long INTERVALO_MS = 5000;
// Endpoint simple y confiable (HTTP, no HTTPS)
const char* URL = "http://example.com";
unsigned long lastRun = 0;
void conectarWiFi() {
Serial.print("Conectando a WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(300);
Serial.print(".");
}
Serial.println("\n✅ WiFi conectado");
Serial.print("IP del ESP32: ");
Serial.println(WiFi.localIP());
}
void pedirWeb() {
HTTPClient http;
Serial.println("\n--- HTTP GET ---");
Serial.print("URL: ");
Serial.println(URL);
http.begin(URL);
int httpCode = http.GET();
Serial.print("HTTP Code: ");
Serial.println(httpCode);
if (httpCode > 0) {
String payload = http.getString();
Serial.println("Respuesta (primeros 200 chars):");
Serial.println(payload.substring(0, 200));
} else {
Serial.println("❌ Error en HTTP");
}
http.end();
}
void setup() {
Serial.begin(115200);
delay(200);
conectarWiFi();
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("⚠️ WiFi caído, reconectando...");
conectarWiFi();
}
if (millis() - lastRun >= INTERVALO_MS) {
lastRun = millis();
pedirWeb();
}
}