GSM Shield

Hello,

I am working on the GSM shield V2.
I got 2 sim cards. I reveive correctly SMS (sms example of GSM library) on one sim card. But with the second one, I receive SMS only after the GSM initialized in the setup but not in the loop (as the other sim card).
So, in the loop I manually restard the GSM and at each start up I receive a SMS even if I send nothing, I always receive the last on. It looks like there are a lot of SMS in the air and the arduino try to receive them but I send only 10 SMS.
See below my code.

Thanks for your help guys!

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.

*/

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

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

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

// 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 native USB port 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");
vcs.hangCall();
}

void loop() {
char c;
sms.flush();
// 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() == '3') {
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");
}
Serial.print("shutdown...");
gsmAccess.shutdown();
Serial.println("OK");
Serial.print("start...");
gsmAccess.begin(PINNUMBER);
Serial.println("OK");
}

    // Any messages starting with # should be discarded
    if (sms.peek() == '3') {
      Serial.println("Discarded SMS");

When the code doesn't match the comments, you look pretty silly.

    // Delete message from modem memory
    sms.flush();
    Serial.println("MESSAGE DELETED");

You need to make CERTAIN that flush() does what that comment says. You are flush()ing all over the place, so I have a very hard time believing that flush() does what the comment says.

  Serial.print("shutdown...");
  gsmAccess.shutdown();
  Serial.println("OK");
  Serial.print("start...");
  gsmAccess.begin(PINNUMBER);
  Serial.println("OK");

Why are you shutting down and beginning again on EVERY pass through loop()?

Thanks for your quick reply.

  1. I don't need the discarded function
  2. I don't know how to be sure the the sms are correctly flush
  3. I reboot the GSM on each loop because I reveive sms only at the first connexion
    --> I put the sim card into a cellphone and no sms are coming but in the arduino at each gsm.access I reveive a sms (like a full buffer sending sms at each connexion of the gsm)

is it more clear?