This project will show you how to interface a 4×4 Keypad with the NodeMCU to make a simple code-based door lock. Entering the wrong code three times in a row or changing the existing code will send an E-mail alert to the specified recipient or the owner of the building.
Contents
- Introduction
- Components Required
- NodeMCU Pin Diagram
- 4×4 Keypad Connections
- The Code
- Keypad Code
- Sending the E-mail Alert
- Uploading the code onto the NodeMCU
- Video Demonstration
Introduction
NodeMCU is a low-cost, open-source, Lua based IoT platform. It includes firmware developed for the popular ESP8266 Wi-Fi SoC (System on a Chip) by Espressif Systems and hardware based on the ESP-12 module. It also supports the ESP32 32-bit MCU. The biggest advantage of using the NodeMCU is the extremely low cost. A NodeMCU 1.0 (ESP-12E) development board costs just INR 350 or about 4.5 USD.
The NodeMCU can be programmed using the Arduino IDE, making it very easy for existing developers to integrate NodeMCU into their projects. As NodeMCU is based on Lua, we can also program the NodeMCU using the ESPlorer IDE to run Lua scripts. However, in this tutorial, we shall use the Arduino IDE to program the NodeMCU.
Components required:
- 1x NodeMCU module ( I have used a NodeMCU 1.0 (ESP-12E) module)
- 1x 4×4 Keypad
- 1x MicroUSB cable
- Jumper Wires (Male-to-Female)
NodeMCU Pin Diagram
We shall be using the GPIO pins to interface the 4×4 Keypad with the NodeMCU.
4×4 Keypad Connections
Make the connections as follows: ( use above image for pin number reference)
Keypad | NodeMCU |
Pin 1 | GPIO16 |
Pin 2 | GPIO5 |
Pin 3 | GPIO4 |
Pin 4 | GPIO0 |
Pin 5 | GPIO2 |
Pin 6 | GPIO14 |
Pin 7 | GPIO12 |
Pin 8 | GPIO13 |
Once these connections are done, our circuit is ready to go and all that is left is the code.
The Code
There are two parts to our code:
- To receive the characters pressed on the keypad
- To send out an E-Mail alert if the wrong code is entered three times in a row or if the code is changed
In the GitHub link mentioned below, download all the three source code files (.ino, .cpp, .h) into a single folder. Do not make any changes to the .cpp file.
First, open the .ino file.
Keypad code
We have to combine both the codes to create the project.
#include <ESP8266WiFi.h>
#include <Keypad.h>
#include <EEPROM.h>
Include the above libraries. To include the ESP8266 WiFi library, you first need to install the esp8266 platform. Go to File –> Preferences and paste the following text into the Additional Boards Manager field:
https://arduino.esp8266.com/stable/package_esp8266com_index.json
Then go to Tools –> Board –> Board Manager and install the esp8266 platform.
Then click this link and download the esp8266 library. Go to Sketch –> Include Library –> Add .ZIP Library and choose the downloaded zip file. The library will now appear in Sketch —> Include Library.
Download the keypad library using this link and follow the same procedure as above to include the library in your project.
const char* ssid = "YOUR WIFI_SSID"; // WIFI network name
const char* password = "YOUR WIFI_PASSWORD"; // WIFI network password
Enter your Wi-Fi network credentials in the SSID and password fields shown above. Make sure that your code is not visible to everyone as your network credentials will be shown as plain text.
char code[]= {'a','b','c','d'};
Replace ‘a’, ‘b’, ‘c’,and ‘d’ with any characters from the keypad. This will be the passcode of the door lock.
keypressed = myKeypad.getKey();
The keypressed variable constantly waits for a key to be pressed. When a key is pressed, the variable stores it.
NOTE: The NodeMCU has something known as the Watchdog Timer. It has two watchdog timers – software and hardware. In case the system remains idle for too long, if you don’t press a key for too long, the software watchdog timer is triggered and the system is reset. The Serial Monitor will show a Soft WDT reset. The system does this because a lot of important functions like maintaining the Wi-Fi connection and managing the TCP/IP stack are performed in the background. To ensure that we get enough CPU time to prevent the reset, I have used the yield() function multiple times in the code. The yield() function allows the CPU to do some background processing until the delay ends. The software WDT can be disabled but doing this for too long will trigger the hardware WDT. It is, therefore, better to use the yield() function.
EEPROM.put(i, code[i]);
Go to Sketch –> Include library and select EEPROM. The EEPROM. put(address, data) function stores the new code in the memory. Starting from address 0, we store the new code character by character.
Sending the E-Mail Alert
Now let us look at how to send E-Mail alerts. This tutorial will show how to use a Gmail account to send the alert.
#include "Gsender.h"
This will include the library used to send the E-Mail alerts.
Gsender *gsender = Gsender::Instance();
The above piece of code creates a pointer to class Instance.
void SendMail(String subject,String message)
The subject and message are used as formal parameters. Whenever we call the SendMail function, we can edit the subject and message strings based on the type of alert.
if(gsender->Subject(subject)->Send("RECIPIENT_MAIL_ID", message))
In the above code, replace “RECIPIENT_MAIL_ID” with the E-Mail ID of the owner of the building or the person who should be alerted in case of unauthorized access.
In the header (.h) file,
const char* FROM = "YOUR_GMAIL_ADDRESS";
type your Gmail address in the field given above.
const char* EMAILBASE64_LOGIN = "--------";
const char* EMAILBASE64_PASSWORD = "------";
Go to this link. Encode your E-Mail address and password in the base 64 format and paste the results in the code above. Note that these encoded values can be decoded as well. To protect your E-Mail address from misuse, do not make the code public.
These are the only changes that need to be made to the code.
Gmail has many privacy settings that prevent third party clients and apps using less secure sign-in methods from accessing the mail. To enable NodeMCU to send the E-Mail, go to https://myaccount.google.com/lesssecureapps and turn on the feature. In case you are worried about the security of your primary account, you can set up a dummy Gmail account. We shall be using the SMTP server to send the mail.
Upload the code onto the NodeMCU
Open the .ino file and upload it to the NodeMCU. Make sure that you have selected the correct board from Tools –> Board.
Once uploaded, the code should work and you can use the Serial Monitor (Ctrl + Shift + M) to see the results.
Press “*” to type the code and “A” to confirm it.
To change the code, press “#” and follow the same steps.
After three incorrect attempts, an E-Mail Alert will be sent.
Video Demonstration
Below is a short video showing the working of the circuit.
Conclusion
Please note that I have divided the code tutorial into Keypad and E-Mail sections for more clarity. However, there is only one code.
Thanks for going through this tutorial! You can add more functionality using LED’s or relays.
Happy creating!