Sending AT commands to a Serial and displaying the reply

Hello

While I am not a programming newbie I am however very rusty when it comes to Serials. I want to send an AT command to a GSM/ GPRS shield connected to my Arduino UNO. The AT command I want to pass in particular is the command to get a networks signal strength.

I am using the SIM900 library to send the command as the GSM library does not compile correctly for me. Meaning I have to use the SoftwareSerial library.

I have this example code working that relies on reading inputs from the serial monitor to carry out commands but I need it to be automated and the command to be passed in hardcoded. In this example code, the place of interest is the simplehwread() method.

#include "SIM900.h"
#include <SoftwareSerial.h>
//#include "inetGSM.h"
//#include "sms.h"
//#include "call.h"

//To change pins for Software Serial, use the two lines in GSM.cpp.

//GSM Shield for Arduino
//www.open-electronics.org
//this code is based on the example of Arduino Labs.

//Simple sketch to communicate with SIM900 through AT commands.

//InetGSM inet;
//CallGSM call;
//SMSGSM sms;

int numdata;
char inSerial[40];
int i=0;


void setup()
{
     //Serial connection.
     Serial.begin(9600);
     Serial.println("GSM Shield testing.");
     //Start configuration of shield with baudrate.
     //For http uses is raccomanded to use 4800 or slower.
     if (gsm.begin(9600))
          Serial.println("\nstatus=READY");
     else Serial.println("\nstatus=IDLE");
};

void loop()
{
     //Read for new byte on serial hardware,
     //and write them on NewSoftSerial.
     serialhwread();
     //Read for new byte on NewSoftSerial.
     serialswread();
};

void serialhwread()
{
     i=0;
     if (Serial.available() > 0) {
          while (Serial.available() > 0) {
               inSerial[i]=(Serial.read());
               delay(10);
               i++;
          }

          inSerial[i]='\0';
          if(!strcmp(inSerial,"/END")) {
               Serial.println("_");
               inSerial[0]=0x1a;
               inSerial[1]='\0';
               gsm.SimpleWriteln(inSerial);
          }
          //Send a saved AT command using serial port.
          if(!strcmp(inSerial,"TEST")) {
               Serial.println("SIGNAL QUALITY");
               gsm.SimpleWriteln("AT+CSQ");
          } else {
               Serial.println(inSerial);
               gsm.SimpleWriteln(inSerial);
          }
          inSerial[0]='\0';
     }
}

void serialswread()
{
     gsm.SimpleRead();
}

No matter how I modify this code, the command does not get passed in and response displayed while the method here does it but not the way I want it to be done. i.e Direct input. Could anyone assist here?

That does not seem to be a complete program. Please post the complete program.

Where does gsm.SimpleWriteln() come from?

Using delay(10) in the hope that more serial data will arrive is very crude.

I don't understand the following

the command does not get passed in and response displayed while the method here does it but not the way I want it to be done

Can you give specific examples of wht should happen and what actually happens

...R

Thanks for the reply.

I'm afraid this is the complete sketch. It's from this github and coded for the specific brand of GSM/GPRS shield I'm using.

The gsm.SimpleWriteln() method comes from SIM900.h and seems to be a method of writing a command to the Arduino.

Basically, the program is set so when it is running on the Arduino it is waiting for an input on the Serial Monitor. If you were to pass in the word "TEST" as in the code, it will automatically return the signal strength.

What I want to do is to remove the need for a Serial Monitor from the equation and simply pass the AT command without having to input it. Say, pass it in automatically every 15 seconds and display the response in the Serial Monitor. Does that make more sense?

Valten1992:
What I want to do is to remove the need for a Serial Monitor from the equation and simply pass the AT command without having to input it. Say, pass it in automatically every 15 seconds and display the response in the Serial Monitor. Does that make more sense?

Don't you just need to use the line gsm.SimpleWriteln("AT+CSQ"); whenever you want?

If you are wondering about how to manage the timing have a look at how millis() is used in the demo several things at a time

...R

Hmmm I definitly tried using it by itself several times but ended up getting no reply. I'll try your suggestions with regards to timing however.

I am using the SIM900 library to send the command as the GSM library does not compile correctly for me.

The two libraries are for completely different hardware. Which hardware do you have?

TinySine's GSM/GPRS shield. I was given it when I started developing on Arduino.

Hey all

So I managed to figure out the library and create an automated solution. I'll modify it again later to consider millis instead of delay but for now it works.

#include "SIM900.h"
#include <SoftwareSerial.h>

int numdata;
char inSerial[40];
int i=0;


void setup()
{
  //Serial connection.
  Serial.begin(9600);
  Serial.println("GSM Shield testing.");
  //Start configuration of shield with baudrate.
  //For http uses is reccomended to use 4800 or slower.
  if (gsm.begin(9600))
    Serial.println("\nstatus=READY");
  else Serial.println("\nstatus=IDLE");
};

void loop()
{
  serialhwread(); //for inputting commands

  serialswread(); //for reading in data from the Arduino

};

void serialhwread()
{
  gsm.SimpleWriteln("AT+CSQ");
  delay(60000);
}

void serialswread()
{
  gsm.WhileSimpleRead();

}

It turns out the simpleRead method only printed out one character at a time so I checked and found a Read statement that takes in sentences as opposed to a per character basis.