Remote Lock/unlock with MKR GSM 1400
Learn how to create a remote lock with the MKR GSM 1400, which can only be unlocked with a unique phone number.
Introduction
One of the many cool things you can use the MKR GSM 1400 board for is to place and receive voice calls. In this tutorial we will see how we can create a smart lock.
Basically, we will configure the MKR GSM 1400 to only accept incoming calls from a specific number, and if we call from that number, we will be able to "unlock" it, which in this case is represented by a red and green LED.
Goals
The goals of this project are:
- Configure our device to listen for incoming calls.
- Configure our device so that only specific numbers can unlock.
- Create a conditional that either switches a red or green LED on or off.
- Send a feedback SMS back to the caller.
Hardware & Software Needed
- Arduino IDE (online or offline).
- MKRGSM library installed.
- Arduino MKR GSM 1400.
- Antenna.
- SIM card from an operator in your country.
Smart Locks
Smart locks are becoming increasingly more popular to use for homes, offices, safes and other places that we wish to keep safe. There is a large number of different components, protocols and security measurements that can be used for this, including RFID tags, secure MQTT and SSL.
As society is rapidly moving towards the digitalization of things, physical objects are starting to disappear. This of course, brings problems in itself, such as cyber-security. But from a positive point of view, it also allows us to manage most of our daily tasks, directly from our phone. Whether it is for purchasing, public transport or identification, we basically use our smartphones for everything these days.
In this project, we will use a very basic method to lock and unlock something: voice calls. The "secure element" that we will use in this will be our very own phone number, which we will use as a comparison variable. This way, if any call in the world comes in, we will not let them in, unless it is identical to the number we compare it to.
Circuit
Programming the Board
We will now get to the programming part of this tutorial. The sketch we will create will include the following features:
- Set up the board to listen for incoming voice calls (using switch statements).
- Create a comparison variable containing a specific number (used to check if incoming number is "safe").
- Create a conditional that either marks incoming call as authorized or unauthorized, and turns on green LED if the call is authorized.
- Send a feedback SMS back to the caller on whether something has been unlocked or not.
1. First, let's make sure we have the drivers installed. If we are using the Web Editor, we do not need to install anything. If we are using an offline editor, we need to install it manually. This can be done by navigating to Tools > Board > Board Manager.... Here we need to look for the Arduino SAMD boards (32-bits Arm® Cortex®-M0+) and install it.
2. Now, we need to install the libraries needed. If we are using the Web Editor, there is no need to install anything. If we are using an offline editor, simply go to Tools > Manage libraries.., and search for MKRGSM and install it.
3. We can now take a look at some of the core functions of this sketch:
- base class for all GSM functions.GSM gsmAccess
- base class for all GSM functions for SMS.GSM_SMS sms
- base class for GSM voice call functions.GSMVoiceCall vcs
- connects to the GSM network with the pin number as a parameter, e.g. 0123.gsmAccess.begin(pin)
- checks if there's a call, and returns:vcs.getvoiceCallStatus()
(no call) orIDLE_CALL
(a call is incoming).RECEIVINGCALL
- retrieves the number of the caller and stores it invcs.retrieveCallingNumber(caller, 20)
variable.caller
- used to hang up on calls.vcs.hangCall()
- creates an SMS for a specific number.sms.beginSMS(number);
- sends the SMS.sms.endSMS()
The sketch can be found in the snippet below. Upload the sketch to the board.
Note: The
andPINNUMBER[]
variables needs to be replaced with your credentials (pin number and phone number).safeNumber
1// Include the GSM library2#include <MKRGSM.h>3
4#include "arduino_secrets.h"5// Please enter your sensitive data in the Secret tab or arduino_secrets.h6// PIN Number7const char PINNUMBER[] = "YOUR_PIN";8
9int greenLED = 2;10int redLED = 3;11
12String comparison;13String safeNumber = "YOUR_PHONE_NUMBER";14
15// initialize the library instance16GSM gsmAccess;17GSMVoiceCall vcs;18GSM_SMS sms;19
20// Array to hold the number for the incoming call21char caller[20];22
23void setup() {24 // initialize serial communications and wait for port to open:25 Serial.begin(9600);26
27 pinMode(redLED, OUTPUT);28 pinMode(greenLED, OUTPUT);29
30 while (!Serial) {31 ; // wait for serial port to connect. Needed for native USB port only32 }33
34 Serial.println("GSM Lock / Unlock");35
36 // connection state37 bool connected = false;38
39 // Start GSM shield40 // If your SIM has PIN, pass it as a parameter of begin() in quotes41 while (!connected) {42 if (gsmAccess.begin(PINNUMBER) == GSM_READY) {43 connected = true;44 } else {45 Serial.println("Not connected");46 delay(1000);47 }48 }49
50 // This makes sure the modem correctly reports incoming events51 vcs.hangCall();52
53 Serial.println("Waiting for a call");54}55
56void loop() {57 // Check the status of the voice call58 switch (vcs.getvoiceCallStatus()) {59 case IDLE_CALL: // Nothing is happening60
61 break;62
63 case RECEIVINGCALL: // Yes! Someone is calling us64
65 Serial.println("RECEIVING CALL");66
67 // Retrieve the calling number68 vcs.retrieveCallingNumber(caller, 20);69
70 //clear comparison string71 comparison = "";72 comparison = String(comparison + caller);73
74 if (comparison.equals(safeNumber)) {75 Serial.println("Phone number matched!");76 Serial.println("Opening lock...");77
78 vcs.hangCall();79
80 //Turn on green LED (UNLOCKED)81 digitalWrite(greenLED, HIGH);82
83 //Send a feedback SMS back to caller.84 sms.beginSMS(caller);85 sms.print("Unlocked!");86 sms.endSMS();87
88 Serial.println("Unlocked!");89 Serial.println();90 }91 else {92 Serial.println("Unauthorized number!");93
94 //hang up call95 vcs.hangCall();96
97 //Turn on red LED (LOCKED)98 digitalWrite(redLED, HIGH);99
100 //Send a feedback SMS back to caller.101 sms.beginSMS(caller);102 sms.print("Unauthorized number!");103 sms.endSMS();104
105 break;106 }107
108 // Print the calling number109 Serial.print("Number:");110 Serial.println(caller);111
112 break;113
114}115
116}
Testing It Out
After the code has been successfully uploaded to the MKR GSM 1400 board, open the Serial Monitor. If everything goes right, we should see the text
"GSM Lock / Unlock"
followed by "Waiting for a call"
printed in the Serial Monitor. We can now test it out by calling the number of the SIM card from our phone. The code is set up to immediately hang up at incoming calls, but it stores the caller's number in the
comparison
variable. If the number we entered in the
safeNumber
matches the caller
, it will turn on the green LED, and print Unlocked!
in the Serial Monitor. If it is an "unauthorized" caller, it will print "Unauthorized number!"
in the Serial Monitor, instead. Additionally, the board also sends an SMS to the caller, which either contains
"Unlocked!"
or "Unauthorized number!"
.Troubleshoot
If the code is not working, there are some common issues we can troubleshoot:
- We have not installed the MKRGSM library.
- We have entered the wrong pin number.
- We are out of coverage (no signal).
- We have tried to call the wrong number.
- SIM card may not be activated.
- The
contains the wrong number (this should be exactly the number that you are calling from).safeNumber
Conclusion
In this tutorial, we set up a remote lock that can only be unlocked when a call from a specific number occurs. The "lock" is unlocked if the
caller
number matches the safeNumber
variable, and sends a reply to the caller to whether it was successful or not. You can now use this security measurement and system feedback in other clever, useful ways. For example, you can take a look at solenoid valves, a type of linear motor that can be configured to serve as a locking mechanism.
Feel free to explore the MKRGSM library further, and try out some of the many cool functions in this library.
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.