This tutorial refers to a product that has reached its end-of-life status.

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

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

Remote lock circuit diagram.
Remote lock circuit diagram.

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:

  • GSM gsmAccess
    - base class for all GSM functions.
  • GSM_SMS sms
    - base class for all GSM functions for SMS.
  • GSMVoiceCall vcs
    - base class for GSM voice call functions.
  • gsmAccess.begin(pin)
    - connects to the GSM network with the pin number as a parameter, e.g. 0123.
  • vcs.getvoiceCallStatus()
    - checks if there's a call, and returns:
    IDLE_CALL
    (no call) or
    RECEIVINGCALL
    (a call is incoming).
  • vcs.retrieveCallingNumber(caller, 20)
    - retrieves the number of the caller and stores it in
    caller
    variable.
  • vcs.hangCall()
    - used to hang up on calls.
  • sms.beginSMS(number);
    - creates an SMS for a specific number.
  • sms.endSMS()
    - sends the SMS.

The sketch can be found in the snippet below. Upload the sketch to the board.

Note: The

PINNUMBER[]
and
safeNumber
variables needs to be replaced with your credentials (pin number and phone number).

1// Include the GSM library
2#include <MKRGSM.h>
3
4#include "arduino_secrets.h"
5// Please enter your sensitive data in the Secret tab or arduino_secrets.h
6// PIN Number
7const char PINNUMBER[] = "YOUR_PIN";
8
9int greenLED = 2;
10int redLED = 3;
11
12String comparison;
13String safeNumber = "YOUR_PHONE_NUMBER";
14
15// initialize the library instance
16GSM gsmAccess;
17GSMVoiceCall vcs;
18GSM_SMS sms;
19
20// Array to hold the number for the incoming call
21char 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 only
32 }
33
34 Serial.println("GSM Lock / Unlock");
35
36 // connection state
37 bool connected = false;
38
39 // Start GSM shield
40 // If your SIM has PIN, pass it as a parameter of begin() in quotes
41 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 events
51 vcs.hangCall();
52
53 Serial.println("Waiting for a call");
54}
55
56void loop() {
57 // Check the status of the voice call
58 switch (vcs.getvoiceCallStatus()) {
59 case IDLE_CALL: // Nothing is happening
60
61 break;
62
63 case RECEIVINGCALL: // Yes! Someone is calling us
64
65 Serial.println("RECEIVING CALL");
66
67 // Retrieve the calling number
68 vcs.retrieveCallingNumber(caller, 20);
69
70 //clear comparison string
71 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 call
95 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 number
109 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.

Offline editors serial monitor, waiting for a call.
Offline editors serial monitor, waiting for a call.

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.

Output if the number is correct or unauthorized.
Output if the number is correct or unauthorized.

Additionally, the board also sends an SMS to the caller, which either contains

"Unlocked!"
or
"Unauthorized number!"
.

Feedback sent back from the board.
Feedback sent back from the board.

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
    safeNumber
    contains the wrong number (this should be exactly the number that you are calling from).

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.