#include <HTTPClient.h>
#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// URL de la que se obtiene el mensaje
String URL_S = "https://tfgjcp1c-3000.use2.devtunnels.ms/";
// Credenciales WiFi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Configuración del LED
const int ledPin_S = 17;
// Configuración de la pantalla LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // Dirección I2C, 16 columnas, 2 filas
void setup() {
Serial.begin(115200);
Serial.println("Setup started...");
connectWiFi();
pinMode(ledPin_S, OUTPUT);
digitalWrite(ledPin_S, LOW);
// Inicializar la pantalla LCD
lcd.init();
lcd.backlight();
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
connectWiFi();
}
Serial.println("Loop started...");
HTTPClient http_S;
if (http_S.begin(URL_S)) {
int httpResponseCode_S = http_S.GET();
if (httpResponseCode_S > 0) {
String payload_S = http_S.getString();
Serial.print("Response payload S: ");
Serial.println(payload_S);
// Extraer solo el texto del cuerpo
String bodyText = extractBodyText(payload_S);
// Mostrar el texto extraído en la pantalla LCD
displayMessage(bodyText);
} else {
Serial.print("Error on HTTP request S: ");
Serial.println(httpResponseCode_S);
}
http_S.end();
} else {
Serial.println("Unable to connect to server for URL_S");
}
delay(5000); // Aumentar el delay para no saturar el servidor con peticiones continuas
}
// Función para extraer el texto del cuerpo de la respuesta HTML
String extractBodyText(String html) {
int bodyStart = html.indexOf("<body>");
int bodyEnd = html.indexOf("</body>");
if (bodyStart != -1 && bodyEnd != -1) {
String bodyContent = html.substring(bodyStart + 6, bodyEnd); // +6 para omitir <body>
bodyContent.trim();
return bodyContent;
} else {
return "No body found";
}
}
// Función para mostrar el mensaje en la pantalla LCD
void displayMessage(String message) {
lcd.clear();
lcd.setCursor(0, 0);
if (message.length() > 16) {
lcd.print(message.substring(0, 16)); // Mostrar los primeros 16 caracteres en la primera fila
lcd.setCursor(0, 1);
lcd.print(message.substring(16)); // Mostrar los siguientes 16 caracteres en la segunda fila (si hay más de 16)
} else {
lcd.print(message); // Mostrar el mensaje completo si es menor o igual a 16 caracteres
}
}
void connectWiFi() {
WiFi.mode(WIFI_OFF);
delay(1000);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("Connected to: ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}