#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
#include <SPI.h>
#include <Wire.h>
#include <WiFi.h>
#include <Preferences.h>
// Pines pantalla ILI9341
#define TFT_CS 15
#define TFT_DC 2
#define TFT_RST 4
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
Adafruit_FT6206 ts = Adafruit_FT6206();
Preferences preferences;
String ssid = "";
String password = "";
bool shiftMode = false;
bool inKeyboard = false; // modo menú vs teclado
// Teclado
const char *keys[5][10] = {
{"1","2","3","4","5","6","7","8","9","0"},
{"q","w","e","r","t","y","u","i","o","p"},
{"a","s","d","f","g","h","j","k","l","BORRAR"},
{"z","x","c","v","b","n","m","SHIFT","OK","."},
{" "," "," "," "," "," "," "," "," "," "}
};
int keyWidth = 32;
int keyHeight = 32;
// Redes detectadas
int nNetworks = 0;
String networks[20]; // hasta 20 redes
// ---------------- DIBUJAR TECLADO ----------------
void drawKeyboard() {
tft.fillScreen(ILI9341_BLACK);
tft.setTextSize(2);
tft.setTextColor(ILI9341_CYAN);
tft.setCursor(10, 10);
tft.print("SSID: " + ssid);
// teclas
for (int row = 0; row < 5; row++) {
for (int col = 0; col < 10; col++) {
int x = col * keyWidth;
int y = row * keyHeight + 40;
tft.drawRect(x, y, keyWidth, keyHeight, ILI9341_WHITE);
String label = keys[row][col];
if (shiftMode && label.length() == 1 && isalpha(label[0])) {
label.toUpperCase();
}
tft.setCursor(x + 4, y + 10);
tft.setTextSize(1);
tft.setTextColor(ILI9341_WHITE);
tft.print(label);
}
}
// contraseña escrita
tft.fillRect(0, 220, 240, 20, ILI9341_BLACK);
tft.setCursor(10, 225);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.print("Pass: " + password);
}
// ---------------- MENÚ DE REDES ----------------
void drawNetworkMenu() {
tft.fillScreen(ILI9341_BLACK);
tft.setTextSize(2);
tft.setTextColor(ILI9341_YELLOW);
tft.setCursor(10, 10);
tft.print("Redes WiFi:");
for (int i = 0; i < nNetworks && i < 6; i++) {
int y = 40 + i * 30;
tft.drawRect(10, y, 220, 28, ILI9341_WHITE);
tft.setCursor(15, y + 8);
tft.setTextSize(2);
tft.setTextColor(ILI9341_CYAN);
tft.print(networks[i]);
}
}
// ---------------- CONEXIÓN ----------------
void tryConnect() {
tft.fillRect(0, 260, 240, 40, ILI9341_BLACK);
tft.setCursor(10, 270);
tft.setTextColor(ILI9341_YELLOW);
tft.setTextSize(2);
tft.print("Conectando...");
WiFi.begin(ssid.c_str(), password.c_str());
int tries = 0;
while (WiFi.status() != WL_CONNECTED && tries < 20) {
delay(500);
tries++;
}
if (WiFi.status() == WL_CONNECTED) {
tft.fillRect(0, 260, 240, 40, ILI9341_BLACK);
tft.setCursor(10, 270);
tft.setTextColor(ILI9341_GREEN);
tft.print("Conectado!");
// Guardar credenciales
preferences.begin("wifi", false);
preferences.putString("ssid", ssid);
preferences.putString("pass", password);
preferences.end();
} else {
tft.fillRect(0, 260, 240, 40, ILI9341_BLACK);
tft.setCursor(10, 270);
tft.setTextColor(ILI9341_RED);
tft.print("Error WiFi!");
}
}
// ---------------- TECLAS ----------------
String getKeyPress(int16_t x, int16_t y) {
int col = x / keyWidth;
int row = (y - 40) / keyHeight;
if (row >= 0 && row < 5 && col >= 0 && col < 10) {
String key = keys[row][col];
if (key == "BORRAR") {
if (password.length() > 0) password.remove(password.length() - 1);
} else if (key == "OK") {
tryConnect();
} else if (key == "SHIFT") {
shiftMode = !shiftMode;
} else {
if (shiftMode) {
key.toUpperCase();
}
password += key;
}
}
return password;
}
// ---------------- SETUP ----------------
void setup() {
Serial.begin(115200);
tft.begin();
if (!ts.begin(40)) {
Serial.println("No Touch Found");
while (1);
}
// Credenciales guardadas
preferences.begin("wifi", true);
String savedSSID = preferences.getString("ssid", "");
String savedPASS = preferences.getString("pass", "");
preferences.end();
if (savedSSID != "" && savedPASS != "") {
ssid = savedSSID;
password = savedPASS;
WiFi.begin(ssid.c_str(), password.c_str());
int tries = 0;
while (WiFi.status() != WL_CONNECTED && tries < 20) {
delay(500);
tries++;
}
if (WiFi.status() == WL_CONNECTED) {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(10, 100);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.print("Conectado a:");
tft.setCursor(10, 130);
tft.print(ssid);
return;
}
}
// Si no conecta → escaneo
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
nNetworks = WiFi.scanNetworks();
for (int i = 0; i < nNetworks && i < 20; i++) {
networks[i] = WiFi.SSID(i);
}
drawNetworkMenu();
}
// ---------------- LOOP ----------------
void loop() {
if (ts.touched()) {
TS_Point p = ts.getPoint();
int16_t x = map(p.y, 0, 240, 0, 240);
int16_t y = map(p.x, 0, 320, 0, 320);
if (!inKeyboard) {
// Selección de red
for (int i = 0; i < nNetworks && i < 6; i++) {
int yTop = 40 + i * 30;
if (x > 10 && x < 230 && y > yTop && y < yTop + 28) {
ssid = networks[i];
password = "";
inKeyboard = true;
drawKeyboard();
break;
}
}
} else {
// Teclado
getKeyPress(x, y);
drawKeyboard();
}
delay(200);
}
}
Loading
ili9341-cap-touch
ili9341-cap-touch