#include #include #include "TFT_eSPI.h" // --- DATOS WIFI --- const char* ssid = "MOVISTAR_DF4F_INV"; const char* password = "mTPYF7AgD2q6TEN4x7bz"; // --- DATOS BROKER --- const char* mqtt_server = "89.116.24.168"; const int mqtt_port = 31160; // Topics const char* topic_foto = "fabacademy/grupo/fotovoltaica"; const char* topic_cons = "fabacademy/grupo/consumo"; const char* topic_temp = "fabacademy/grupo/temperatura"; TFT_eSPI tft; WiFiClient wioClient; PubSubClient client(wioClient); void callback(char* topic, byte* payload, unsigned int length) { String message = ""; for (int i = 0; i < length; i++) message += (char)payload[i]; Serial.print("Mensaje recibido en ["); Serial.print(topic); Serial.print("]: "); Serial.println(message); // Actualizar la pantalla tft.setTextSize(3); if (String(topic) == topic_foto) { tft.fillRect(160, 40, 140, 30, TFT_BLACK); tft.setCursor(160, 40); tft.setTextColor(TFT_YELLOW); tft.print(message); } else if (String(topic) == topic_cons) { tft.fillRect(160, 90, 140, 30, TFT_BLACK); tft.setCursor(160, 90); tft.setTextColor(TFT_RED); tft.print(message); } else if (String(topic) == topic_temp) { tft.fillRect(160, 140, 140, 30, TFT_BLACK); tft.setCursor(160, 140); tft.setTextColor(TFT_CYAN); tft.print(message); } } void reconnect() { while (!client.connected()) { Serial.print("Intentando conexión MQTT..."); String clientId = "Wio-User-" + String(random(0xffff), HEX); if (client.connect(clientId.c_str())) { Serial.println("¡CONECTADO AL BROKER!"); client.subscribe(topic_foto); client.subscribe(topic_cons); client.subscribe(topic_temp); } else { Serial.print("Fallo, rc="); Serial.print(client.state()); Serial.println(" reintentando en 5s"); delay(5000); } } } void setup() { Serial.begin(115200); // INICIALIZAR PANTALLA Y LUZ tft.begin(); tft.setRotation(3); pinMode(LCD_BACKLIGHT, OUTPUT); digitalWrite(LCD_BACKLIGHT, HIGH); // <--- ESTO ENCIENDE LA PANTALLA tft.fillScreen(TFT_BLACK); tft.setTextColor(TFT_WHITE); tft.setTextSize(2); tft.setCursor(10, 10); tft.println("Conectando WiFi..."); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } tft.fillScreen(TFT_BLACK); tft.setCursor(0, 40); tft.print(" Solar:"); tft.setCursor(0, 90); tft.print(" Consumo:"); tft.setCursor(0, 140); tft.print(" Temp:"); client.setServer(mqtt_server, mqtt_port); client.setCallback(callback); } void loop() { if (!client.connected()) { reconnect(); } client.loop(); }