4×4 Keypad Door Lock using NodeMCU with E-Mail Alerts

by Apr 29, 2020nodemcu projects

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

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.

Difference between these two NodeMCU boards? - Arduino Stack Exchange
NodeMCU 1.0 (ESP-12E) Module

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

ESP8266 NodeMcu Pinout - ESP8266 Shop
Pin Diagram of NodeMCU 1.0 (ESP-12E) Module

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)

KeypadNodeMCU
Pin 1GPIO16
Pin 2GPIO5
Pin 3GPIO4
Pin 4GPIO0
Pin 5GPIO2
Pin 6GPIO14
Pin 7GPIO12
Pin 8GPIO13

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.

Download

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.

Connected to Wi-Fi
NodeMCU is connected to the Wi-Fi network
3 wrong attempts in a row

After three incorrect attempts, an E-Mail Alert will be sent.

Email alert
The E-Mail alert sent when wrong passcode was entered 3 times in a row
Working of the lock
Code successfully changed
E-Mail alert received when the code is changed

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!

Creating a multiplication Skill in Alexa using python

Written By Monisha Macharla

Hi, I'm Monisha. I am a tech blogger and a hobbyist. I am eager to learn and explore tech related stuff! also, I wanted to deliver you the same as much as the simpler way with more informative content. I generally appreciate learning by doing, rather than only learning. Thank you for reading my blog! Happy learning!

RELATED POSTS

Smart Traffic Light System using NodeMCU(ESP32)

Smart Traffic Light System using NodeMCU(ESP32)

In this tutorial, we are going to make an IoT based Smart Traffic Light system. We will connect the lights by using ESP32. Let's begin! Contents: IntroductionMaterials RequiredThe CircuitCode and it's ExplanationThe Final Run Introduction First of all, we will create...

VIDEOS – FOLLOW US ON YOUTUBE

EXPLORE OUR IOT PROJECTS

IoT Smart Gardening System – ESP8266, MQTT, Adafruit IO

Gardening is always a very calming pastime. However, our gardens' plants may not always receive the care they require due to our active lifestyles. What if we could remotely keep an eye on their health and provide them with the attention they require? In this article,...

How to Simulate IoT projects using Cisco Packet Tracer

In this tutorial, let's learn how to simulate the IoT project using the Cisco packet tracer. As an example, we shall build a simple Home Automation project to control and monitor devices. Introduction Firstly, let's quickly look at the overview of the software. Packet...

All you need to know about integrating NodeMCU with Ubidots over MQTT

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

All you need to know about integrating NodeMCU with Ubidots over Https

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

How to design a Wireless Blind Stick using nRF24L01 Module?

Introduction Let's learn to design a low-cost wireless blind stick using the nRF24L01 transceiver module. So the complete project is divided into the transmitter part and receiver part. Thus, the Transmitter part consists of an Arduino Nano microcontroller, ultrasonic...

Sending Temperature data to ThingSpeak Cloud and Visualize

In this article, we are going to learn “How to send temperature data to ThingSpeak Cloud?”. We can then visualize the temperature data uploaded to ThingSpeak Cloud anywhere in the world. But "What is ThingSpeak?” ThingSpeak is an open-source IoT platform that allows...

Amaze your friend with latest tricks of Raspberry Pi and Firebase

Introduction to our Raspberry Pi and Firebase trick Let me introduce you to the latest trick of Raspberry Pi and Firebase we'll be using to fool them. It begins with a small circuit to connect a temperature sensor and an Infrared sensor with Raspberry Pi. The circuit...

How to implement Machine Learning on IoT based Data?

Introduction The industrial scope for the convergence of the Internet of Things(IoT) and Machine learning(ML) is wide and informative. IoT renders an enormous amount of data from various sensors. On the other hand, ML opens up insight hidden in the acquired data....

Smart Display Board based on IoT and Google Firebase

Introduction In this tutorial, we are going to build a Smart Display Board based on IoT and Google Firebase by using NodeMCU8266 (or you can even use NodeMCU32) and LCD. Generally, in shops, hotels, offices, railway stations, notice/ display boards are used. They are...

Smart Gardening System – GO GREEN Project

Automation of farm activities can transform agricultural domain from being manual into a dynamic field to yield higher production with less human intervention. The project Green is developed to manage farms using modern information and communication technologies....