I am working on SMS communication with my phone and Arduino. My intension is simple. When somebody send an SMS, the Arduino should read a text file located on my server and forward the content to the same number.

The problem here is, when I send an SMS to my Arduino, which is waiting for my message, it does not read my message. But when I separately run this example code, I see my SMS is received. It means my SMS is already present in the modem memory. But why does this happen? How to solve this? Here is my code:

// libraries
#include <GSM.h>

// PIN Number
#define PINNUMBER ""

// APN data
#define GPRS_APN       "GPRS_APN" // replace your GPRS APN
#define GPRS_LOGIN     "login"    // replace with your GPRS login
#define GPRS_PASSWORD  "password" // replace with your GPRS password

// initialize the library instance
GSMClient client;
GPRS gprs;
GSM gsmAccess; 
GSM_SMS sms;

// URL, path & port (for example: arduino.cc)
char server[] = "yourdomain.com";
char path[] = "/current.txt";
int port = 80; // port 80 is the default for HTTP

char k[30]; 
char z = '*';
int s=0;
String place;
char remoteNum[20];  // telephone number to send sms

void setup() {
    // initialize serial communications and wait for port to open:
    while (!Serial) {
        ; // wait for serial port to connect. Needed for Leonardo only
    Serial.println("SMS connected");
    // connection state
    boolean notConnected = true;
    while(notConnected) {
        if((gsmAccess.begin(PINNUMBER)==GSM_READY) &
            notConnected = false;
        else {
            Serial.println("Not connected");
    Serial.println("GPRS connected...");
    Serial.println("Server Connecting.");
    // if you get a connection, report back via serial:

void loop() {
    if (sms.available()) {
        Serial.println("Message received from:");
        sms.remoteNumber(remoteNum, 20);
        Serial.println("MESSAGE DELETED");

void smss() {
  // I just want phone number
    if (client.available()) {
        char c = client.read();
        //       Filtering a specific message
        if (z==c) {
        if(s == 1) {
            int i=0;
            k[i] = c;
            // Serial.print(k);
        place +=k;

    if (!client.available() && !client.connected()) {
        Serial.print("Place =");
        Serial.println("MESSAGE DELETED");
        // do nothing forevermore:

// if you didn't get a connection to the server:
// Serial.print(server);

void serverr() {
    if (client.connect(server, port)) {
        client.print("GET /current.txt");
        Serial.print("GET /current.txt");
        client.println(" HTTP/1.1");
        Serial.println(" HTTP/1.1");
        client.println("Host: www.yourdomain.com");
        Serial.println("Host: www.yourdomain.com");
        client.println("User-Agent: Arduino");
        Serial.println("User-Agent: Arduino");
        client.println("Accept: text/html");
        Serial.println("Accept: text/html");
        client.println("Connection: close");
        Serial.println("Connection: close");

    } else {
        Serial.println("connection failed");
        Serial.println("\n FAILED!\n");
  • Show the content of smss() function, so that we can see how you read the SMS and then delete it.
    – jfpoilpret
    Commented May 8, 2014 at 20:37
  • I have given the function definition.
    – Manihatty
    Commented May 8, 2014 at 20:46
  • It would help if you told what debug messages get displayed on your serial monitor when running this program. Also, can you confirm that remoteNumber is defined as char remoteNumber[20];? Finally, I wonder if you can flush() an SMS without reading its content first.
    – jfpoilpret
    Commented May 8, 2014 at 20:56
  • In the serial monitor, It just waiting for my sms and stays static.But when i run this [code](arduino.cc/en/Reference/GSMSMSRead), I receive that sms. Ya the its 'char remoteNumber[20];'.
    – Manihatty
    Commented May 8, 2014 at 21:01
  • the latest code you posted is obviously wrong: the smss() function is never called. The global structure is just a mess as you did not break down behavior into different functions; instead some functions do a mix of unrelated actions.
    – jfpoilpret
    Commented May 9, 2014 at 20:53

1 Answer 1


I see one problem with the global structure of your program; everytime loop() is run, it:

  1. checks if an SMS is present, and gets the originating phone number if present, otherwise it just goes on, ie smss() function returns without modifying remoteNumber
  2. send an SMS to remoteNumber

One problem here is that your loop() will repeatedly try to send an SMS to the same remoteNumber even if no SMS was received; actually, to solve this issue, you should modify smss() to:

  • either wait (forever) for an SMS to be received and return only in this situation
  • or return a value (eg bool) that indicates whether an SMS was received or not; then an SMS should be sent if and only if an SMS was just received.

Here is an example:

void setup()
    GSM and GPRS initialization
    Connection to server

void loop()
    if (smss())
        Reading the txt file content in server


bool smss() // I just want phone number
    if (sms.available())
        Serial.println("Message received from:");
        sms.remoteNumber(remoteNum, 20);
        Serial.println("MESSAGE DELETED");
        return true;
    return false;

Note that this code does not integrate error support as it should; indeed sms.remoteNumber() may return error codes (any value >1), in which case you should consider no SMS was received at all.

  • Will the sms.flush() will delete the content in remoteNum ? Because its in smss().
    – Manihatty
    Commented May 8, 2014 at 21:36
  • I have updated and added my void loop() in the question. But still,giving same problem
    – Manihatty
    Commented May 8, 2014 at 21:53
  • You've added much more to your code than just my suggestion; now we see how you get the text content from your server. And once again the whole logic of your program does not seem correct: write a function to fully read the complete content from your server (there must be a while inside it) instead of reading byte per byte at every call of loop(). And this problem is not linked to this specific question anymore.
    – jfpoilpret
    Commented May 9, 2014 at 4:26
  • I think, It would be better, If I give the complete code. I have updated the complete code. I have found a problem, If I don't run the serverr() function, I am able to receive sms, It means the server() conflicts the sms. Is my serverr() function correct ?
    – Manihatty
    Commented May 9, 2014 at 5:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.