#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define WIFI_CHANNEL 6
#define TFT_DC 2
#define TFT_CS 15
#define I2C_cap_SDA 21
#define I2C_cap_SCL 22
const char *MQTT_BROKER_ADRESS = "test.mosquitto.org";
const uint16_t MQTT_PORT = 1883;
const char *MQTT_CLIENT_NAME = "mario_sub";
WiFiClient espClient;
PubSubClient mqttClient(espClient);
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
void ConnectWiFi() {
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
Serial.print("Connecting to WiFi ");
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
}
void InitMqtt() {
mqttClient.setServer(MQTT_BROKER_ADRESS, MQTT_PORT);
mqttClient.setCallback(OnMqttReceived);
}
void ConnectMqtt() {
while (!mqttClient.connected()) {
Serial.print("Starting MQTT connection...");
if (mqttClient.connect(MQTT_CLIENT_NAME)) {
mqttClient.subscribe("mmggjj/palabra"); // Subscribirse al topic
Serial.println("Subscribed to topic");
} else {
Serial.print("Failed, rc=");
Serial.println(mqttClient.state());
delay(5000);
}
}
}
void HandleMqtt() {
if (!mqttClient.connected()) {
ConnectMqtt();
}
mqttClient.loop();
}
void OnMqttReceived(char *topic, byte *payload, unsigned int length) {
Serial.print("Received on ");
Serial.print(topic);
Serial.print(": ");
String message = "";
for (size_t i = 0; i < length; i++) {
message += (char)payload[i];
}
Serial.println(message);
// Verifica el topic y escribe el mensaje en la pantalla
if (strcmp(topic, "mmggjj/palabra") == 0) {
writeTextOnScreen(message);
}
}
void writeTextOnScreen(String text) {
// Borra la pantalla y escribe el texto recibido
tft.fillScreen(ILI9341_BLACK); // Limpia la pantalla
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(10, 50); // Ajusta la posición del texto
tft.println(text); // Escribe el texto recibido
}
void setup() {
Serial.begin(115200);
Serial.println("Pantalla TFT con MQTT");
tft.begin();
tft.setRotation(0);
tft.fillScreen(ILI9341_BLACK);
ConnectWiFi();
InitMqtt();
}
void loop() {
HandleMqtt();
delay(10);
}
/*
void processPoint(String message) {
Serial.print("Mensaje recibido: ");
Serial.println(message);
// Crear un buffer dinámico para el JSON
DynamicJsonDocument doc(1024);
// Parsear el mensaje JSON
DeserializationError error = deserializeJson(doc, message);
// Verificar errores en el parseo
if (error) {
Serial.print("Error al parsear JSON: ");
Serial.println(error.c_str());
return;
}
// Extraer los valores de "word" y "number"
const char* word = doc["word"];
int number = doc["number"];
Serial.print("Palabra: ");
Serial.println(word);
Serial.print("Número: ");
Serial.println(number);
// Aquí puedes usar los datos según tu aplicación
// Por ejemplo, actualizando información o realizando acciones:
Serial.println("Procesando la palabra y el número...");
// Si tienes una pantalla, puedes mostrar la palabra o el número:
// tft.print(word);
// tft.print(number);
}
*/