Hello,
I am trying to use the STM32107VC EVAL board to send a message using a GSM Modem by sending AT commands via the USART.
I have created the following code for the same. The output is showing on the hyper terminal, but it is still not sending messages through the Modem.
Can anyone please help and suggest where I am going wrong?? Thank you!
void init_GSM_Modem(void); const char command_CMGF[]="AT+CMGF=1\r"; //Selecting text mode const char CtrlZ =0x1A; const char command_CMGS[]="AT+CMGS=+91989xxxxxxxx\r"; //Phone number to send message to const char command_AT[]="AT\r"; const char msg01[]="Hello! This is a Test Message via STM32F107"; //Message to be sent /* Leaving out init_serial, SendChar and GetChar Functions for reducing the code length. */ /*---------------------------------------------------------------------------- * init_GSM_Modem : Initialize Modem Commands *---------------------------------------------------------------------------*/ void init_GSM_Modem(void) { delay2(); puts(command_AT); delay2(); puts(command_CMGF); delay2(); puts(command_CMGS); delay2(); puts(msg01); delay2(); while (!(USART2->SR & 0x0080)); USART2->SR & 0x0000; USART2->DR = 0x1A; //sending CtrlZ command } int main(void) { SystemInit(); init_serial(); init_GSM_Modem(); while(1) {;} }
I have a feeling that I am doing something wrong while sending the "CtrlZ command". I wanted to send it when the interrupt flag was raised by hardware and cleared by software.
You need to wait for the response to each AT command before moving on to the next - don't just rely on delays!
For debug, use AT+CMEE=1 to get extended error codes...
Hey Andrew,
Thanks for such a prompt reply. I will try to use the AT+CMEE command after every other AT command.
What would be the approximate time gap for sending out the next command (in seconds)??
Thanks.
"I will try to use the AT+CMEE command after every other AT command"
Please read the description of the AT+CMEE command in your AT Commands manual - you don't use it after each command!
"What would be the approximate time gap for sending out the next command (in seconds)??"
I said do not rely upon arbitrary, "blind" delays!
Instead, your application needs to pay attention to the response for each command sent...
Haven't you used Hyperterminal and played some with a connected modem? Don't you notice that it gives responses? Don't you notice that the responses given relates to the commands you send? Don't you think you should write a program that is clever enough to understand received responses and actually read and process them? Don't you think that would be much more robust? Or you think the responses was just random noise that is best to ignore?
At the very least, only proceed to the next command after receiving "OK" from the previous command.
Note also that AT+CMGS issues a prompt when - and only when - it is ready for the message body...
I guess I haven't tried most of the things you mentioned. Even though its the first time that I'm working on a modem, I was in a hurry to go to the final step rather than going the way you and Andrew suggested. I was under time pressure so thought of skipping it. Forgot the basic principle of programming.
I think I'll be able to work it out better now.
Thank you both. :)
It isn't too uncommon to write some function send_at_cmd() that sends a command and eats any echo while looking for ok or error. And that optionally waits for a specific response string from the modem together with a timeout. And that is able to detect unsolicited strings reported from the modem - like "RING" or similar.
So maybe something like send_at_cmd(cmd,timeout) and send_at_cmd_wait(cmd,response,timeout) and with each call returning a value that indicates if the modem accepted the command or not. And with a function handle_unsolicited(cmd) called if send_at_cmd() or send_at_cmd_wait() did receive any unexpected strings.
Yeah, now I understand it much better. Thanks a lot. I don't have the hardware currently so will check for the responses to the strings sent once back in the lab where I work. This is a much effective way of working.
I'll revert back if I face any problem again. Thanks again.