Antoniolinux
Antoniolinux

Reputation: 1

send SMS with SIM 900 with st nucleo

i use a Sim900 of SimCOM if i sand AT command or other commands all work well, but i have a problem for sand a SMS i write this code

    #include "mbed.h"
#include <string>
Serial pc(SERIAL_TX, SERIAL_RX); // pc comunication
Serial SIM900(PA_9, PA_10);   //tx, rx SIM 900
string result;
void power(){
 sim_power.write(1); // accension gsm
    wait(1);
    sim_power.write(0);
    wait(13);
    SIM900.printf("ATE0\r\n");  // DISABLE ECHO 
    wait(1);}
 void clearString()
 {  result.clear();   }
 void callback_rx() { 
while (SIM900.readable()) {
  x = SIM900.getc();
  result += x;
pc.putc(x);      
   }
   }
 void sendSMS()
 {
 clearString();
 SIM900.printf("AT+CMGF=1\r"); //at command for send sms
 wait_ms(100);
 clearString();
 wait_ms(100);   
 SIM900.printf("AT+CMGS=");
 SIM900.putc('"');
 SIM900.printf("+32292*******"); 
 SIM900.putc('"');
 SIM900.printf("\r");
 SIM900.printf("Example Message SMS");
 SIM900.putc(0x1A);
 wait_ms(20000);
 } 
 int main() {    
 power();
 pc.printf("\r\n GSM 900 TEST\n"); 
 SIM900.attach(&callback_rx);  
 SIM900.baud(9600); // 
 wait_ms(100);         
  while(1) {
 wait_ms(10);
 sendSMS();  // SEND SMS     
 wait_ms(100);                 
 } 
 }

i see this when i send the sms but i have this strange message:

AT+CMGF=1AT+CMGS="+32292*"" Example Message SMS

i see that the system send the message later two loop, i don't know where is the error can you help me?

best regards. A.

Upvotes: 0

Views: 5456

Answers (2)

Hamza BENDALI BRAHAM
Hamza BENDALI BRAHAM

Reputation: 152

Try this,

void sendSMS(string number, string message){
  SIM900.printf("AT+CMGF=1\r\n");
  wait_ms(100);
  SIM900.printf("AT+CMGS=\"%s\"\r\n",number.c_str());
  wait_ms(100);
  SIM900.printf("%s ",message.c_str());  
  SIM900.putc(0x1A);
}

and call this function by

sendSMS("+32292*******", "Example Message SMS");

Upvotes: 0

ayhankorkmaz
ayhankorkmaz

Reputation: 88

I think you have some timing problem. You must wait until you get answer from SIMCOM after commonds,Like "OK" . After getting answer, you can send other commands

Upvotes: 1

Related Questions