0

I'm trying to receive an SMS and show it on the serial monitor.

My code is as following:

void setup() 
{

  Serial.begin(2400);
  Serial.write("AT+CMGF=1\r");           //set GSM to text mode
  delay(1500);

  Serial.write("AT+CPMS=\"SM\"\r");         //Preferred SMS Message Storage
  delay(1000);

  Serial.write("AT+CMGL=\"ALL\"\r");       //List SMS Messages from Preferred Storage
  while(1)
  {
    if(Serial.available())
    {
      Serial.write(Serial.read());  
    }   
  }
}

void loop() 
{
}

The circuit diagram is as shown below.

enter image description here

The output that I am getting is as following:

AT+CMGR=1
AT+CMGF=1

OK
AT+CPMS="SM"

ERROR
AT+CMGR=1
AT+CMGF=1

OK
AT+CPMS="SM"

ERROR

ERROR
AT+CMGR=1
AT+CMGF=1

I am not able to use AT+CPMS or AT+CMGL commands.

5
  • I put your code inside the code block exactly as it was. Please clean it up because there are extra curly braces.
    – Wesley Lee
    Commented Oct 23, 2017 at 17:33
  • Your output doesn't match your code. The output says you are sending CMGR and CMGF in one line, but your code only sends CMGF.
    – JRE
    Commented Oct 23, 2017 at 17:36
  • Cleaned the code.
    – Ryhot
    Commented Oct 23, 2017 at 17:56
  • @JRE : please check the updated output.
    – Ryhot
    Commented Oct 23, 2017 at 18:05
  • ERROR SOLVED:For receiving SMS, 5V supply from arduino is insufficient , thus 12v 1 amp supply is required separately.I had faulty adapter, changed it and ERROR got resolved. Though still not able to read messages Because CPMS command gives 0,20 that means 0 sms present and max memory is 20 SMS.PS: I have SMS stored on sim.
    – Ryhot
    Commented Oct 24, 2017 at 12:33

3 Answers 3

0

As others have stated, you cannot use the hardware serial port for communication with the device and communication over the USB cable.

OPTION 1
Switch to a different microcontroller, such as the arduino mega with multiple hardware serial ports on board.

OPTION 2
Implement the Software Serial library for communication with the sim card device and use the hardware serial for communication with your computer.

Note: Software Serial implementations are more computationally intensive than the use of hardware serial. Do not expect the same performance from a software serial implementation and do not expect to be able to smoothly run multiple instances of it on a single 8 bit mcu.

Example Code (Untested):

  #include <SoftwareSerial.h>

  void setup() 
  {
  Serial.begin(9600);
  SoftwareSerial mySerial(10,11); //RX,TX pins
  mySerial.begin(2400);
  mySerial.write("AT+CMGF=1\r");           //set GSM to text mode
  delay(1500);

  mySerial.write("AT+CPMS=\"SM\"\r");         //Preferred SMS Message Storage
  delay(1000);

  mySerial.write("AT+CMGL=\"ALL\"\r");       //List SMS Messages from Preferred Storage
  while(1)
  {
    if(mySerial.available())
    {
      Serial.write(mySerial.read());  
    }   
  }
}


void loop(){}

Disconnect from pin 0 and pin 1 and connect to pins 10 and 11 respectively.

1

The Arduino UNO that you are using only supports one Serial connection (over the TX and RX pins). Since you are using these pins to talk to the device, you cannot also send data over Serial.

Read this article to see more about Serial and how it works on different boards.

3
  • So what is to be done as an alternative ?
    – Ryhot
    Commented Oct 23, 2017 at 17:12
  • @Ryhot I'm not sure, there isn't really much that you can do. You could look into getting an LCD display and then you could write the SMS messages to that. Or, alternatively you could just use a MEGA which has more than 1 Serial port. Please up-vote the answer if it helped you!
    – Joe Iddon
    Commented Oct 23, 2017 at 17:17
  • Here it seems to work . learnerswings.com/2014/03/…
    – Ryhot
    Commented Oct 23, 2017 at 17:33
0

The RX/TX pins are on Uno connected to the USB chip. If you connect a device to RX/TX, you should not use Serial Monitor. It sometimes works, but the device receives the data send to Serial Monitor too and can't process them.

Use SoftwareSerial library for connecting the serial device, if you want to have Serial free for printing to Serial Monitor over USB.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.