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.
The goals of this project are:
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.
We will now get to the programming part of this tutorial. The sketch we will create will include the following features:
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
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}
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!"
.
If the code is not working, there are some common issues we can troubleshoot:
safeNumber
contains the wrong number (this should be exactly the number that you are calling from).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.