GSM Code Problems

Hi, as you can see, I made the arduino gsm sheild to send a message every time the button is pressed. But I don’t know why when the button is pressed, it just keeps spamming messages.

/Note:This code is used for Arduino 1.0 or later/
#include <SoftwareSerial.h>

const int buttonPin = 2;
int buttonState = 0;

SoftwareSerial Sim900Serial(2, 3);
void setup()
{
pinMode(buttonPin, INPUT);
Sim900Serial.begin(115200); // the GPRS baud rate
delay(500);
Sim900Serial.println(“AT+IPR=19200”);
delay(500);
Sim900Serial.begin(19200); // the GPRS baud rate
delay(1000);
Serial.begin(9600); // the Hardware serial rate
Serial.println(“Please type ‘s’ to send SMS”);
}
void loop()
{
buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) {

SendTextMessage();
}
if (Sim900Serial.available())
Serial.write(Sim900Serial.read());

else{
(buttonState == LOW);
}

}

void SendTextMessage()
{
Sim900Serial.print(“AT+CMGF=1\r”); //Sending the SMS in text mode
delay(100);
Sim900Serial.println(“AT + CMGS = “*********””);//The target phone number
delay(100);
Sim900Serial.println(“Hello I am testing at 9.25”);//the content of the message
delay(100);
Sim900Serial.println((char)26);//the ASCII code of the ctrl+z is 26
delay(100);
Sim900Serial.println();
}

I get very bored that a great deal of info on the www that implies a delay wait for a responce for AT commands is adequate to make the modem work.

GSM commands especially texts are subject to delays on SIM registration, allocation of frequency slots to communicate from tower to your GSM Modem, traffic density at Tower itself, you will receive responces from Tower faster if your SIM is the only one asking for service, but if there are 100's / 1000's of communications on the tower then when you request a send text the delay can be significant I have measured responce delays up to 10's seconds.

To handle this the Hayes command set of is a synchronous protocol.. Send ...Wait for a reply

You must send command and wait and receive the responce , decode the responce thereby ensuring its safe to send the next command.

Responce from any GSM modem when you send a comands

Sim900Serial.print("AT+CMGF=1\r"); //Sending the SMS in text mode

... reply is OK with a carriage return.. Line feed character.

.. Advise you issue ATE0 to kill local command echo, otherwise you have extra decodes, without local command echo ... you only need to look at the reply data.

Sim900Serial.println("AT + CMGS = "*********"");//The target phone number

.. reply here .. is carriage return.. Line feed and the chatacter >

Sim900Serial.println("Hello I am testing at 9.25");//the content of the message

.. this has no reply

Sim900Serial.println((char)26);//the ASCII code of the ctrl+z is 26

...This terminates the above yext send command so reply is OK carriage return Line feed.

Sugest these lines of code are replaced with

char temp [256];
strcpy (temp, "Hello I am testing at 9.25");
strcat (temp, "0x1A");
Sim900Serial.println(temp);

This ensures the whole text detail including the Cntl Z is sent as one string..

The reason things go wrong with only delay's

If you send data before geting correct responce sequence. then the modem tends to get into a lock up state. theres no quick recovery from this, there is usually at least a 15 second harware time to clear the error, if your lucky you will then a responce from the Modem

... ERROR carriage return line feed

Hope this helps, its quite easy to write a state driven system to send / then receive / then decode the responce.. and proceed to next step in the required

Happy coding..

In addition to what tgsuperspec has said, you are checking the status if the pin every time you go through the loop. What you should be doing is comparing the state of the pin to the state it was last time through. If it was LOW last time, and is still LOW, you don't need to send another SMS do you? Only when it transitions from HIGH to LOW.

If it's a mechanical switch, how are you handling the debouncing?