GSM Interfacing problem.

Go To Last Post
37 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have interfaced GSM 300 with the atmega32 to send an sms, while trying to send sms I am getting error +CMS ERROR: 518. Plz help me. I have attached my code below. I programmed using avr studio 4

 

#include <avr/io.h>

#define F_CPU 8000000

#define BAUD 9600                                
#define BAUDRATE ((F_CPU)/(BAUD*16UL)-1) 
#include <util/delay.h>
#include <string.h>

void sendSMS();
void USART_Transmit(char data);
void senddata(char string[]);
void USART_Init();

int main()
{
     USART_Init(); 
     sendSMS();
}

void sendSMS()
{
     senddata("AT+CMGF=1");
     USART_Transmit(13);
     _delay_ms(500);
     
     senddata("AT+CMGS=09487554063");
     USART_Transmit(13);
     _delay_ms(10000);
     
     senddata("HiHari");
     USART_Transmit(13);
     _delay_ms(100);
     USART_Transmit(26);
     _delay_ms(500);
}
 
void USART_Transmit(char data )
{
     while ( !( UCSRA & (1<<UDRE)) )
     ;
    UDR = data;
}
 
void senddata(char string[])
{
      int len,count;
      len = strlen(string);
        
       for (count=0;count<len;count++)
      {
        USART_Transmit(string[count]);
     }
}
 
void USART_Init()
{
    UBRRH=(BAUDRATE>>8); 
    UBRRL=BAUDRATE; 
    UCSRB|=(1<<RXEN)|(1<<TXEN); 
    UCSRC|=(1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1);
}

Last Edited: Tue. Apr 18, 2017 - 09:21 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

At which action does the error message occur? (That is, which senddata() call just precedes the error message)?

 

It appears to come from your GSM module so it is likely that there is a message format error. Or, maybe you need to wait for a response from the module at some point?

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Mon. Apr 17, 2017 - 03:58 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

At the message change mode AT+CMGF=1 it gives OK message but at the next mode AT+CMGS="094********" it give +CMS ERROR=518.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Then I will bet that there is something about that line that the module does not "like". Google is your friend, here. Just search for CMS ERROR=518 and you will get a number of hits with explanations. Here are several. Pretty sure that your answer is in one of these:

 

http://stackoverflow.com/questio...

 

http://www.kannel.org/pipermail/...

 

http://www.smssolutions.net/tuto...

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The CMS error 518 is 'Invalid parameter'. There is only one parameter, the phone number, so there's something wrong with that.

A quick google for 'At cmgs example' gives you this:http://www.developershome.com/sms/cmgsCommand.asp .

There it says that the phone number needs to be enclosed in quotes in the command sent to the SIM300 module.

ALSO: If you want to anonymise the phone number then you should do it in your first post also...

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Mon. Apr 17, 2017 - 08:58 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hari_22 wrote:
I have interfaced GSM 300

You mean a SIM300 module - from SIMCOM ?

 

 I am getting error +CMS ERROR: 518

So have you looked-up what that error means?

 

Have you tried to reproduce that error by sending commands manually at a terminal?

 

     senddata("AT+CMGF=1");
     USART_Transmit(13);
     _delay_ms(500);

     senddata("AT+CMGS=09487554063");
     USART_Transmit(13);
     _delay_ms(10000);

     senddata("HiHari");
     USART_Transmit(13);

You are making the classic mistake of just sending commands blindly - without waiting for the responses from the module.

 

This is like driving a car with your eyes shut, relying only on time delays for when to turn the wheel!!

 

surprise

 

You must wait for - and act upon - the responses from the module!

 

In particular, after sending

AT+CMGS=09487554063

to the module, you must wait for it to give you the

>

prompt before entering the message text!

 

Also, remember that sending an SMS takes a significant time - so, again, you must wait for the module to report that it's ready before attempting the next message.

 

It shouldn't be  necessary to repeat AT+CMGF before each message - should it?

 

Note that this has nothing to do with AVR - the same applies whatever is controlling the module!

 

#BlindDelays

 

Last Edited: Mon. Apr 17, 2017 - 09:52 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:
the phone number needs to be enclosed in quotes in the command sent to the SIM300 module

Correct. And not just the SIM300 - this is a standard GSM command.

 

In post #1, Hari_22 wrote:

     senddata("AT+CMGS=09487554063");

So the number is not correctly quoted;

 

But, in post #3, Hari_22 wrote:
AT+CMGS="094********"
where the number is correctly quoted!!

 

So Hari_22 needs to get the story straight here ... 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you, When I am testing directly with the serial terminal with pc its working. But in code I get a probelm. I also added a delay of 1000ms for respond. Whether I have to add AT+CMGF=1 every time before transmitting data also?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
     senddata("AT+CMGS=09487554063");

 

This is incorrect format?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hari_22 wrote:
This is incorrect format?
 

 

Did you not read #5 and #7 ?!

Last Edited: Mon. Apr 17, 2017 - 10:41 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hari_22 wrote:
I also added a delay of 1000ms for respond. Whether I have to add AT+CMGF=1 every time before transmitting data also?

 

Did you not read #6 ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ya I read it sir. I had tried too. but no response. Please tell me whether my senddata function is also correct in my program.

Last Edited: Mon. Apr 17, 2017 - 10:46 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

@Hari,

 

I think that Andy means that you need the quote characters e.g.

     senddata("AT+CMGS=\"09487554063\"");

But I have not read the docs.

 

If you type the commands from a terminal,  you would see exactly how the GSM module responds.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So what, exactly, did you "try" ?

 

Before, you said you were getting  +CMS ERROR: 518 response - are you now getting nothing at all?

 

Are you certain that your AVR baud rate is correct?

 

Again:

 

You must wait for - and act upon - the responses from the module!

 

And, to be clear, you must do this for each and every command that you send to the module. 

 

Last Edited: Mon. Apr 17, 2017 - 10:54 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Bart Simpsons Chalkboard

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Hari_22 wrote:

     senddata("AT+CMGS=09487554063");

 

This is incorrect format?

 

Yes. You need to realize that the quotes in that is used merely to inform the compiler that this is a string of characters. The above will not send any quotes to the module. In order to send quotes to the module, you must have quote characters inside the quoted string in your source code, as shown in #13 above.

 


 

Assuming everything goes correctly without errors your approach of having delays so as not to "overrun" the SIM module might work. But that is a dangerous assumption. If anything can go wrong, it eventually will.. So, David is urging you to actually read back the responses from the module, and act if they indicate an error. The simplest but absolutely necessary thing to do upon an error is to not execute the rest of the current "action", so e.g. if you get the error you are discussing here (from the AT+CMGS command) you will not send the message string etc to the module at all, since this will generate more errors which i) will confuse you, and ii) might set the module into an unknown state you don't want.

 


 

It is time for you to step back. Read some documentation. Locate some good examples on the net and study them - I would be very surprised if no-one has published an example involving having an AVR sending SMS messages using a SIM module. In fact - I would be surprised if there are not several such examples out there.

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:
I would be surprised if there are not several such examples out there.

Sadly, there is a huge number of bad examples which make exactly the same mistake - not paying attention to responses from the module.

 

The fundamental nature of AT Commands is that it is a command-response protocol:

 

You send a command, then you must wait for the response(s) before proceeding to the next command.

 

The response is the module's indication that it is ready for the next command.

 

V.250 is now the formalised AT commands spec; 

 

http://www.8052.com/forum/read/1...

 

http://www.8052.com/forum/read/1...

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As already noted, none of this has anything specifically to do with AVR - exactly the same would apply to any microcontroller or any other device using AT Commands to control a device.

 

This is the way AT Commands work - it is not related to the particular controller device.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Get it to work from a terminal before you try to control it from a program.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you all. Its working. I changed the string part in code.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So you're still blindly relying upon delays?

 

As already noted, this may appear to "work" (sic) for a few casual tries under "clean" conditions - but is by no means a reliable or robust solution!

Last Edited: Tue. Apr 18, 2017 - 08:25 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Hari, 

At the message change mode AT+CMGF=1 it gives OK message but at the next mode AT+CMGS="094********" it give +CMS ERROR=518.

As Andy has said repeatedly, check and act upon response.  You will get an OK or an error message. If you get OK then you can immediately proceed with next command and if not you handle the error appropriately. 

 

David

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

DAFlippers wrote:
If you get OK then you can immediately proceed with next command

 

There's another downside to delays!

 

With delays, you always have to use the worst case - ie, longest - delay. Clearly, this will unnecessarily slow down your systems when things are going well!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just a quick thanks @awneil for the links! Great stuff!

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have added a delay too after each command process.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hari_22 wrote:
I have added a delay too after each command process.

 

You're really not listening - are you?!

 

< rolls eyes >

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just typed a long reply but Freaks ate it. I guess it doesn't matter as OP clearly is not going to listen to any advice anyway!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
OP clearly is not going to listen to any advice anyway!

Indeed.

 

frown

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

S sir, I could not get what you said in #21.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hari_22 wrote:
S sir, I could not get what you said in #21.

It's not rocket science!!

 

I wrote:
So you're still blindly relying upon delays?

See #6: "This is like driving a car with your eyes shut, relying only on time delays for when to turn the wheel!!"

 

And #14,15: "You must wait for - and act upon - the responses from the module!"

 

And #16: "read back the responses from the module, and act if they indicate an error"

 

And #17: "You send a command, then you must wait for the response(s) before proceeding to the next command"

 

And #22: "check and act upon response. You will get an OK or an error message. If you get OK then you can immediately proceed with next command and if not you handle the error appropriately"

 

I wrote:
As already noted, this may appear to "work" (sic) for a few casual tries under "clean" conditions - but is by no means a reliable or robust solution!

 

See #16: "If anything can go wrong, it eventually will"

 

Again, just blindly using delays is like driving a car down the street with your eyes shut, relying only on time delays for when to turn the wheel - think about it: how well do you think that would work in real life??!!

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You could try sending:

ATV0

Then your device will return a numeric result code, which you may find easier then text to capture and process!

ie.

instead of "OK"

it returns "0"  (zero) on successful command execution.

 

Jim

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:
You could try sending:

ATV0

Then your device will return a numeric result code, which you may find easier then text to capture and process!

Yes, that always used to be my advice - but I don't think it works any more.

 

frown

 

There just aren't numeric equivalents for all the new GSM errors - such as the +CMS ERROR... reported here.

 

frown

 

EDIT: 

 

Example (for a PSTN modem): http://www.8052.com/forum/read/7... - note the 2004 date!

 

 

Last Edited: Tue. Apr 18, 2017 - 05:37 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hari_22 wrote:

 

void sendSMS()
{
     senddata("AT+CMGF=1");
     USART_Transmit(13);
     _delay_ms(500);
     
     senddata("AT+CMGS=09487554063");
     USART_Transmit(13);
     _delay_ms(10000);
     
     senddata("HiHari");
     USART_Transmit(13);
     _delay_ms(100);
     USART_Transmit(26);
     _delay_ms(500);
}
 
 

 

 


senddata(AT+CMGS="+91XXXXXXXXXX\r\n");
_delay_ms(500);
senddata("Your Message");
_delay_ms(500);
USART_Transmit( 26 );  // Decimal 26 (Ctrl+Enter)
_delay_ms(1000);

 

You are sending decimal 13 after string, Do not send it and check with above code. (Just follow the method and check  ).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Gangusa wrote:
You are sending decimal 13 after string

Of course he is; 13 is the ASCII code for Carriage-Return - this is perfectly correct!

 

 Do not send it

Wrong.

 

senddata(AT+CMGS="+91XXXXXXXXXX\r\n");

No, that is wrong - your quotes are in the wrong place.

 

Also, the command terminator is just CR - not CRLF

 

_delay_ms(500);

So you haven't been listening, either!

 

USART_Transmit( 26 );  // Decimal 26 (Ctrl+Enter)

The ASCII code 26 is Ctrl+Z - not Ctrl+Enter.

 

check with above code

Checked & found wanting.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Dear Andy, 

 

I hope your head recovers from all the banging against the wall.

 

David 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just one comment, not that I disagree but as I remember, there are different "echo" modes a phone/modem can be in, so make sure that it's in default or set it to the mode you want.

 

And again I would use a terminal first to make sure that I understood everything before starting to makeing it automatic with a program.  

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sparrow2 wrote:
there are different "echo" modes

There are only 2 echo modes: on & off.

 

Or were you thinking of the terse/verbose response mode: http://www.avrfreaks.net/comment... ?

 

I would use a terminal first to make sure that I understood everything

Absolutely!

 

But, from #7. it appears that he had done this - and then incorrectly implemented the command in his code!