Unable to read sms with a SIM900 board

Hi,
I have a classical SIM900 board, I can place call, it works well, but for whatever reason, I cannot read the incoming sms.
Here is the code for the sms :

#include <SoftwareSerial.h>
char data = 0; // Will hold the incoming character from the GSM shield
SoftwareSerial SIM900(7, 8);

void setup()
{
Serial.begin(19200);
Serial.println("Power on");
SIM900.begin(19200);                                                              // SIM900power();
SIM900power();
delay(20000);
SIM900.print("AT+CMGR=1\r"); // set SMS mode to text
delay(100);
SIM900.print("AT+CNMI=2,2,0,0,0\r"); // blurt out contents of new SMS upon receipt to the GSM shield's serial out
delay(100);
}

void SIM900power()
// software equivalent of pressing the GSM shield "power" button
{
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(7000);
}

 
void loop()
{
if(SIM900.available() >0)
  {
  data=SIM900.read();
  Serial.print("SMS received, data : ");
  Serial.println(data);
  delay(500);
  }
}

The answer I have on the Serial Monitor is the following :
17:57:09.451 → SIM900.println() Power on
[here, I send the sms “A”]
SMS received, data : ⸮
SMS received, data :
SMS received, data :
etc…

Why do I receive “⸮”, then nothing, and not my text ?
I was not able to figure out in other post why…
Anyone could help ?
Thanks !

You need to set first the text mode for sms with AT+CMGF=1

+CMGR Will read one sms

Hi, thanks for this, but I tested this, and in both ways the result was the same... "SMS received, data : ⸮" Any other idea ? Thanks

Is your baud rate set correctly?

Can we see the code you use to test? Can you show what the console said when you send those commands to your module?

gpena2087: Is your baud rate set correctly?

clearly it’s correct (as the rest of OP’s text displays correcyly)

The code is the one I put in my first post. I re-run it with CMGF with the same effect.

I added some additional variables in the following code to try to better understand the issue, but without real change :

#include <SoftwareSerial.h>
char data = 0; // Will hold the incoming character from the GSM shield
String data2 = "000";
SoftwareSerial SIM900(7, 8);

void setup()
{
Serial.begin(19200);
Serial.println("Power on");
SIM900.begin(19200);                                                              // SIM900power();
SIM900power();
//SIM900.println("AT+CPIN=\"282\"");
delay(20000);
SIM900.print("AT+CMGF=1\r"); // set SMS mode to text
delay(100);
SIM900.print("AT+CNMI=2,2,0,0,0\r"); // blurt out contents of new SMS upon receipt to the GSM shield's serial out
delay(100);
}

void SIM900power()
// software equivalent of pressing the GSM shield "power" button
{
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(7000);
}

void loop()
{
Serial.print("data : ");
Serial.println(data);
Serial.print("data2 : ");
Serial.println(data2);
if(SIM900.available() >0)
  {
data=SIM900.read();
Serial.print("SMS received, data : ");
  Serial.println(data);
  delay(500);
  data2=SIM900.read();
  Serial.print("SMS received, data2 : ");
  Serial.println(data2);
  }
Serial.print("SIM600.available() : ");
Serial.println(SIM900.available());
Serial.print("SIM900.println() :   ");
SIM900.println();
if ((data == "A") or (data2 == "A"))
  Serial.println("**********************SMS received ***************");
}
[code]

Some lines are useless, but i was just trying different things.
On the serial monitor, I have :

Power on
data :
22:27:25.706 -> data2 : 000
22:27:25.706 -> SIM600.available() : 0
22:27:25.706 -> SIM900.println() :   data :
22:27:25.739 -> data2 : 000
22:27:25.739 -> SIM600.available() : 0
22:27:25.739 -> SIM900.println() :   data :
etc..
and when a sms is received :
22:29:18.580 -> SMS received, data : ⸮
SMS received, data2 : 0
22:29:19.078 -> SIM600.available() : 4
22:29:19.111 -> SIM900.println() :   data : ⸮
22:29:19.111 -> data2 : 0
22:29:19.111 -> SMS received, data :
SMS received, data2 : 0
22:29:19.608 -> SIM600.available() : 17
22:29:19.641 -> SIM900.println() :   data :
then it continues like that with the 17 raising up and down till 0...

On the serial monitor, I have :

Power on data : 22:27:25.706 -> data2 : 000 22:27:25.706 -> SIM600.available() : 0 22:27:25.706 -> SIM900.println() : data : 22:27:25.739 -> data2 : 000 22:27:25.739 -> SIM600.available() : 0 22:27:25.739 -> SIM900.println() : data : etc.. and when a sms is received : 22:29:18.580 -> SMS received, data : ⸮ SMS received, data2 : 0 22:29:19.078 -> SIM600.available() : 4 22:29:19.111 -> SIM900.println() : data : ⸮ 22:29:19.111 -> data2 : 0 22:29:19.111 -> SMS received, data : SMS received, data2 : 0 22:29:19.608 -> SIM600.available() : 17 22:29:19.641 -> SIM900.println() : data : then it continues like that with the 17 raising up and down till 0...

Don’t give us useless code, build a minimum working code to play with, focus on solving the current issue...

You need to show us the output of all those AT commands to see if they all answer OK

Also you test if at least 1 byte is available with if(SIM900.available() >0) but you read 2.

Also this does not workif ((data == "A") or (data2 == "A"))data is a char.. don’t you get warning at compile time?

Read this tutorial Serial Input Basics to understand how to deal with Serial

You set variable ,,data,, as a char and in the buffer you will have word or sentance. Try to avoid this simply by going with

If(SIM900.available>0) Serial.write(SIM900.read())

Thats the first thing. Second one is that in my experience, baud rates on my arduino uno higher then 9600 dosnt work properly. Try with 9600 because I dont know what arduino you are using.

Based on your inputs, I tried the following :

#include <SoftwareSerial.h>
char data = 0;
SoftwareSerial SIM900(7, 8);

void setup()
{
Serial.begin(9600);
Serial.println("Power on");
SIM900.begin(9600);                                                     
SIM900power();
delay(20000);
SIM900.print("AT+CMGF=1\r"); 
delay(100);
SIM900.print("AT+CNMI=2,2,0,0,0\r"); 
delay(100);
}

void SIM900power()
{
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(7000);
}

 
void loop()
{
if(SIM900.available() >0)
  {
  data=SIM900.read();
  Serial.print("SMS received, data : ");
  Serial.println(data);
  delay(500);
  }
}

And I get :
Power on
⸮SMS received, data : ⸮
⸮SMS received, data : ⸮
⸮SMS received, data : ⸮
⸮SMS received, data : ⸮

I hope I have well integrated your remark :

You set variable ,data, as a char and in the buffer you will have word or sentance. Try to avoid this simply by going with

If(SIM900.available>0)
Serial.write(SIM900.read())

As I was not sure, I tried the following :

#include <SoftwareSerial.h>
const byte numChars = 32;
char data[numChars]; 
boolean newData = false;
SoftwareSerial SIM900(7, 8);

void setup()
{
Serial.begin(9600);
Serial.println("Power on");
SIM900.begin(9600);                
SIM900power();
delay(20000);
SIM900.print("AT+CMGF=1\r"); 
delay(100);
SIM900.print("AT+CNMI=2,2,0,0,0\r");
delay(100);
Serial.println("Ready");
}

void SIM900power()
{
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(7000);
}

 
void loop()
{
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
    while (SIM900.available() > 0 && newData == false) {
        rc = SIM900.read();
        if (rc != endMarker) {
            data[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            data[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }

  Serial.print("data : ");
  Serial.println(data);
  delay(500);
  }

and I got this :

Power on
Ready
data :
data :
data :
data :
data : ⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮
data : ⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮
data : ⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮
data : ⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮
data : ⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮

So it is still not convincing !

One point I have not understood :

You need to show us the output of all those AT commands to see if they all answer OK

Can you explain what I should do if what I copied above is incomplete (and sorry for the stupid question…).

Thanks !

J-M-L: clearly it’s correct (as the rest of OP’s text displays correcyly)

Not true. The 4G shield I use must be set to the same baud rate as the monitor otherwise serial.print info will display but info from the shield will be garbage.

gpena2087:
Not true. The 4G shield I use must be set to the same baud rate as the monitor otherwise serial.print info will display but info from the shield will be garbage.

I think you are confusing things. There are 2 serial communication going on, on two separate Serial lines

Arduino <–> GSM module → They need to agree on what baud rate to use
Arduino <–> Serial Console on the PC → They need to agree on what baud rate to use

There is nothing imposing that the baud rates are the same. You can talk at 9600 bauds to your GSM on Serial1 and Serial.print() to the console at 115200 bauds. (with hardware or Sowftare Serial if your arduino does not have 2 Serial ports)

In the user manual, the speed btw the Arduino & the GSMS module should be set up at 19200 baud. I tried with different speed with nil effect… Still do not understand why it is not working…

The speed between the Arduino and GSM can vary - that’s the first think you need to establish

Use a default piece of code listeneing to the Serial console input and relay that to your gsm and print out what you receive back

If by send AT you don’t get an OK back there is no need to dig further… change baud rates until your modules connect properly

(Provide information on how those things are wired and powered as well
And which module you exactly have)