Sending an SMS through GSM without serial monitor

Hello, I just got my GSM shield and I ran the example for sending an SMS which the code for can be found here: http://arduino.cc/en/Reference/GSMSMSPrint

Now what I'm trying to do is to just send an predefined message such as "Welcome" but the problem is I am not receiving any messages.

Here is the code:

#include <GSM.h>

GSM gsmAccess;
GSM_SMS sms;

char num[]={ '0','1','2','3','4','5','6','7','8','9'};
char message[] ={'W','e','l','c','o','m','e'};

void setup() {
  gsmAccess.begin("0967");

  sms.beginSMS(num);
  sms.print(message);
  sms.endSMS();
}

void loop() {

}

Note: I wrote a random number here not my number just for the sake of explanation, but when I ll run the code I ll put my real number.

Any help is appreciated, thank you!

you do not need the serial interface at all - thats only for debugging purposes. your issue is that you need to wait for the device to be ready; you cannot send an SMS before the network has been established. you can check out the sample to see how this works http://arduino.cc/en/Guide/ArduinoGSMShield#toc9

  // 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);
    }
  }

it may not be connected immediately, the above code keeps trying until the connection is established - hence the delay(1000) and try again - you can strip the Serial.println commands and the sketch will wait until the device is ready before moving to the next step.

char message[] ={'W','e','l','c','o','m','e'};

That is NOT a string. There is no NULL terminator.

  sms.print(message);

The print() method expects a string. When you pass it something that is not a string, you can not be sure what it will do. So, don't do that.

It is far simpler to declare an array that contains a string, anyway.

char message[] = "Welcome"; // This IS a string, since the compiler knows to add a NULL at the end

Why are you trying to define each element individually?

Thank you ardiri for pointing out that point, I added that part of the code to wait for the connection to establish.
PaulS thank you for your clarification, I am still new to programming, I did change the way I define my input into a string as you showed. However, why is it defined in: http://arduino.cc/en/Reference/GSMSMSPrint, that it accepts a char array, or is that the same thing as a string?

I updated my code but I still couldn't receive an sms, here is the code:

#include <GSM.h>

GSM gsmAccess;
GSM_SMS sms;


void setup() {
  Serial.begin(9600);
  
  bool notConnected=true;
  
  while(notConnected)
  {
    if(gsmAccess.begin("0967")==GSM_READY)
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }
  Serial.println("Connected!");
  
  sms.beginSMS("0123456789");
  sms.print("Hello");
  sms.endSMS();
}

void loop() {

}

You might want to try prefixing your number with the international dialing code for your country rather than 0.

Let's assume the poster edited out their own phone number because
this is a public forum shall we?

Yea, I just edited out my phone number to post the code here. I think my main problem is not able to pass the data into "sms.beingSMS()" and "sms.print()" . On the send sms example provided with the GSM library the code for receiving this data is given by this function:

/*
  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++;
      }
    }
  }
}

while the "new line" is set in the serial monitor.

How do I pass on the data the same way without manually inputting the data through the serial monitor.

How do I pass on the data the same way without manually inputting the data through the serial monitor.

strcpy(result, "Send this, sucker!");
#include <GSM.h>

GSM gsmAccess;
GSM_SMS sms;


void setup() 
{
  char remoteNumber[20] = "+447751123456";
  char message[100] = "Hello!";
  
  Serial.begin(9600);
  
  bool notConnected=true;
  
  while(notConnected)
  {
    if(gsmAccess.begin("")==GSM_READY)
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }
  Serial.println("Connected!");
  
  sms.beginSMS(remoteNumber);
  sms.print(message);
  sms.endSMS();
}

void loop() 
{
}

Thank you very much everyone for your help, I really do appreciate it. However, I'm still not able to receive an SMS on my phone. I reviewed my code and tried to emulate the serial input from the example but still I'm not getting any luck.

The code I posted above works. Uno R3, official Arduino GSM/GPRS shield.

I reviewed my code and tried to emulate the serial input from the example but still I'm not getting any luck.

I changed my code. It still doesn't do what I want. I'm not going to show you the code, or explain what I want, but I want your help.

Have I paraphrased your post correctly?

PaulS I apologize for not showing you the code in the last post because I did not update anything to it beyond dannable's suggestion.

It finally worked after I used delays before sms.beginSMS(), sms.print() and sms.endSMS() (even though I'm not sure why it needed delays):

#include <GSM.h>

GSM gsmAccess;
GSM_SMS sms;


void setup() 
{
  char remoteNumber[20] = "+447751123456";
  char message[100] = "Hello!";
  
  Serial.begin(9600);
  
  bool notConnected=true;
  
  while(notConnected)
  {
    if(gsmAccess.begin("")==GSM_READY)
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }
  Serial.println("Connected!");
  
  delay(1000);
  sms.beginSMS(remoteNumber);
  delay(1000);
  sms.print(message);
  delay(1000);
  sms.endSMS();
}

void loop() 
{
}

Thank you all very much for your help!

even though I'm not sure why it needed delays

So, now the next step is to determine how short those delays can be. Waiting for the hardware to be ready (before the call to beginSMS() makes sense. Once the hardware is ready, the other delays should not be needed.

You're right! I found that the shortest delay is 45 ms.

  delay(45);
  sms.beginSMS(remoteNumber);
  sms.print(message);
  sms.endSMS();