#include #include #include "TFT_eSPI.h" // --- CONFIGURACIÓN --- const char* ssid = "MOVISTAR_DF4F_INV"; const char* password = "mTPYF7AgD2q6TEN4x7bz"; 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 msg = ""; for (int i = 0; i < length; i++) msg += (char)payload[i]; tft.setTextSize(3); // Texto grande para los valores if (String(topic) == topic_foto) { tft.fillRect(160, 40, 150, 30, TFT_BLACK); tft.setCursor(160, 40); tft.setTextColor(TFT_YELLOW); tft.print(msg + "W"); } else if (String(topic) == topic_cons) { tft.fillRect(160, 90, 150, 30, TFT_BLACK); tft.setCursor(160, 90); tft.setTextColor(TFT_RED); tft.print(msg + "W"); } else if (String(topic) == topic_temp) { tft.fillRect(160, 140, 150, 30, TFT_BLACK); tft.setCursor(160, 140); tft.setTextColor(TFT_CYAN); tft.print(msg + "C"); } } void reconnect() { while (!client.connected()) { tft.fillRect(0, 210, 320, 30, TFT_BLACK); tft.setCursor(10, 210); tft.setTextSize(1); tft.setTextColor(TFT_WHITE); tft.print("Conectando MQTT..."); String clientId = "Wio-" + String(random(0xffff), HEX); if (client.connect(clientId.c_str())) { tft.fillRect(0, 210, 320, 30, TFT_BLACK); tft.setCursor(10, 210); tft.print("MQTT: ONLINE"); client.subscribe(topic_foto); client.subscribe(topic_cons); client.subscribe(topic_temp); } else { delay(5000); } } } void setup() { tft.begin(); tft.setRotation(3); pinMode(LCD_BACKLIGHT, OUTPUT); digitalWrite(LCD_BACKLIGHT, HIGH); tft.fillScreen(TFT_BLACK); tft.setTextColor(TFT_WHITE); tft.setTextSize(2); // Títulos fijos tft.setCursor(10, 40); tft.print("SOLAR:"); tft.setCursor(10, 90); tft.print("CONSUMO:"); tft.setCursor(10, 140); tft.print("TEMP:"); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); } client.setServer(mqtt_server, mqtt_port); client.setCallback(callback); } void loop() { if (!client.connected()) reconnect(); client.loop(); }