//Al tocar la pantalla dibujar los puntos e enviar la coordenada tocada (x,y) en uno o dos topics mediante MQTT.
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
#include <ArduinoJson.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_FT6206 ctp = Adafruit_FT6206();
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
TS_Point p_current;
TS_Point p_prev;
String payload;
String content = "";
String readTopic = "";
char readChar;
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/punto");
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 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 pos_x y pos_y
int posX = doc["pos_x"];
int posY = doc["pos_y"];
Serial.print("Coordenada X: ");
Serial.println(posX);
Serial.print("Coordenada Y: ");
Serial.println(posY);
// Actualizar las coordenadas actuales
p_current.x = posX;
p_current.y = posY;
// Dibujar en la pantalla
tft.drawPixel(p_current.x, p_current.y, ILI9341_WHITE);
}
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);
if (strcmp(topic, "mmggjj/punto") == 0) {
processPoint(message);
}
}
void setup()
{
// Inicializar puerto serie
Serial.begin(115200);
Serial.println("Dibujar el código secreto");
// Inicializar display
Wire.setPins(I2C_cap_SDA, I2C_cap_SCL);
tft.begin();
/*
// Inicializar sensor capacitivo
if (!ctp.begin(1))
{
Serial.println("No se pudo inicializar el controlador FT6206");
while (true);
}
*/
tft.setRotation(2);
Serial.println("Capacitive touchscreen started");
tft.fillScreen(ILI9341_BLACK);
tft.setRotation(2);
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);
}
*/