Arduino GSM connection[SOLVED]

Hi.I am trying to send sms using Arduino GSM Shield with Arduino Uno r3.The code uploaded is:
// Include the GSM library
#include <GSM.h>

#define PINNUMBER "1234"

// initialize the library instance
GSM gsmAccess(true);
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");
}

void loop()
{

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

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

  • Serial.flush();*
  • return 0;*
  • }*
  • if(inChar!='\r')*
  • {*
    _ result = inChar;_
    * i++;*
    * }*
    * }*
    * }*
    }
    ..and the serial monitor output is:
    SMS Messages Sender
    AT%13%
    0 9>AT%13%%13%%10%OK%13%%10%
    AT+CPIN=1234%13%
    9 44>AT+CPIN=1234%13%%13%%10%+CPIN: READY%13%%10%%13%%10%OK%13%%10%
    AT+CGREG?%13%
    44 75>AT+CGREG?%13%%13%%10%+CGREG: 0,2%13%%10%%13%%10%OK%13%%10%
    AT+CGREG?%13%
    89 120>AT+CGREG?%13%%13%%10%+CGREG: 0,2%13%%10%%13%%10%OK%13%%10%
    AT+CGREG?%13%
    120 23>AT+CGREG?%13%%13%%10%+CGREG: 0,2%13%%10%%13%%10%OK%13%%10%
    AT+CGREG?%13%
    but suddenly stop write output code and switch off the NET and the STATUS led on Arduino.
    I have read that the meaning of CGREG: 0,2% is waiting for connecting .¿Could be about the code?
    Thank you very much.

You failed to post your code correctly. Read the stickies at the top of the forum, and you'll know what you need to do, if you are to reasonably expect anyone to help you.

Why are you using Serial.flush()? In that code it is absolutely useless.

Hi.True,sorry.The ctrl+c and ctrl+v sometimes comes by inertia.
Respecting to Arduino GSM connection,with another library example(GSMScannetworks) occurs the same but in this occasion the AT code not print on Serial monitor,only 'GSM networks scanner'.The changes i do:

  • The PINNUMBER
  • GSM gsmAccess(true);

Thank you very much.

Hi.Solved.I made two changes:
1 .- Desactivate PIN of sim card with phone.
2.- Provide a little more power(9v battery).

  • SMS sends without problem.
  • GSM scan networks perhaps needs more power supply.

Thank you very much to every one.