0

I have an Arduino Mega 2560 and a sim900 gsm module. I interfaced them successfully and written the code. Its working, but I can only send 1 sms at a time in the while loop. That means when I write a while loop to execute the sendsms() 5 times by using a while loop. Only one sms is sent.. and it stops...

The code is below:

#include <SoftwareSerial.h>
#include <String.h>
SoftwareSerial mySerial(52, 53);

void setup()
{
     mySerial.begin(19200);      // the GPRS baud rate   
     Serial.begin(19200);    // the GPRS baud rate 
     delay(500);

}

int x = 0;

loop()
{

    while (x<5)
    {
     SendTextMessage();  
     x++;
     }  

 }


void SendTextMessage()
{
 mySerial.print("AT+CMGF=1\r");
 delay(100);
 mySerial.println("AT + CMGS = \"+94776511996\"");
 delay(100);
 mySerial.println("hey wow");
 delay(100);
 mySerial.println((char)26);
 delay(100);
 mySerial.println();
}
hlovdal
  • 26,565
  • 10
  • 94
  • 165
sham999
  • 121
  • 1
  • 3
  • 12

2 Answers2

2

You can't just dump your AT commands at the SIM900 with 100mS delay, and expect it to work. The SIM900 responds to AT commands (typically with "OK"), and you should wait for this response before issuing the next command. You can getaway with ignoring these responses only if you provide enough delay between AT commands to make sure that every command is only sent after the SIM900 had enough time to respond to the previous one. To make a quick verification of this, I would add a delay(10000) - a 10 seconds delay - at the end of your sendTextMessage() function. This will (probably) give the SIM900 enough time to complete the SMS transmission before moving on to the next one.

Lior Hass
  • 91
  • 1
  • 4
  • Make sure to try that 10 second delay only for testing purposes. For real code you MUST (as stated in the beginning of the answer) *read and parse* everything received from the modem. Never, never, ever use delay to wait for any AT command response. It's as useful as kicking dogs that stand in your way in order to get them to move. Yes it might actually work some times, but at some point you will be sorry for taking that approach... – hlovdal Apr 06 '14 at 18:08
  • For some further hints on how to do this properly, see the first part of [this answer](http://stackoverflow.com/a/15591673/23118) or [some of my other answers](http://stackoverflow.com/search?q=%5Bat-command%5D%20%22read%20and%20parse%22) related to this for more details. – hlovdal Apr 06 '14 at 18:10
  • Sir actually my program should send sms to 10 numbers when my program executes. At the same time the rfid reader connected to the arduino also should work. So if I need to wait 10seconds after each sms command, my system is useless. hlovdal> what do you mean by "read and parse everything received from the modem." please explain with a code example. Thanks – sham999 Apr 07 '14 at 06:14
0
void SendTextMessage(){
  mySerial.write("AT+CMGF=1\r\n");
  delay(1000); 
  mySerial.write("AT+CMGS=\"+94776511996\"\r\n");
  delay(1000);
  mySerial.write("Test");
  delay(1000);   
  mySerial.write((char)26);
  delay(2000);
  }