#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <WebServer.h>
#include <Keypad.h>
// LCD setup
LiquidCrystal_I2C lcd(0x27, 20, 4); // Adjust the address and size as needed
// Wi-Fi setup
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Web server
WebServer server(80);
// Keypad setup
const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {19, 18, 5, 17}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {16, 4, 0, 2}; // Connect to the column pinouts of the keypad
Keypad keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String inputData = "";
unsigned long lastPressTime = 0;
int pressCount = 0;
char lastKey = '\0';
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize LCD
lcd.begin(20, 4);
lcd.backlight();
lcd.print("WiFi AP Started");
// Start Wi-Fi Access Point
WiFi.softAP(ssid, password);
Serial.println("Access Point Started");
Serial.print("IP Address: ");
Serial.println(WiFi.softAPIP());
// Setup web server routes
server.on("/", HTTP_GET, handleRoot);
server.on("/send", HTTP_POST, handleSend);
server.begin();
}
void loop() {
server.handleClient();
// Check for keypad input
char key = keypad.getKey();
if (key) {
unsigned long currentTime = millis();
// Check if the same key is pressed again within 300 ms
if (key == lastKey && (currentTime - lastPressTime < 300)) {
pressCount++;
} else {
pressCount = 1; // Reset count for a new key press
}
lastPressTime = currentTime;
lastKey = key;
// Process the key press based on the count
char character = getCharacter(key, pressCount);
if (character != '\0') {
inputData += character; // Append the selected character to inputData
lcd.clear();
lcd.print("Input: " + inputData);
}
}
}
char getCharacter(char key, int count) {
switch (key) {
case '1':
return (count == 1) ? 'A' : (count == 2) ? 'B' : (count == 3) ? 'C' : '\0';
case '2':
return (count == 1) ? 'D' : (count == 2) ? 'E' : (count == 3) ? 'F' : '\0';
case '3':
return (count == 1) ? 'G' : (count == 2) ? 'H' : (count == 3) ? 'I' : '\0';
case '4':
return (count == 1) ? 'J' : (count == 2) ? 'K' : (count == 3) ? 'L' : '\0';
case '5':
return (count == 1) ? 'M' : (count == 2) ? 'N' : (count == 3) ? 'O' : '\0';
case '6':
return (count == 1) ? 'P' : (count == 2) ? 'Q' : (count == 3) ? 'R' : (count == 4) ? 'S' : '\0';
case '7':
return (count == 1) ? 'T' : (count == 2) ? 'U' : (count == 3) ? 'V' : '\0';
case '8':
return (count == 1) ? 'W' : (count == 2) ? 'X' : (count == 3) ? 'Y' : (count == 4) ? 'Z' : '\0';
case '9':
return ' '; // Space for key '9'
default:
return '\0'; // No valid character
}
}
void handleRoot() {
String html = "<html><body><h1>ESP32 Keypad Input</h1>";
html += "<form action=\"/send\" method=\"POST\">";
html += "<input type=\"text\" name=\"inputData\" value=\"" + inputData + "\"><br>";
html += "<input type=\"submit\" value=\"Send\">";
html += "</form></body></html>";
server.send(200, "text/html", html);
}
void handleSend() {
// Handle the input data sent from the form
String inputDataReceived = server.arg("inputData");
Serial.println("Received Input: " + inputDataReceived);
server.send(200, "text/plain", "Input received: " + inputDataReceived);
}