Pages: [1]   Go Down
Author Topic: Receive SMS example  (Read 7704 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys,

     I noticed on the Receive gsm text for the shield that multiple msgs cannot be received. The coding is only detecting the first msg and stop detecting until it is reset.

Code:
/*
 SMS receiver
 
 This sketch, for the Arduino GSM shield, waits for a SMS message
 and displays it through the Serial port.
 
 Circuit:
 * GSM shield attached to and Arduino
 * SIM card that can receive SMS messages
 
 created 25 Feb 2012
 by Javier Zorzano / TD
 
 This example is in the public domain.
 
 http://arduino.cc/en/Tutorial/GSMExamplesReceiveSMS
 
*/

// include the GSM library
#include <GSM.h>

// PIN Number for the SIM
#define PINNUMBER "1111"

// initialize the library instances
GSM gsmAccess;
GSM_SMS sms;

// Array to hold the number a SMS is retreived from
char senderNumber[20]; 

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 Messages Receiver");
   
  // connection state
  boolean notConnected = true;
 
  // Start GSM connection
  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(senderNumber, 20);
    Serial.println(senderNumber);

    // An example of message disposal   
    // Any 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);

}



Thank you, in advance.
Logged

Madrid (Spain)
Offline Offline
Full Member
***
Karma: 0
Posts: 103
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

Try to receive sms using AT commands with the next code and sending them via serial monitor (configured CR / NL).

Code:
#include <SoftwareSerial.h>
#include <string.h>

char incoming_char = 0;
SoftwareSerial cell(2, 3);

void setup()
{
  Serial.begin(9600);
  cell.begin(9600);
 
  Serial.print("Starting modem communication...");
  delay(12000);
  Serial.print("OK\nIntroduce your AT commands:\n");
}

void loop()
{
  if(cell.available() > 0)
  {
    incoming_char=cell.read();
    if((incoming_char >= ' ') && (incoming_char <= 'z'))
      Serial.print(incoming_char);
    else
    {
      Serial.print("%");
      Serial.print((int)incoming_char);
      Serial.print("%");
      if(incoming_char==10)
        Serial.println();
    }
  }

  if(Serial.available() > 0)
  {
    incoming_char = Serial.read();
    cell.print(incoming_char);
  }
}

The AT commands for receiving SMS are:

ATE1 --> Set echo ON
AT+CMGF=1 --> Establishes text mode in modem
AT+CMGL="REC UNREAD",1 --> Checks if exists unread SMS. Return OK if not exist any SMS, in other case returns a list with them (the first number of each of them are the index of each of them).
AT+CMGR=[sms_index],0 --> Read SMS with index sms_index from modem memory (ej: AT+CMGR=1,0).
AT+CMGD=[sms_index] --> Delete SMS with index sms_index from modem memory (ej: AT+CMGD=1).

The problem can be attached with your local operator. Are you using the SIM that came with the shield?.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I live in Canada and the SIM card that was provided did not work here. So I am using a Fido/Rogers SIM card. I will try this code out now.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I could not get the codes to work, so I changed this (the 10 to 11) because of the + coming in front.

Code:
     Serial.print("%");
      Serial.print((int)incoming_char);
      Serial.print("%");
      if(incoming_char==11)
        Serial.println();
    }


So to clarify the issue - the gsm picks up the first text message coming in but not any others that comes in after unless it is reset

I appreciate the help guys.
« Last Edit: April 30, 2013, 08:08:10 am by akashroy » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did you manage to execute the AT dialogue? When you execute the sketch you shall see the modem answers. May you copy here the output received through the Serial monitor?

thanks.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
ATE1 --> Set echo ON
AT+CMGF=1 --> Establishes text mode in modem
AT+CMGL="REC UNREAD",1 --> Checks if exists unread SMS. Return OK if not exist any SMS, in other case returns a list with them (the first number of each of them are the index of each of them).
AT+CMGR=[sms_index],0 --> Read SMS with index sms_index from modem memory (ej: AT+CMGR=1,0).
AT+CMGD=[sms_index] --> Delete SMS with index sms_index from modem memory (ej: AT+CMGD=1).

I'm not sure if I understood what to do with these commands guys.

After I executed the given code, all I got was:
____
Starting modem communication...OK
Introduce your AT commands:
____

Nothing else occurred after I had texted the SIM card number either.

Thanks for the help guys - You guys are my lifeline right now!

« Last Edit: April 30, 2013, 07:30:15 pm by akashroy » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would like to notify that I am using the mega, so would the line:
SoftwareSerial cell(2, 3);

Would be different, or no ?

Thanks guys
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First of all, sorry for the delay answering. Long holiday in Madrid.

And yes, things are a bit different in a Mega because interrupts are not the same and communications between modem and arduino have to change.

First, go to the library file called GSM3SoftSerial.cpp and uncomment the lines:

// These can be used in Arduino Mega
#define __TXPIN__ 11
#define __RXPIN__ 10
#define __RXINT__ 4

Now you have to throw a couple of wires:
- From the shield pin called "GSM TX" to pin number 10
- From the shield pin called "GSM RX" to pin number 11

Now you should be able to execute the AT commands and see the answers. Please tell us how it is going.

If you move to UNO remember to take out the wires and comment again the lines.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys,
   
        Thanks for your help - the service was mistakenly cut off.
Akash
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I hope you guys can help me.  I can use the following code to receive SMS messages, and then use the body of the SMS message to do some conditional programming.  The only problem is that I can only do it by typing my AT commands into the serial monitor. When I try to put the receive SMS command (AT+CMGR=1,0) in my loop, it doesn't work.  Ideally I would be able to check for a new SMS every 30 seconds.  This is the working code:
Code:
#include <SoftwareSerial.h>
#include <string.h>
char command = 0; //Conditional Programming Variable changes with SMS content.
char incoming_char = 0;
SoftwareSerial cell(7, 8); //my shield uses these pins for software serial.

void setup()
{
  Serial.begin(19200);
  cell.begin(19200);
  delay(2000);
  Serial.print("Starting modem communication...");
  delay(12000);
  Serial.print("OK\nIntroduce your AT commands:\n");
}

void loop()

{
  if(cell.available() > 0)
  {
    incoming_char=cell.read();
    if((incoming_char >= ' ') && (incoming_char <= 'z'))
      Serial.print(incoming_char);
    if(incoming_char=='#'){
      Serial.println("SMS command mode:"); //If the SMS contains "#" the arduino looks for commands that follow.
      command=cell.read();
    }
    if (command == 'a'){  //If the next character in the SMS is "a", then do the following
      Serial.println("Commands would execute here and now.");
      command = 0;
    }

    else
    {
      Serial.print("%");
      Serial.print((int)incoming_char);
      Serial.print("%");
      if(incoming_char==10)
        Serial.println();

      //Serial.println(incoming_char);



    }
  }

  if(Serial.available() > 0)
  {
    incoming_char = Serial.read();
    cell.print(incoming_char);
  }
}




When I send a SMS to my cell shield that contains "#a" in the body of the message and enter the "AT+CMGR=1,0" into the serial monitor the program reads the SMS and then does whatever I tell it to do.  Right now it just executes a serial print that says "Commands would execute here and now".

After I run this I'd also like to delete the SMS with the "AT+CMGD=1" command.

Here is my code with the timed SMS check... It runs the "AT+CMGR=1,0" command every 30 seconds, but doesn't work.

Code:
#include <SoftwareSerial.h>
#include <string.h>
char command = 0; //Conditional Programming Variable changes with SMS content.
char incoming_char = 0;
SoftwareSerial cell(7, 8); //my shield uses these pins for software serial.

//These variables are for timing the Receive SMS command.
long previousMillis = 0; //Equals current millis every time interval is met.
long interval = 30000; //Amount of time to wait between Receive SMS commands

void setup()
{
  Serial.begin(19200);
  cell.begin(19200);
  delay(2000);
  Serial.print("Starting modem communication...");
  delay(12000);
  Serial.print("OK\nIntroduce your AT commands:\n");
}

void loop()

{
//--------Timed SMS Receive command code ---------
  if (currentMillis - previousMillis > interval) {

    cell.print('AT+CMGR=1,0');

    previousMillis = currentMillis;
  }
//------------------------------------------------


  if(cell.available() > 0)
  {
    incoming_char=cell.read();
    if((incoming_char >= ' ') && (incoming_char <= 'z'))
      Serial.print(incoming_char);
    if(incoming_char=='#'){
      Serial.println("SMS command mode:"); //If the SMS contains "#" the arduino looks for commands that follow.
      command=cell.read();
    }
    if (command == 'a'){  //If the next character in the SMS is "a", then do the following
      Serial.println("Commands would execute here and now.");
      command = 0;
    }

    else
    {
      Serial.print("%");
      Serial.print((int)incoming_char);
      Serial.print("%");
      if(incoming_char==10)
        Serial.println();

      //Serial.println(incoming_char);



    }
  }

  if(Serial.available() > 0)
  {
    incoming_char = Serial.read();
    cell.print(incoming_char);
  }
}





The result is this:
"1%49%1%49%3%51%1%49%2%50%"

Which makes no sense to me... I realize I haven't included the auto delete code in either example, that's because I haven't even gotten to that point yet.  The previous posts were very helpful, could you please point out what I am doing wrong? I spent all day trying to figure it out. Thanks in advance.
« Last Edit: July 22, 2013, 06:54:47 pm by captainhooyah » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I needed to include a carriage return. I should note I am using the Seeed Studio GPRS shield and not the official arduino shield.  I got it to receive messages using AT commands like this:

Code:
cell.println("AT+CMGR=1,0");
cell.print("\r");

Thanks to Davidgoth for helping me out via private message.

If anyone is having issues with the this and the seeed studio gprs shield send me a PM, I can probably help you out at this point.



Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also it was a lot easier to debug via serial monitor if I took this code chunk and changed it.

Code:
else
    {
      Serial.print("%");
      Serial.print((int)incoming_char);
      Serial.print("%");
      if(incoming_char==10)
        Serial.println();
      }

Code:
else
    {
      //Serial.print("%");
      Serial.print(incoming_char);
      //Serial.print("%");
      if(incoming_char==10)
        Serial.println();
      }
Logged

Pages: [1]   Go Up
Jump to: