#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Adafruit_ILI9341.h>
#include <Wire.h>
#include <Adafruit_FT6206.h>
#define BLYNK_TEMPLATE_ID "TMPL2_GVjrn5v"
#define BLYNK_TEMPLATE_NAME "Security System"
#define BLYNK_AUTH_TOKEN "BSbwDLrlD1gHQpDafZ_ETx1x0bk57voB"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
#define TFT_CS 15 // Chip Select
#define TFT_DC 2 // Data/Command
#define TFT_RST -1 // Reset pin (usa -1 si no tienes)
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
Adafruit_FT6206 ctp = Adafruit_FT6206();
#define BUTTON_SIZE 40
#define BUTTON_PADDING 10
#define BUTTON_COLOR ILI9341_WHITE
#define BUTTON_PRESS_COLOR ILI9341_LIGHTGREY
#define DISPLAY_HEIGHT 240
#define DISPLAY_WIDTH 320
#define BAR_HEIGHT 40
const int ledPin = 27;
const char keys[4][4] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
String input = ""; // Para almacenar las teclas ingresadas
const String correctCode = "1234"; // Clave correcta para acceso
// Calcula el ancho y altura total del teclado
int keyboardWidth = (BUTTON_SIZE + BUTTON_PADDING) * 4 - BUTTON_PADDING;
int keyboardHeight = (BUTTON_SIZE + BUTTON_PADDING) * 4 - BUTTON_PADDING;
// Ajuste de desplazamientos para centrar el teclado y bajarlo un poco
int offsetX = (DISPLAY_WIDTH - keyboardWidth) / 2 - 40; // Mover un poco hacia la izquierda
int offsetY = (DISPLAY_HEIGHT - BAR_HEIGHT - keyboardHeight) / 2 + BAR_HEIGHT + 30; // Bajar el teclado un poco más
void setup() {
Blynk.begin(auth, ssid, pass);
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
while (!Serial); // Espera el monitor serial
Serial.println(F("Teclado 4x4!"));
tft.begin();
tft.fillScreen(ILI9341_BLACK);
if (!ctp.begin(10)) {
Serial.println("No se pudo iniciar el controlador de pantalla táctil FT6206");
while (1);
}
Serial.println("Controlador de pantalla táctil capacitiva iniciado");
drawKeyboard();
drawInputBar();
}
BLYNK_WRITE(V4) {
int pinValue = param.asInt(); // Obtener el valor del botón (0 o 1)
// Enciende o apaga el LED según el valor del botón
digitalWrite(ledPin, pinValue);
Blynk.virtualWrite(V1, "En espera"); // Escribe en el pin virtual V1
Blynk.virtualWrite(V2, 0);
if (pinValue == LOW) {
// Si el botón está encendido, envía "Acceso Permitido" a la app de Blynk
Blynk.virtualWrite(V1, "En Espera"); // Escribe en el pin virtual V1
Blynk.virtualWrite(V2, 0); // Enciende el LED en la app Blynk (si tienes otro widget vinculado)
} else {
// Si el botón está apagado, envía otro mensaje y apaga el LED
Blynk.virtualWrite(V1, "Acceso Permitido"); // Escribe en el pin virtual V1
Blynk.virtualWrite(V2, 255);
}
}
void loop() {
Blynk.run();
if (!ctp.touched()) {
return;
}
TS_Point p = ctp.getPoint();
p.x = map(p.x, 0, 240, 240, 0);
p.y = map(p.y, 0, 320, 320, 0);
int col = (p.x - offsetX) / (BUTTON_SIZE + BUTTON_PADDING);
int row = (p.y - offsetY) / (BUTTON_SIZE + BUTTON_PADDING);
if (row >= 0 && row < 4 && col >= 0 && col < 4) {
char pressedKey = keys[row][col];
Serial.print("Tecla presionada: ");
Serial.println(pressedKey);
// Añade la tecla a la entrada
if (input.length() < 4) {
input += pressedKey; // Solo permite hasta 4 dígitos
drawInputBar(); // Redibuja la barra de entrada
}
// Verifica si la entrada tiene 4 caracteres
if (input.length() == 4) {
if (input == correctCode) {
showAccessGranted();
digitalWrite(ledPin, HIGH);
delay(1000); // Espera 1 segundo
} else {
showAccessDenied();
}
delay(2000); // Muestra el mensaje por 2 segundos
resetInput(); // Resetea la entrada para un nuevo intento
}
highlightButton(col, row);
delay(200); // Retardo para evitar múltiples registros
drawKeyboard(); // Redibuja el teclado
}
}
void drawKeyboard() {
for (int row = 0; row < 4; row++) {
for (int col = 0; col < 4; col++) {
int x = col * (BUTTON_SIZE + BUTTON_PADDING) + offsetX;
int y = row * (BUTTON_SIZE + BUTTON_PADDING) + offsetY;
tft.fillRect(x, y, BUTTON_SIZE, BUTTON_SIZE, BUTTON_COLOR);
tft.drawRect(x, y, BUTTON_SIZE, BUTTON_SIZE, ILI9341_BLACK);
tft.setCursor(x + BUTTON_SIZE / 2 - 10, y + BUTTON_SIZE / 2 - 10);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.print(keys[row][col]);
}
}
}
void drawInputBar() {
// Dibuja la barra de entrada
tft.fillRect(0, 0, DISPLAY_WIDTH, BAR_HEIGHT, ILI9341_BLUE);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(10, 10);
tft.print("Entrada: ");
tft.print(input);
}
void highlightButton(int col, int row) {
int x = col * (BUTTON_SIZE + BUTTON_PADDING) + offsetX;
int y = row * (BUTTON_SIZE + BUTTON_PADDING) + offsetY;
tft.fillRect(x, y, BUTTON_SIZE, BUTTON_SIZE, BUTTON_PRESS_COLOR);
tft.drawRect(x, y, BUTTON_SIZE, BUTTON_SIZE, ILI9341_BLACK);
tft.setCursor(x + BUTTON_SIZE / 2 - 10, y + BUTTON_SIZE / 2 - 10);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.print(keys[row][col]);
}
void showAccessGranted() {
tft.fillScreen(ILI9341_GREEN);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.setCursor(50, DISPLAY_HEIGHT / 2 - 20);
tft.print("Acceso Permitido");
Blynk.virtualWrite(V1, "Acceso Permitido");
Blynk.virtualWrite(V2, 255); // Enciende el LED
}
void showAccessDenied() {
tft.fillScreen(ILI9341_RED);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(50, DISPLAY_HEIGHT / 2 - 20);
tft.print("Acceso Denegado");
Blynk.virtualWrite(V1, "Acceso Denegado");
Blynk.virtualWrite(V3, 255); // Enciende el LED
}
void resetInput() {
input = ""; // Resetea la entrada
tft.fillScreen(ILI9341_BLACK); // Vuelve el fondo a negro
drawInputBar(); // Redibuja la barra de entrada
drawKeyboard(); // Redibuja el teclado
Blynk.virtualWrite(V1, "En Espera");
Blynk.virtualWrite(V2, 0); // Apaga el LED
Blynk.virtualWrite(V3, 0); // Apaga el LED
digitalWrite(ledPin, LOW);
delay(1000); // Espera 1 segundo
}Loading
ili9341-cap-touch
ili9341-cap-touch