/********************************************************************
Este ejemplo pinta en la pantalla el pixel tocado.
Se ha modificado levemente, ya que dibuja el pixel y además todo el cuadrado
que lo rodea , para que se vea mejor.
*********************************************************************/
#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>
// 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);
#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 = "MCR_exam_sub";
// instanciar objetos
WiFiClient espClient;
PubSubClient mqttClient(espClient);
String content = "";
char readChar;
String strX;
String strY;
unsigned int numX;
unsigned int numY;
TS_Point p_received;
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);
SuscribeMqtt();
mqttClient.setCallback(OnMqttReceived);
}
void ConnectMqtt()
{
while (!mqttClient.connected())
{
Serial.print("Starting MQTT connection...");
if (mqttClient.connect(MQTT_CLIENT_NAME))
{
SuscribeMqtt();
}
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();
}
void SuscribeMqtt()
{
mqttClient.subscribe("MCR/prueba");
}
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 DecodeMsg(const String msg) {
if (msg == "clear") {
tft.fillScreen(ILI9341_WHITE);
}
else {
readChar = ' ';
strX = "";
strY = "";
int j = 0;
while (readChar != ',') {
readChar = (char)msg[j];
strX.concat(readChar);
j++;
}
strX.remove(strX.length() - 1); // Elimino la ","
numX = (unsigned int)strX.toInt();
readChar = ' ';
while (readChar != '\0') {
readChar = (char)msg[j];
strY.concat(readChar);
j++;
}
strY.remove(strY.length() - 1); // Elimino el último carácter
numY = (unsigned int)strY.toInt();
p_received.x = numX;
p_received.y = numY;
DrawBigPixel(p_received, ILI9341_RED);
}
}
void OnMqttReceived(char *topic, byte *payload, unsigned int length)
{
Serial.print("Received on ");
Serial.print(topic);
Serial.print(": ");
content = "";
for (size_t i = 0; i < length; i++)
{
content.concat((char)payload[i]);
}
Serial.print(content);
Serial.println();
DecodeMsg(content);
}
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_WHITE);
// Giro la pantalla para que la distribución del display y el sensor capacitivo sean iguales
tft.setRotation(2);
}
void loop()
{
HandleMqtt();
delay(10);
}Loading
ili9341-cap-touch
ili9341-cap-touch