Playing with Arduino – Sending and Receiving SMS
In this tutorial, we are going to send and receive SMS with Arduino using GSM shield. Before starting the project, it is assumed that you have basic knowledge on Arduino and Serial monitor to interface Arduino with PC.
Required Components
- Arduino
- GSM/GPRS Shield (here Telefonica GSM/GPRS Shield is used)
- SIM Card
Sending Message/SMS using Arduino
1. To use GSM with Arduino, we need to use GSM library. So import GSM library header file GSM.h at first.
#include <GSM.h>
2. To use SIM card we need to define its PIN code/number. PIN numbers enables the SIM functionality. If your SIM doesn’t contain any PIN number, then skip this.
#define PINNUMBER ""
3. Now use the code below and upload into your arduino.
#include <GSM.h> #define PINNUMBER "" GSM gsmAccess; GSM_SMS sms; void setup() { // initialize serial communications Serial.begin(9600); Serial.println("Send SMS Messages"); // connection state boolean notConnected = true; // Start GSM shield // If your SIM has PIN, pass it as a parameter of begin() in quotes while(notConnected) { if(gsmAccess.begin(PINNUMBER)==GSM_READY) notConnected = false; else { Serial.println("Not connected"); delay(1000); } } Serial.println("GSM initialized"); } void loop() { Serial.print("Enter receiver's mobile number: "); char remoteNumber[20]; // mobile number to send sms readSerial(remoteNumber); Serial.println(remoteNumber); // sms text Serial.print("Now, type Message: "); char txtMsg[200]; readSerial(txtMsg); Serial.println("SENDING"); Serial.println(); Serial.println("Message:"); Serial.println(txtMsg); // send the message sms.beginSMS(remoteNumber); sms.print(txtMsg); sms.endSMS(); Serial.println("\nCOMPLETE!\n"); } /* Read input serial */ int readSerial(char result[]) { int i = 0; while(1) { while (Serial.available() > 0) { char inChar = Serial.read(); if (inChar == '\n') { result[i] = '\0'; Serial.flush(); return 0; } if(inChar!='\r') { result[i] = inChar; i++; } } } }
Receiving/Reading Message
For SMS receiver too, the hardware requirement is same. You need to interface the Arduino to PC. It waits and print the received message in its serial monitor. Use the following code in your Arduino for receiver and run the serial monitor.
// gsml libraries #include <GSM.h> // sim card PIN Number #define PINNUMBER "" // initialize the library instance GSM gsmAccess; GSM_SMS sms; char remoteNumber[20]; void setup() { // initialize serial communications Serial.begin(9600); Serial.println("SMS Messages Receiver"); // connection state boolean notConnected = true; // Start GSM shield // If your SIM has PIN, pass it as a parameter of begin() in quotes while(notConnected) { if(gsmAccess.begin(PINNUMBER)==GSM_READY) notConnected = false; else { Serial.println("Not connected"); delay(1000); } } Serial.println("GSM initialized"); Serial.println("Waiting for messages"); } void loop() { char c; // If there are any SMSs available() if (sms.available()) { Serial.println("Message received from:"); // Get remote number sms.remoteNumber(remoteNumber, 20); Serial.println(remoteNumber); // This is just an example of message disposal // Messages starting with # should be discarded if(sms.peek()=='#') { Serial.println("Discarded SMS"); sms.flush(); } // Read message bytes and print them while(c=sms.read()) Serial.print(c); Serial.println("\nEND OF MESSAGE"); // delete message from modem memory sms.flush(); Serial.println("MESSAGE DELETED"); } delay(1000); }
Try to send sms from your mobile phone to the number used in SMS receiver. Your message should be printed in serial monitor.