#include <Keypad.h>
#include <WiFi.h>
#include <LiquidCrystal_I2C.h>
// Define keypad configuration
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {33, 25, 26, 14};
byte colPins[COLS] = {27, 13, 16, 4};
// Initialize keypad object
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Initialize WiFi client and receiver details
WiFiClient client;
const char* receiverIP = "receiver_ip_address"; // Replace with actual IP
const int receiverPort = 8888;
// Define variables for current state of the system
String inputToken = ""; // Buffer for user input token
int currentToken = 0; // Current token being served
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Connect to WiFi network
connectToWiFi();
}
void loop() {
// Get key pressed from keypad
char key = keypad.getKey();
// Check if a key is pressed
if (key) {
// Check if the pressed key is 'A' for manual token entry
if (key == 'A') {
// Transmit manually entered token and random counter number
transmitData(inputToken.toInt(), random(1, 5)); // Random counter number between 1 and 4
inputToken = ""; // Clear input token buffer
} else if (key == 'B') { // Check if the pressed key is 'B' for token increment
// Increment token and reset to 1 if it reaches 100
currentToken = (currentToken % 100) + 1;
// Transmit incremented token along with random counter number
transmitData(currentToken, random(1, 5)); // Random counter number between 1 and 4
} else if (isdigit(key) || key == '*') { // Check if the pressed key is a digit or '*'
// Append numeric key press to input token buffer
inputToken += key;
// Print current input token for feedback
Serial.println("Current input token: " + inputToken);
} else { // Handle invalid key press
Serial.println("Invalid key press!");
}
}
}
// Function to connect to WiFi network
// int attempts = 0;
// WiFi.begin("your_wifi_ssid", "your_wifi_password");
// // Attempt to connect with exponential backoff
// while (WiFi.status() != WL_CONNECTED && attempts < 5) {
// delay(1000 * (attempts + 1)); // Exponential backoff
// Serial.println("Connecting to WiFi...");
// attempts++;
// }
// // Check if connection is successful
// if (WiFi.status() != WL_CONNECTED) {
// Serial.println("Failed to connect to WiFi.");
// } else {
// Serial.println("Connected to WiFi");
// }
void connectToWiFi() {
Serial.begin(9600);
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
}
// Function to transmit data to receiver
void transmitData(int token, int counter) {
// // Check if client is connected to WiFi
// if (!client.connected()) {
// // Attempt to reconnect if not connected
// if (!client.connect(receiverIP, receiverPort)) {
// Serial.println("Failed to connect to receiver.");
// return;
// }
// }
// Send data to receiver
Serial.println("Counter " + String(counter) + ", Token " + String(token));
client.print("Counter: ");
client.print(counter);
client.print(", Token: ");
client.print(token);
client.println();
}
// #include <Keypad.h>
// #include <WiFi.h>
// #include <LiquidCrystal_I2C.h>
// // Define keypad configuration
// const byte ROWS = 4;
// const byte COLS = 4;
// char keys[ROWS][COLS] = {
// {'1', '2', '3', 'A'},
// {'4', '5', '6', 'B'},
// {'7', '8', '9', 'C'},
// {'*', '0', '#', 'D'}
// };
// byte rowPins[ROWS] = {33, 25, 26, 14};
// byte colPins[COLS] = {27, 13, 22, 4};
// // Initialize keypad object
// Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// // Initialize WiFi client and receiver details
// WiFiClient client;
// const char* receiverIP = "receiver_ip_address"; // Replace with actual IP
// const int receiverPort = 8888;
// // Define variables for current state of the system
// String inputToken = ""; // Buffer for user input token
// int currentToken = 0; // Current token being served
// // Initialize LCD object
// LiquidCrystal_I2C lcd(0x27, 16, 2); // Address 0x27, 16 columns, 2 rows
// void setup() {
// // Initialize serial communication
// Serial.begin(115200);
// // Connect to WiFi network
// connectToWiFi();
// // Initialize LCD
// lcd.init();
// lcd.backlight(); // Turn on backlight
// lcd.clear(); // Clear the display
// }
// void loop() {
// // Get key pressed from keypad
// char key = keypad.getKey();
// // Check if a key is pressed
// if (key) {
// // Check if the pressed key is 'A' for manual token entry
// if (key == 'A') {
// // Transmit manually entered token and random counter number
// transmitData(inputToken.toInt(), random(1, 5)); // Random counter number between 1 and 4
// inputToken = ""; // Clear input token buffer
// } else if (key == 'B') { // Check if the pressed key is 'B' for token increment
// // Increment token and reset to 1 if it reaches 100
// currentToken = (currentToken % 100) + 1;
// // Transmit incremented token along with random counter number
// transmitData(currentToken, random(1, 5)); // Random counter number between 1 and 4
// } else if (isdigit(key) || key == '*') { // Check if the pressed key is a digit or '*'
// // Append numeric key press to input token buffer
// inputToken += key;
// // Print current input token for feedback
// Serial.println("Current input token: " + inputToken);
// } else { // Handle invalid key press
// Serial.println("Invalid key press!");
// }
// }
// }
// // Function to connect to WiFi network
// void connectToWiFi() {
// Serial.begin(9600);
// Serial.print("Connecting to WiFi");
// WiFi.begin("Wokwi-GUEST", "", 6);
// while (WiFi.status() != WL_CONNECTED) {
// delay(100);
// Serial.print(".");
// }
// Serial.println(" Connected!");
// }
// // Function to transmit data to receiver
// void transmitData(int token, int counter) {
// // Send data to receiver
// Serial.println("Counter " + String(counter) + ", Token " + String(token));
// client.print("Counter: ");
// client.print(counter);
// client.print(", Token: ");
// client.print(token);
// client.println();
// // Display data on LCD
// lcd.setCursor(0, 0); // Set cursor to the first column of the first row
// lcd.print("Counter: ");
// lcd.print(counter);
// lcd.setCursor(0, 1); // Set cursor to the first column of the second row
// lcd.print("Token: ");
// lcd.print(token);
// }
// #include <Keypad.h>
// #include <WiFi.h>
// #include <LiquidCrystal_I2C.h>
// // Define keypad configuration
// const byte ROWS = 4;
// const byte COLS = 4;
// char keys[ROWS][COLS] = {
// {'1', '2', '3', 'A'},
// {'4', '5', '6', 'B'},
// {'7', '8', '9', 'C'},
// {'*', '0', '#', 'D'}
// };
// byte rowPins[ROWS] = {33, 25, 26, 14};
// byte colPins[COLS] = {27, 13, 22, 4};
// // Initialize keypad object
// Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// // Initialize WiFi client and receiver details
// WiFiClient client;
// const char* receiverIP = "receiver_ip_address"; // Replace with actual IP
// const int receiverPort = 8888;
// // Define variables for current state of the system
// String inputToken = ""; // Buffer for user input token
// int currentToken = 0; // Current token being served
// // Initialize LCD object
// LiquidCrystal_I2C lcd(0x27, 16, 2); // Address 0x27, 16 columns, 2 rows
// void setup() {
// // Initialize serial communication
// Serial.begin(115200);
// // Connect to WiFi network
// connectToWiFi();
// // Initialize LCD
// lcd.init();
// lcd.backlight(); // Turn on backlight
// lcd.clear(); // Clear the display
// }
// void loop() {
// // Get key pressed from keypad
// char key = keypad.getKey();
// // Check if a key is pressed
// if (key) {
// // Check if the pressed key is 'A' for manual token entry
// if (key == 'A') {
// // Transmit manually entered token and random counter number
// transmitData(inputToken.toInt(), random(1, 5)); // Random counter number between 1 and 4
// inputToken = ""; // Clear input token buffer
// } else if (key == 'B') { // Check if the pressed key is 'B' for token increment
// // Increment token and reset to 1 if it reaches 100
// currentToken = (currentToken % 100) + 1;
// // Transmit incremented token along with random counter number
// transmitData(currentToken, random(1, 5)); // Random counter number between 1 and 4
// } else if (isdigit(key) || key == '*') { // Check if the pressed key is a digit or '*'
// // Append numeric key press to input token buffer
// inputToken += key;
// // Print current input token for feedback
// Serial.println("Current input token: " + inputToken);
// } else { // Handle invalid key press
// Serial.println("Invalid key press!");
// }
// }
// }
// // Function to connect to WiFi network
// void connectToWiFi() {
// Serial.begin(9600);
// Serial.print("Connecting to WiFi");
// WiFi.begin("Wokwi-GUEST", "", 6);
// while (WiFi.status() != WL_CONNECTED) {
// delay(100);
// Serial.print(".");
// }
// Serial.println(" Connected!");
// }
// // Function to transmit data to receiver
// void transmitData(int token, int counter) {
// // Send data to receiver
// Serial.println("Counter " + String(counter) + ", Token " + String(token));
// client.print("Counter: ");
// client.print(counter);
// client.print(", Token: ");
// client.print(token);
// client.println();
// // Display data on LCD
// lcd.setCursor(0, 0); // Set cursor to the first column of the first row
// lcd.print("Counter: ");
// lcd.print(counter);
// lcd.setCursor(0, 1); // Set cursor to the first column of the second row
// lcd.print("Token: ");
// lcd.print(token);
// }