Send and Receive SMS with Sim900 & Arduino

 In this Arduino with Sim900 GSM module tutorial, we will show how to send and receive SMS to and from Sim900 GSM shield . This can be helpful for developing GSM network based Internet of Things(IoT) applications. With SMS you can just do anything like controlling DC motors, receive notification if there is intruder at home, create fire alert system, receive humidity and temperature data from remote location etc.

Recommended Prerequisite 

 Sim900 GSM Module Arduino Tutorial

In the last tutorial in the above link, we showed you how to use communicate with Sim900A module with AT commands using terminal. There we directly typed the AT commands into the terminal and viewed the response from the module. We also explained in that tutorial what Sim900A GSM shield is, how to connect Arduino with Sim900A GSM shield, what is GSM and GPRS.

Interfacing Arduino with Sim900A GSM module

 The following picture shows how to wire the Arduino and Sim900A GSM module.

Interfacing Arduino with Sim900A GSM module 

We need only 3 connection. Arduino pin 8 is configured as Rx serial receive pin and is connected to the TXD pin of the module. Arduino pin 7 is configured as Tx serial transmit pin and is connected to the RXD pin of the module. The GND pin of Arduino and GSM module are connected together. In this tutorial we will be using SoftwareSerial library to configure the Rx and Tx pin to pin 8 and pin 7 respectively.

Program Code

 Here we will write program code to allow user to send and receive SMS by pressing either 's' or 'r' for send and receive respectively. The SMS message to be sent is written within the program and when the user enter s in the terminal the stored SMS is sent. The user can select r to receive to view any new SMS message.

The Arduino program code is below.

//Program to send and receive SMS using SIM900A, Arduino and a terminal
//https://ee-diary.blogspot.com

#include <SoftwareSerial.h>

//configure software serial on Arduino pin 8 and 7 to communicate with SIM900
SoftwareSerial sim900(8,7); //Rx, Tx

void setup()
{
//configure serial communication with terminal
Serial.begin(9600);
//configure serial communication with SIM900A GSM module
sim900.begin(9600);
Serial.println("Initializing...ok");
delay(1000);
Serial.println("Press 's' to send and 'r' to receive SMS");
}

void loop()
{
if (Serial.available()>0)
switch(Serial.read()){
case 's':
sendSMS();
break;
case 'r':
recieveSMS();
break;
}
}

void serialPrint()
{
delay(500);
while (Serial.available())
{
sim900.write(Serial.read());//send typed text on terminal to SIM900A
}
while(sim900.available())
{
Serial.write(sim900.read());//send received text on SIM900A to terminal
}
}

void sendSMS(){
sim900.println("AT+CMGF=1"); //Sets GSM Module in SMS Mode
serialPrint();
sim900.println("AT+CMGS=\"+PhoneNumber\""); // set phone number to send SMS
serialPrint();
sim900.println("From Sim900A: ee-diary");// SMS content
sim900.write(26);// CTRL+Z ascii code to exit SMS mode
serialPrint();
}

void recieveSMS(){
sim900.println("AT+CMGF=1"); //set GSM module in SMS mode
serialPrint();
sim900.println("AT+CNMI=2,2,0,0,0"); //select SMS read mode to show received SMS
serialPrint();
}

In the above program code, we have used SoftwareSerial.h library to configure the pin 8 and pin 7 of Arduino as serial Rx and Tx pin. This is done because the USART Tx and Rx pin of Arduino is connected to USB which is used to upload the program and display message on the serial monitor and therefore will interfere with the AT command sent/received with GSM module. To use the SerialSoftware library we have created an instance sim900() of the library.

In the setup() function we have configured the serial baud rate of 9600 for communication with the serial monitor and for the serial communication with the module. Then we have printed some message so that the user knows where the program progress and what to do. Message is displayed to user to enter s or r to send or receive SMS message.

In the loop() function, we wait for user to enter either s or r to send and receive message. If the user wants to send SMS then the function sendSMS() gets executed. 

In the sendSMS() function, we first set the sim900 module into SMS mode by sending the AT command AT+CMGF=1. Then we set the phone number to send the message using the AT command AT+CMGS=\"+PhoneNumber\". The phone number has to be in international format. The standard number of digits for GSM number is 10 digits but we have also to include the country code which is 2 or 3. So the total is 13 or 12 digits. You have to place the number where you want the sim900 module to send the SMS message. The next step is to write the actual SMS message which here is "From Sim900A: ee-diary". Then we send ASCII code 26 for CTRL+Z to exit SMS mode. The SMS message is then sent to the provided phone number.

If the user inputs r to receive message, the function receiveSMS() function is invoked. In this function, we enter the SMS mode by executing the AT command AT+CMGF=1. Then to read the recent SMS message in the SMS inbox we execute the AT command AT+CNMI=2,2,0,0,0

In between the command sent to Sim900A in both the sendSMS() and receiveSMS() functions we have used the serialPrint() function. This serialPrint() function simply echos the character/text typed in the serial monitor(terminal) to the user and also sends it to the Sim900 module. Similarly, it reads the Sim900 message and displays on the serial monitor or terminal.

 

Post a Comment

Previous Post Next Post