0

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:
    Serial.begin(9600);
    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) &
           (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))
            notConnected = false;
        else {
            Serial.println("Not connected");
            delay(1000);
        }
    }
    Serial.println("GPRS connected...");
    Serial.println("Server Connecting.");
    // if you get a connection, report back via serial:
    serverr();
}

void loop() {
    if (sms.available()) {
        Serial.println("Message received from:");
        sms.remoteNumber(remoteNum, 20);
        Serial.println(remoteNum);
        sms.flush();
        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) {
            s=1;
        }
        if(s == 1) {
            int i=0;
            k[i] = c;
            i=i+1;
            // Serial.print(k);
        }
        place +=k;
        //Serial.print(place);
    }
    //***************************************************  

    if (!client.available() && !client.connected()) {
        sms.beginSMS(remoteNum);
        Serial.print("Place =");
        Serial.print(place);
        sms.print(place);
        sms.endSMS(); 
        sms.flush();
        Serial.println("MESSAGE DELETED");
        Serial.println();
        Serial.println("disconnecting.");
        client.stop();
        // do nothing forevermore:
        for(;;)
            ;
    }
}

// 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");
        client.println();
        Serial.println();

        Serial.println("\nCOMPLETE!\n");
        //client.stop();
    } 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 May 8 '14 at 20:37
  • I have given the function definition. – Manihatty May 8 '14 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 May 8 '14 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 May 8 '14 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 May 9 '14 at 20:53
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

        sms.beginSMS(remoteNum);
        sms.print(txtMsg);
        sms.endSMS(); 
    }
}

bool smss() // I just want phone number
{
    if (sms.available())
    {
        Serial.println("Message received from:");
        sms.remoteNumber(remoteNum, 20);
        Serial.println(remoteNum);
        sms.flush();
        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.

| improve this answer | |
  • Will the sms.flush() will delete the content in remoteNum ? Because its in smss(). – Manihatty May 8 '14 at 21:36
  • I have updated and added my void loop() in the question. But still,giving same problem – Manihatty May 8 '14 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 May 9 '14 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 May 9 '14 at 5:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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