How to let GSM Shield send sms to phone automatically

Hi Guys,

Anyone know how to use GSM Shield automatically send message to the phone?
Cos currently the code I am using need to manually key in the number before the message can be send to the phone.

//Include VirtualWire library
#include <VirtualWire.h>
#include <GSM.h>

 #define PINNUMBER ""
// Pins definition 
const int led_pin = 8;
const int receive_pin = 4;
int pinSpeaker = 5; 
GSM gsmAccess;
GSM_SMS 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 Messages Sender");

  // connection state
  boolean notConnected = true;

  // Start GSM shield
  // If your SIM has PIN, pass it as a parameter of begin() in quotes
  while (notConnected)
  {
    if (gsmAccess.begin(PINNUMBER) == GSM_READY)
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("GSM initialized");

  
   Serial.begin(9600); // Debugging only
   // Initialise the IO and ISR
   vw_set_rx_pin(receive_pin);
   vw_setup(4000); // Transmission rate
   // Start the receiver PLL
   vw_rx_start();
   // Set LED pin and Buzzer
   pinMode(led_pin, OUTPUT);
   pinMode(pinSpeaker, OUTPUT);
   
}
 
void loop()
{
   uint8_t buf[VW_MAX_MESSAGE_LEN];
   uint8_t buflen = VW_MAX_MESSAGE_LEN;
 
   // Check if a message was received
    if (vw_get_message(buf, &buflen)) 
    {
      if(buf[0]=='1') 
      {
      Serial.println("Motion detected!");
      digitalWrite(led_pin,1);
      playTone(300, 160);
      delay(150);
      SendSMS();
      }  
     if(buf[0]=='0')
     {
     Serial.println("Motion ended!");
     digitalWrite(led_pin,0);
     playTone(0, 0);
     delay(300); 
     }
   }
}

void SendSMS() {

    Serial.print("Enter a mobile number: ");
  char remoteNum[20];  // telephone number to send sms
  readSerial(remoteNum);
  Serial.println(remoteNum);

  // sms text
  Serial.print("Now, enter SMS content: ");   
  char txtMsg[200];
  readSerial(txtMsg);
  Serial.println("SENDING");
  Serial.println();
  Serial.println("Message:");
  Serial.println(txtMsg);

  // send the message
  sms.beginSMS(remoteNum);
  sms.print(txtMsg);
  sms.endSMS();
  Serial.println("\nCOMPLETE!\n");

}

// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) 
{
    duration *= 1000;
    int period = (1.0 / freq) * 1000000;
    long elapsed_time = 0;
    while (elapsed_time < duration) 
    {
    digitalWrite(pinSpeaker,HIGH);
    delayMicroseconds(period / 2);
    digitalWrite(pinSpeaker, LOW);
    delayMicroseconds(period / 2);
    elapsed_time += (period);
   }
}

/*
  Read input serial
 */
int readSerial(char result[])
{
  int i = 0;
  while (1)
  {
    while (Serial.available() > 0)
    {
      char inChar = Serial.read();
      if (inChar == '\n')
      {
        result[i] = '\0';
        Serial.flush();
        return 0;
      }
      if (inChar != '\r')
      {
        result[i] = inChar;
        i++;
      }
    }
  }
}

Here is my code.

Thank you for your help in advance.

hi,

if the sketch works well as it is then leave away all the stuff with manual input of phone number and SMS text.
Just take the lines:

// send the message
  sms.beginSMS(remoteNum);
  sms.print(txtMsg);
  sms.endSMS();
  Serial.println("\nCOMPLETE!\n");

Replace remoteNum by the phone number in "", e.g. "+498821xxxxxxxx". It is important to begin the phone number with the country code, in my example "+49".
Replace txtMsg with the text you want to send, e.g. "hello world".

Thats all. Gool luck

SupArdu