Analyze this code and fix the bug that the Press o...
Erstellt am: 8. März 2025
Erstellt am: 8. März 2025
Analyze this code and fix the bug that the Press of button "1" is only recognized once, but every other button is recognized when pressed consecutively:
"#include <ESP8266WiFi.h>
#include <Keypad.h>
const char* ssid = "KeypadAP"; // Receiver's Access Point SSID
const char* password = "12345678"; // Receiver's Access Point Password
const IPAddress receiverIP(192, 168, 4, 1); // Receiver's IP Address
const uint16_t receiverPort = 80; // Receiver's Port Number
// Keypad configuration
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] = {D0, D5, D6, D7}; // Row pins connected to D0, D5, D6, D7
byte colPins[COLS] = {D8, D3, D2, D1}; // Column pins connected to D8, D3, D1, D1
//byte rowPins[ROWS] = {D1, D2, D3, D8}; // Row pins connected to D0, D5, D6, D7
//byte colPins[COLS] = {D7, D6, D5, D0}; // Column pins connected to D8, D3, D1, D1
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
WiFiClient client;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password); // Connect to receiver's Access Point
Serial.print("Connecting to Receiver");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to Receiver!");
}
void loop() {
if (!client.connected()) {
if (client.connect(receiverIP, receiverPort)) {
Serial.println("Connected to Receiver.");
} else {
Serial.println("Failed to connect to Receiver.");
delay(1000);
return;
}
}
char key = keypad.getKey();
if (key) {
Serial.print("Key Pressed: ");
Serial.println(key);
client.print(key); // Send key to the receiver
}
}"
A common cause of “only the first press of ‘1’ works” on an ESP8266 keypad is that pin D0 (GPIO16) is not a good choice for normal keypad scanning. On most ESP8266 boards (e.g. NodeMCU/WeMos D1 mini), D0/GPIO16 is tied to the reset circuitry or used for deep sleep wake‑up. It rarely behaves like a regular input pin in this scenario, and often allows only one “low” reading before it stops working.
Simply avoid using D0
as either a row pin or a column pin for your keypad. In your current code:
cppbyte rowPins[ROWS] = {D0, D5, D6, D7}; // <-- D0 can cause trouble byte colPins[COLS] = {D8, D3, D2, D1};
the very first row (where '1'
lives) is scanned on D0, and that is why you see the one-and-done behavior for button '1'
.
Use any of the other standard GPIO pins on the NodeMCU that are safer for general input, such as D1, D2, D5, D6, D7, D8 (GPIO5, GPIO4, GPIO14, GPIO12, GPIO13, GPIO15, etc., depending on your board). For example, you could change your row/column arrays to something like:
cpp// Example: Move row 0 from D0 to D4 (just as an example). byte rowPins[ROWS] = {D4, D5, D6, D7}; byte colPins[COLS] = {D8, D3, D2, D1};
Make sure you physically move that wire from D0 to D4 (or whichever pin you choose), and update your software pin list to match your actual wiring.
Once you drop D0 from your keypad wiring, you should find that button 1
is recognized as reliably as all the other keys.