AT Comands from ATmega32 to SIM900 to send SMS

Status
Not open for further replies.

drtvskuthsav

Member level 2
Joined
Apr 7, 2010
Messages
46
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,630
Dear All,
I was trying to send AT Commands to SIM900 using Serial Communication. Here is My code. It is not working. Please help me with it. Please find the mistakes and correct them.
Baudrate=9600
My module is set for auto baudrate.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <mega32.h>
#include <stdio.h>
#include <stdlib.h>
#asm
   .equ __lcd_port=0x18 ;PORTB
#endasm
#include <lcd.h>
#include <delay.h>
void ustx(void)
{
UDR=0x0d;
UDR=0x0a;
}
void srtx(void)
{
UDR=0x1a;
}
void main(void)
{
UCSRA=0x00;
UCSRB=0x08;
UBRRH=0x00;
UBRRL=0x33;
lcd_puts("hello");
printf("A");
delay_ms(5000);
printf("AT");
ustx();
delay_ms(2500);
printf("AT+IPR=9600");
ustx();
delay_ms(2500);
printf("AT");
ustx();
delay_ms(2500);
printf("AT+CMGF=1");
ustx();
delay_ms(2500);
printf("AT+CSMP=17,167,0,241");
ustx();
delay_ms(2500);
printf("AT+CMGS=\"9566809793\"");
ustx();
delay_ms(2500);
printf("Hello!!");
srtx();
ustx();
delay_ms(2500);
}

 
Last edited by a moderator:

drtvskuthsav said:
Here is My code. It is not working.
Inside ustx():
UDR=0x0d;
UDR=0x0a;


I don't know if 0x0A is needed by your modem, other modems do not need it. In any case it is wrong in this way, try keeping a small delay in between of those 2 commands.
I don't know if it is a good idea to write a program in this way, you don't seem to "listen" to the modem's responce. Instead you are constantly feeding it with commands using delays in between.
Are you are using a terminal software as a sniffer on you transmission lines?
Finally I believe that an interrupt driven communication is the only reasonable choice.
So do you get an answer in the first place? Sending sms is the next step compared to this one.

And a useful reference on this topic:
https://www.developershome.com/sms/
 

So how about this interrupt driven code?


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <mega32.h>
#include <delay.h>
#include <stdio.h>
#include <stdlib.h>
#include <delay.h>
unsigned char si=0,qi=0;
char q[160];
interrupt [USART_TXC] void usart_transmit_isr(void)
{if(qi!=si)
UDR=q[si++];
}
void sendmsg(char *s)
{
qi=0;
si=1;
while(*s)
{q[qi++]=*s++;
}
UDR=q[0];
}
void main(void)
{
UCSRA=0x00;
UCSRB=0x48;
UBRRH=0x00;
UBRRL=0x33;
#asm ("sei")
sendmsg("AT\r\n");
delay_ms(2000);
sendmsg("AT+CMGF=1\r\n");
delay_ms(2000);
sendmsg("AT+CSMP=17,167,0,241\r\n");
delay_ms(2000);
sendmsg("AT+CMGS=\"+917739478576\"\r\n");
delay_ms(2000);
sendmsg("Hello!");
delay_ms(2000);
UDR=0x1A;
}




I tried this with Hyperterminal. Its printing the code as expected. But when I use this with modem am not able to send any messages.
 
Last edited by a moderator:

drtvskuthsav said:
I tried this with Hyperterminal. Its printing the code as expected. But when I use this with modem am not able to send any messages.
Still this code does not listen to the modem's responce, no rx interrupt exists.
Since you are using a terminal software, could you please share what is the modem's responce to your commands?
A snapshot of the terminal screen would be even better.
 

first of all, check your voltage tx, make sure it is compatible with the voltage in of your micro.
 

Still this code does not listen to the modem's responce, no rx interrupt exists.

Can you give me the code to listen to modems response


Since you are using a terminal software, could you please share what is the modem's responce to your commands?
A snapshot of the terminal screen would be even better.

I used Hyperterminal to test if the modem is able to send messages. Do you want me to share the response I got from the modem when I used Hyperterminal?
 

drtvskuthsav said:
I used Hyperterminal to test if the modem is able to send messages. Do you want me to share the response I got from the modem when I used Hyperterminal?
Yes please, a screenshot would be even better.
 

Yes please, a screenshot would be even better.
I attached the snapshot of Hyperterminal when used to communicate with SIM900 Modem
 

Attachments

  • GSM.bmp
    3 MB · Views: 181

It looks OK. The modem responds that the sms left to network.
It may sound stupid, but are you sure that this phone is valid? Maybe you would try to send it by typing the number in international format?
For example in my country the international code is 0030. So I would try sending an sms to 0030123456 or +30123456.
 


Am receiving the message on my phone when I send the message using Hyperterminal. But am not receiving the message when am sending the 'at commands' to modem from ATmega32. I want help with this part. How to send 'at commands' Modem using ATmega32. Am using CodeVision AVR. Can you post some sample code?
 

drtvskuthsav said:
Am receiving the message on my phone when I send the message using Hyperterminal. But am not receiving the message when am sending the 'at commands' to modem from ATmega32.
OK. Can you debug your code?
 

OK. Can you debug your code?

I did not understand what do you mean by debug. When I connect microcontroller to PC via DB9, it is printing the code on Hyperterminal as it should. Then I disconnected it from PC and connected it to MODEM. No message is sent. I don't have a code for receive. Can you give some code?
 

post #1
drtvskuthsav said:
I was trying to send AT Commands to SIM900 using Serial Communication. Here is My code. It is not working.

post #3
I tried this with Hyperterminal. Its printing the code as expected. But when I use this with modem am not able to send any messages.

post #10
Am receiving the message on my phone when I send the message using Hyperterminal. But am not receiving the message when am sending the 'at commands' to modem from ATmega32. I want help with this part.

post #12
When I connect microcontroller to PC via DB9, it is printing the code on Hyperterminal as it should. Then I disconnected it from PC and connected it to MODEM. No message is sent.

OK please decide what is your problem. First you have problems in general, then the problem was that everything looked good but no sms was sent, then you send the sms only through hyperterminal and finally the code is printed OK but no sms is sent. We are not playing games here, give a clear description from the beggining if you want to get help. After 12 posts, still the problem is not clear because you constantly change your mind about what the problem is.


I did not understand what do you mean by debug.
https://en.wikipedia.org/wiki/Debugging


I don't have a code for receive. Can you give some code?
OK, this is how an interrupt routine should look like. I don't use your compiler so the function name is random. You need to set RXCIE bit inside UCSRB register in order to activate rx interrupt.

Code:
volatile unsigned char in_data[200]
volatile unsigned char in_cnt = 0;
void rx_isr (void)
{
  volatile unsigned char _udr = UDR
  if(in_cnt >= 200)
    in_cnt = 0;

  in_data[in_cnt] = _udr;
}

So now you have your incoming data stored inside in_data[] array. I just wonder what are you going to do with those data since no debug will take place...
 


Dear Alexxx,
I know we are not playing games here. And am not changing the mind. Its how you interpret.
Here is what I did so far.
>> First I used hyper terminal and modem to check if I can send SMS.
>>Then I wrote a code to communicate 'AT Commands' to modem. I did not want to check the response of the modem as it would make the code complicated. The modem did not send any message when I connected it with ATmega32.
>>To check if there is any problem with code, I disconnected it(ATmega32) from modem and connected to PC. In Hyper terminal The ATmega32 printed the AT Commands as desired.
 

OK, now you cleared things a bit.

But I still wonder. From the posted bmp in post #8 it is clear that the sms left to network succesfully. What is the situation as we speak?

1. You send sms from hyperterminal but you cannot do that from ATmega?
2. Or now you can send from ATmega as well and all you need is the code for reading modem's responce?

1. If so, then the problem is easy to fix. Since the baudrate is OK (because hyperterminal printed the bytes that the MCU send to the modem), then the problem is you are missing something. This "something" should possibly be control bytes, like \r, or \n or the sub character, after you send sms text. It would help if we could see in hyperterminal the exact same screenshot, but this time in hex bytes and not as text. I know hyperterminal is kind of simple software, I don't know if it can print in hex. There other terminal software you can get for free, please take a look at the below edaboard thread.

https://www.edaboard.com/threads/228501/

2. In that case, the code provided in post #13 is a good starting point. You can add a small part, when modem sends 0x0a, then you must check: is this the first time it is sent? If yes, then you let it continue sending bytes. If now it is the second time, then you are ready to process modem's responce. To understand what I'm talking about, here is a brief example when modem sends "OK":

0x0D 0x0A "OK" 0x0D 0x0A

So you ignore the first 0x0A, but after the second one, you collect the answer and then you clear the buffer.
 


Alexxx
Here I attached the snapshots of terminal software when I used ATmega and Docklight.
 

Attachments

  • gsmterm1.bmp
    1 MB · Views: 119
  • gsmterm2hex.bmp
    3 MB · Views: 122

drtvskuthsav said:
Here I attached the snapshots of terminal software when I used ATmega and Docklight.
So the problem is that the modem does not at all respond to your commands.
As far I saw from the docklight screenshots, you set the baudrate at 9600. From your register values it also seems that you are using an 8MHz crystal (or the internal RC).

Reading the datasheet's note with bold in page 11, someone can find out that the modem operates by default at 115200.
https://robokits.co.in/datasheets/SIM900_ATC_V1_00.pdf

If the problem is not with baudrate (you mentioned that autobaud is used), then you should look into hardware. Commands seem to be OK.
Check out power supply, reset pin, and all power pins of the modem in general.

However, I would make sure 100% that baudrate is not a problem, by reseting modem's factory settings and then try again at 115200.
 


Alexxx but the modem is able to send messages when connected with Hyperterminal. I used Hyperterminal and changed the modem baudrate to 9600 and disconnected it. Then connected it with ATmega32.

Can you once again make sure that my code is ok. The scree shots I've attached are when I connected the ATmega32 with PC to check how the commands are appearing on the Terminal.
 

drtvskuthsav said:
Can you once again make sure that my code is ok.
I have already checked your UART settings and they seem OK. Since we have the screenshot, all the information we need is there.
I just took a closer look to the datasheet. In page 11 again:

The "AT" or "at" prefix must be set at the beggining of each Command line. To terminate a Command line enter <CR>.
Commands are usually followed by a responce that includes "<CR><LF><responce><CR><LF>".

The first screenshot in hyperterminal does not show control bytes. You are using <CR><LF> from MCU instead of just <CR>. Is there a possibility that you omit <LF> when sending from PC?
<LF> is included only in modem's responce.
I don't know if this solves the problem, but you must change that anyway. Try paying closer attention to the datasheet from now on. ;-)


Hope it helped,
Alexandros
 

Alexxx the problem was not solved yet. Both my modem board and The controller board is having DB9 Female pins. So I thought of using DB9 Male pin - Male Pin Connector. But I checked how the pins of this connector are connected internally. When I tested with multimeter I found that Tx pin of one end of connector is connected to Tx at other end and Simlarly Rx-Rx connections. I think this should not be like this. Am I right? Could this be the problem?
My modem is having only single voltage input. So I don't think we need to check Voltage levels. R8??
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…