Problem displaying message on serial monitor

HI,
I am trying to view a message on the serial monitor from my SIM card using a gsm module through AT commands.All is happening well except the message does not display and I think there may be some coding fault while reading serially please help. Following is the code.

#include <SoftwareSerial.h>
SoftwareSerial cell(2,3);
char incoming_char = 0;
String content = "";

void setup()
{
 //Initialize serial ports for communication.
 Serial.begin(9600);
 cell.begin(9600);
 Serial.println("Starting GSM Communication...");
 cell.println("AT");
 delay(2000);
 show();
 delay (2000);
 cell.println("AT+CMGF?");
 delay(2000);
 show();
 delay(2000);
 cell.println("AT+CMGR=1");
 delay(2000);
 show();

}
void loop()
{;
}

void show()
{
  content = "";
  while(cell.available()!=0)
  {
   if( cell.available() > 0 )
   {
 //Get the character from the cellular serial port.
 incoming_char = cell.read();
 content = content + String(incoming_char);
   }
  }
  Serial.print(content);
}

This is the output where everything is printed out except message

Message print.jpg

Murtaza_Ilyas:
everything is printed out except message

Are you absolutely sure, that “cell.begin(9600);” is the correct baud rate?

Although the “SoftwareSerial” library is one of the worst libraries that comes with Arduino, you normally should see at least some correct and some strange characters in the response, if the hardware connection is made correctly and the baud rate is correct.

Is your GSM module working with 5V logic level?

If your GSM module works with 3.3V logic level, the 5V level on the Arduino TX might be much too high and you would need level shifters in your circuit.

yes my GSM is working with 5V logic level and when i change the baud rate to 4800 or 19200 strange characters start to appear.The other thing is that if baud rate was the issue i think the other things should not have been printed.

Murtaza_Ilyas: yes my GSM is working with 5V logic level and when i change the baud rate to 4800 or 19200 strange characters start to appear.The other thing is that if baud rate was the issue i think the other things should not have been printed.

You are absolutely correct. As other messages like "OK" are printed, baud rate is correct. My mistake.

Does it make a difference if you give 5 seconds time for responding instead of 2 seconds:

 cell.println("AT+CMGR=1");
 delay(5000);
 show();

Does this make a difference?

 while(cell.available()!=0)

I suspect this is the problem. At 9600 baud you only get a character every millisec or so. But this test will repeat within microseconds and there won't be any data.

Look at how data is received in the examples in serial input basics - they do not depend on time.

If the delay()s that you have are only ever needed in setup() they won't do any harm. But if you need to manage timing in loop() use millis() and NOT delay() - see the demo several things at a time.

...R

Thank you Robin2 i will try it out. While sending data I have to use delays because apart from this example actually i have to connect to a website using gprs so to do that i have to give a delay to the next command until connection is established.

jurs: Does it make a difference if you give 5 seconds time for responding instead of 2 seconds:

 cell.println("AT+CMGR=1");
 delay(5000);
 show();

Does this make a difference?

Yes I tried 5000 but there was no change.

Murtaza_Ilyas: While sending data I have to use delays

Use millis() rather than delay() to manage timing without blocking as shown in several things at a time.

...R

I read your post in (several things at a time) and i made a function for sending gsm commands but i think there is something wrong with it can you please look at it.And one question, does millis() renew in each iteration of loop.

#include <SoftwareSerial.h>
SoftwareSerial cell(2,3);
char incoming_char = 0;
String content = “”;

const int Interval1 = 500;
const int Interval2 = 1000;

unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()
unsigned long previousMillis = 0;

void setup()
{
//Initialize serial ports for communication.
Serial.begin(9600);
cell.begin(9600);
Serial.println(“Starting GSM Communication…”);

}
void loop()
{currentMillis=millis();
GSM();

}

void GSM()
{ cell.println(“AT”);
show();
if (currentMillis - previousMillis >= Interval1){
cell.println( “AT+CMGF=1”);
previousMillis += Interval1;
}
show();
if(currentMillis - previousMillis >= Interval2){
cell.println(“AT+CMGR=1”);
previousMillis += Interval2;
}
show();
Serial.println(previousMillis);
Serial.println(currentMillis);
}

void show()
{
content = “”;
while(cell.available()!=0)
{
if( cell.available() > 0 )
{
// Get the character from the cellular serial port.
incoming_char = cell.read();
content = content + String(incoming_char);
}
}
Serial.print(content);
}

Murtaza_Ilyas:
I read your post in (several things at a time) and i made a function for sending gsm commands but i think there is something wrong with it can you please look at it.

Perhaps you can use that:

void show(unsigned int delayTime)
{
  boolean timeout=true;
  unsigned long time=millis();
  while(millis()-time<delayTime)
  {
    if (cell.available())
    {
      timeout=false;
      Serial.write(cell.read());
    }
  }
  if (timeout) Serial.println("[TIMEOUT]");
}

So instead of your original code two lines of code:

 delay(2000);
 show();

you then would use a single line:

show(2000);

Murtaza_Ilyas:
And one question, does millis() renew in each iteration of loop.

The function call “millis()” will retrieve always the current value of a milliseconds counter when you call the function. So each call of “millis()” in a loop retrieves the current value at that moment.

Or perhaps use an even more advanced function to send the GSM command as well as showing the result:

void gsmCommand(char* command, unsigned int delayTime)
{
  boolean timeout=true;
  unsigned long time=millis();
  cell.println(command);
  while(millis()-time<delayTime)
  {
    if (cell.available())
    {
      timeout=false;
      Serial.write(cell.read());
    }
  }
  if (timeout) Serial.println("[TIMEOUT]");
}

Called in the code by (example):

  gsmCommand("AT+CMGR=1",2000);

It is very difficult to copy your code into my text editor when you don’t use code tags (the scroll with <> on it)

so it looks like this

.
As far as I can see you still have

while(cell.available()!=0)

even though I mentioned it in Reply #4

Use one of the examples from serial input basics to receive your data (as also suggested in Reply #4) Those examples do not rely on time and neither do they block the Arduino.

…R

but i think there is something wrong with it can you please look at it.

Sure enough. Its right here:

String content = "";

thank you all for your replies. Sorry Robin2 i didn't know how to put codes in quick reply so i just copy pasted.I also checked it by commenting while loop but still no success. @jurs thank you I am going to try it. @PaulS can you please explain why String content = ""; is a problem.

@PaulS can you please explain why String content = ""; is a problem.

You have limited memory on an Arduino. Using a class that makes swiss cheese of that memory is not smart.

In the end, there is a maximum amount of data that you can handle. Learn to do that using NULL terminated arrays of chars (otherwise known as strings (which are NOT the same thing as Strings)) and save your memory for important things.

Murtaza_Ilyas: I also checked it by commenting while loop but still no success.

I did not mean that you should just comment-out the code. I meant that you should think about why it probably does not work.

Have you tried the examples in serial input basics ?

...R