#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Adafruit_ILI9341.h>
#include <Arduino.h>
#include <Adafruit_FT6206.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Defining the WiFi channel speeds up the connection:
#define WIFI_CHANNEL 6
// constantes del MQTT
// direccion broker, puerto, y nombre cliente
const char *MQTT_BROKER_ADRESS = "mqtt.eclipseprojects.io";
//const char *MQTT_BROKER_ADRESS = "test.mosquitto.org";
const uint16_t MQTT_PORT = 1883;
const char *MQTT_CLIENT_NAME = "ARP_PRUEBA";
#define topicName "arp/PRUEBA"
// instanciar objetos
WiFiClient espClient;
PubSubClient mqttClient(espClient);
String payload;
String content = "";
void ConnectWiFi()
{
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
Serial.print("Connecting to WiFi ");
Serial.print(WIFI_SSID);
// Wait for connection
while (WiFi.status() != WL_CONNECTED)
{
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void InitMqtt()
{
mqttClient.setServer(MQTT_BROKER_ADRESS, MQTT_PORT);
}
void ConnectMqtt()
{
while (!mqttClient.connected())
{
Serial.print("Starting MQTT connection...");
if (mqttClient.connect(MQTT_CLIENT_NAME))
{
Serial.print("Client connected");
}
else
{
Serial.print("Failed MQTT connection, rc=");
Serial.print(mqttClient.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void HandleMqtt()
{
if (!mqttClient.connected())
{
ConnectMqtt();
}
mqttClient.loop();
}
String GenerateMsg (const TS_Point point) {
String msg;
msg = String(point.x) + "," + String(point.y);
return msg;
}
void PublishMqtt(String data)
{
payload = "";
payload = data;
mqttClient.publish(topicName, (char *)payload.c_str());
}
// Identificador controlador táctil capacitivo
#define I2C_cap_SDA 10
#define I2C_cap_SCL 8
Adafruit_FT6206 ctp = Adafruit_FT6206();
// Identificador controlador Display SPI
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// Variables de tipo TS_Point para almacenar los píxeles tocados en la pantalla
TS_Point p_current;
TS_Point p_prev;
// Botones
#define Button 17
volatile bool clearScreen;
void IRAM_ATTR ISR_B (void) {
clearScreen = true;
}
void DrawBigPixel(const TS_Point point, const uint16_t color) {
tft.drawPixel(point.x, point.y, color);
tft.drawPixel(point.x - 1, point.y + 1, color);
tft.drawPixel(point.x, point.y + 1, color);
tft.drawPixel(point.x + 1, point.y + 1, color);
tft.drawPixel(point.x - 1, point.y, color);
tft.drawPixel(point.x + 1, point.y, color);
tft.drawPixel(point.x - 1, point.y - 1, color);
tft.drawPixel(point.x, point.y - 1, color);
tft.drawPixel(point.x + 1, point.y - 1, color);
}
void setup()
{
// Inicializar puerto serie
Serial.begin(115200);
Serial.println("Pintar con la pantalla capacitiva");
ConnectWiFi();
InitMqtt();
// 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)
;
}
Serial.println("Capacitive touchscreen started");
// Rellenar el fondo de la pantalla en blanco
tft.fillScreen(ILI9341_BLACK);
// Giro la pantalla para que la distribución del display y el sensor capacitivo sean iguales
// Botón
pinMode(Button, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(Button), &ISR_B, FALLING);
clearScreen = false;
}
void loop()
{
HandleMqtt();
// Si se ha tocado la pantalla
if (ctp.touched())
{
p_current = ctp.getPoint(); // Guardo el pixel que se ha tocado
if (p_current != p_prev) // Si no es el mismo que se ha tocado previamente
{
DrawBigPixel(p_current, ILI9341_WHITE);
// Mando el mensaje
PublishMqtt(GenerateMsg(p_current));
Serial.printf("x:%d, y:%d \r\n", p_current.x, p_current.y); // Escribo por el puerto serie el pixel presionado
}
p_prev = p_current; // Actualizo el píxel actual
}
if (clearScreen) {
clearScreen = false;
tft.fillScreen(ILI9341_BLACK);
PublishMqtt("clear");
}
delay(10);
}Loading
ili9341-cap-touch
ili9341-cap-touch