This sketch waits for an SMS message and prints it to the serial monitor. It uses the GSM library of the Arduino GSM Shield and an active SIM card. To operate, the SIM card doesn't need a data plan.
First, import the MKRGSM library
#include <MKRGSM.h>
SIM cards may have a PIN number that enables their functionality. Define the PIN for your SIM. If your SIM has no PIN, you can leave it blank :
#define PINNUMBER ""
Initialize instances of the classes you're going to use. You're going to need both the GSM and GSM_SMS class.
Create a char
array to hold the number that is sending the message :
char remoteNumber[20];
In setup
, open a serial connection to the computer. After opening the connection, send a message indicating the sketch has started.
Create a local variable to track the connection status. You'll use this to keep the sketch from starting until the SIM is connected to the network :
Connect to the network by calling gsmAccess.begin()
. It takes the SIM card's PIN as an argument. By placing this inside a while()
loop, you can continually check the status of the connection. When the modem does connect, gsmAccess()
will return GSM_READY
. Use this as a flag to set the notConnected
variable to true
or false
. Once connected, the remainder of setup
will run.
Finish setup
with some information to the serial monitor.
SMS messages are received by the modem. SIM cards have some memory space to store incoming SMS. The number of SMS the card can hold can be as few as 10, or as many as 200, depending on the SIM. You should check with your provider to determine how many your SIM can keep in memory.
In loop()
, create a variable of type char
to temporarily hold characters from any SMS received. Use sms.available()
to check for the presence of any messages on the SIM :
If a SMS is available, retrieve the remote sender's number by calling sms.remoteNumber(remoteNumber, 20)
. the remoteNumber
argument is the char
array you declared in the beginning of the sketch, it can be no longer than 20 characters. Send this number to the serial monitor.
It's possible to delete SMS messages by calling sms.flush()
. Using sms.peek()
it's possible to identify the message index number, which could be helpful for removal
The code below won't remove any from the SIM, but you could iterate through a for
loop, or identify a specific index number to remove, instead of the dummy # used below
To read a message, use sms.read()
. Here, you'll store each character from the message into the variable c
and print it out as it gets read.
Indicate the message is complete and remove it from memory with sms.flush()
.
Add a brief delay and close the loop
.
Once your code is uploaded, open the serial monitor. With a phone, or other SMS enabled service, send a SMS to the number of your SIM. You should see the message print out on screen when it is received.
The complete sketch is below.
* MKRGSMExamplesMakeVoiceCall - How to make a voice call with mic and speaker.
Last revision 2017/11/29 by AG