Send SMS
Use the Serial Monitor to type in SMS messages to different phone numbers.
This sketch send a SMS message from an Arduino board equipped with a GSM shield. Using the serial monitor of the Arduino Software (IDE), you'll enter the number to connect with, and the text message to send.
Hardware Required
- Arduino Board 
- SIM card 
Circuit
 
  
    
    
Code
First, import the GSM library
#include <GSM.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.
1GSM gsmAccess;2
3GSM_SMS sms;In
setup1void setup(){2
3  Serial.begin(9600);4
5  Serial.println("SMS Messages Sender");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 :
1boolean notConnected = true;Connect to the network by calling
gsmAccess.begin()while()gsmAccess()GSM_READYnotConnectedtruefalsesetup1while(notConnected)2
3  {4
5    if(gsmAccess.begin(PINNUMBER)==GSM_READY)6
7      notConnected = false;8
9    else10
11    {12
13      Serial.println("Not connected");14
15      delay(1000);16
17    }18
19  }Finish
setup1Serial.println("GSM initialized.");2}Create a function named
readSerialintchar1int readSerial(char result[])2{Create a variable to count through the items in the serial buffer, and start a
while1int i = 0;2
3  while(1)4
5  {As long as there is serial information available, read the data into a variable named
inChar1while (Serial.available() > 0)2
3    {4
5      char inChar = Serial.read();If the character being read is a newline, terminate the array, clear the serial buffer and exit the function.
1if (inChar == '\n')2
3      {4
5        result[i] = '\0';6
7        Serial.flush();8
9        return 0;10
11      }If the incoming character is an ASCII character other than a newline or carriage return, add it to the array and increment the index. Close up the
while1if(inChar!='\r')2
3      {4
5        result[i] = inChar;6
7        i++;8
9      }10
11    }12
13  }14}In
loopcharremoteNumberreadSerialremoteNumberreadSerialremoteNumber1Serial.print("Enter a mobile number: ");2
3  char remoteNumber[20];4
5  readSerial(remoteNumber);6
7  Serial.println(remoteNumber);Create a new
chartxtMsgtxtMsgreadSerial1Serial.print("Now, enter SMS content: ");2
3  char txtMsg[200];4
5  readSerial(txtMsg);Call
sms.beginSMS()remoteNumbersms.print()sms.endSMS()loop1Serial.println("SENDING");2
3  Serial.println();4
5  Serial.println("Message:");6
7  Serial.println(txtMsg);8
9  sms.beginSMS(remoteNumber);10
11  sms.print(txtMsg);12
13  sms.endSMS();14
15  Serial.println("\nCOMPLETE!\n");16}Once your code is uploaded, open the serial monitor. Make sure the serial monitor is set to only send a newline character on return. When prompted to enter the number you wish to call, enter the digits and press return. You'll then be asked to enter your message. Once you've typed that, press return again to send it.
Complete Sketch
The complete sketch is below.
Last revision 2018/08/23 by SM
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.