1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Problem in sending SMS through GSM module using 8051

Discussion in '8051/8951' started by Bob Parihar, Apr 29, 2015.

  1. Bob Parihar

    Bob Parihar Member

    Joined:
    Oct 8, 2014
    Messages:
    87
    Likes:
    0
    iam doing a project on sms sending through gsm
    using SIM 900 module and 8051
    My code is:

    #include<reg51.h>
    unsigned char *command = "AT";
    unsigned char *echo = "ATE0";
    unsigned char *msgConfig = "AT+CMGF=1";
    unsigned char *number = "AT+CMGS=\"8283******\"";
    unsigned char *message = "hello";
    unsigned char CTRLZ = 0x1A; // now as a variable
    void serial_init(void);
    void serial(unsigned char);
    void puts(unsigned char *p );
    void delay(void);

    void main()
    {
    serial_init();
    puts(command);
    serial(0x0D); // /r after each command
    delay(); // delay of approx 1 sec

    puts(echo);
    serial(0x0D);
    delay();

    puts(msgConfig);
    serial(0x0D);
    delay();

    puts(number);
    serial(0x0D);
    delay();

    puts(message);
    serial(0x0D);
    delay();

    serial(CTRLZ); //editing here

    while(1);

    }
    void serial_init(void)
    {

    TMOD=0x20; //timer 1, mode 2(8-bit autoreload) to set baud rate
    TH1=0xFD; //-3 to TH1 for 9600 baud rate
    SCON=0x50; // 8 bit txion, 1 start 1 stop bit, REN enable for both txfr and rxve
    TR1=1; // start timer
    }

    void puts(char *p)
    {
    char *temp = p; /*temp pointer so that the actual pointer is not displaced */
    while(*temp != 0x00)
    {
    serial(*temp);
    temp++;
    }
    }

    void serial(unsigned char x)
    {

    SBUF=x;
    while(TI==0);
    TI=0;

    }
    void delay(void) // delay for approx 1 sec
    {
    int i;
    TMOD=0x01; // timer 0 in mode 1
    for(i=0;i<142;i++)
    {
    TL0=0x00; // starting value from 0
    TH0=0x00;
    TR0=1; // sart timer
    while(TF0==0); // polling TF flag for high
    TR0=0; // stop timer
    TF0=0; // clear flag TF0
    //}
    }
    }

    the problem here is SMS won't send by the GSM

    when i use the calling function by replacing the AT commands then calling works
    but SMS won't.. i think iam wrong in sending AT commands for SMS... please correct me
     
    Last edited: Apr 30, 2015
  2. Tony Stewart

    Tony Stewart Well-Known Member Most Helpful Member

    Joined:
    Aug 31, 2012
    Messages:
    3,151
    Likes:
    281
    Location:
    Richmond Hill , ON Canada near Toronto
    Does SMS need CR or LF?
     
  3. Wp100

    Wp100 Active Member

    Joined:
    Feb 3, 2009
    Messages:
    959
    Likes:
    43
    Location:
    England
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Tony Stewart

    Tony Stewart Well-Known Member Most Helpful Member

    Joined:
    Aug 31, 2012
    Messages:
    3,151
    Likes:
    281
    Location:
    Richmond Hill , ON Canada near Toronto

    There are AT commands to delimit either or both, so I assume that was done.
     
  6. Bob Parihar

    Bob Parihar Member

    Joined:
    Oct 8, 2014
    Messages:
    87
    Likes:
    0
    im not getting you
     
  7. Bob Parihar

    Bob Parihar Member

    Joined:
    Oct 8, 2014
    Messages:
    87
    Likes:
    0
  8. Tony Stewart

    Tony Stewart Well-Known Member Most Helpful Member

    Joined:
    Aug 31, 2012
    Messages:
    3,151
    Likes:
    281
    Location:
    Richmond Hill , ON Canada near Toronto
    Delimiters separate one parameter from the next - they split the command line up into words.
     
  9. Cicero

    Cicero Active Member

    Joined:
    Nov 21, 2014
    Messages:
    335
    Likes:
    36
    Location:
    UK
    He is sending a CR after each line, separately, so its all good there.

    I suspect it may be the final CTRLZ maybe?
    Code (text):

    unsigned char *CTRLZ = 0x1A;

    puts(CTRLZ);
     
    That doesn't send the 0x1A, you're sending the address. Try removing the pointer, unsigned char CTRLZ = 0x1A;

    EDIT: Actually I'm wrong here, CTRLZ does contain 0x1A, its just declared as a pointer though. Which isn't quite right.
     
    Last edited: Apr 30, 2015
  10. Bob Parihar

    Bob Parihar Member

    Joined:
    Oct 8, 2014
    Messages:
    87
    Likes:
    0

    removed pointer...and i used variable... doesn't work
     
  11. Cicero

    Cicero Active Member

    Joined:
    Nov 21, 2014
    Messages:
    335
    Likes:
    36
    Location:
    UK
    EDIT: Please see response below.
     
    Last edited: Apr 30, 2015
  12. Cicero

    Cicero Active Member

    Joined:
    Nov 21, 2014
    Messages:
    335
    Likes:
    36
    Location:
    UK
    Theres also a problem with your puts routine.
    Code (text):

    void puts(char *p)
    {
    char *temp = p; /*temp pointer so that the actual pointer is not displaced */
    while(*temp != 0x00)
    {
    serial(*temp);
    temp++;
    }
    }
    This is only really for sending a string, because its waiting for your null character at the end. If you try send a single unsigned char like CTRLZ, it isn't a string, so it may not have a NULL after it, and it'll just increase its address and send random data until it finds one.

    Rather send CTRLZ using your serial function, like you do for your 0x0D.

    And actually, looking at the Arduino code, you will have to add in LF characters after all - you only send CR's.

    Here's the Arduino code:
    Code (text):
    void sendSMS(String message)
    {
      SIM900.print("AT+CMGF=1\r");                     // AT command to send SMS message
      delay(100);
      SIM900.println("AT + CMGS = \"+12128675309\"");  // recipient's mobile number, in international format
      delay(100);
      SIM900.println(message);                         // message to send
      delay(100);
      SIM900.println((char)26);                        // End AT command with a ^Z, ASCII code 26
      delay(100);
      SIM900.println();
      delay(5000);                                     // give module time to send SMS
      SIM900power();                                   // turn off module
    }
    println() prints both a CR and a LF character. Notice you only print a CR. Also, after printing the 0x1A, there's also a CRLF, and another CR LF when it sends a blank SIM900.println();
     
    Last edited: Apr 30, 2015
  13. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
    You have been given false info.... CTRLZ was fine as a pointer but it needed referencing as puts accepts a pointer.

    puts(CTRLZ);

    Should have been..
    puts(*CTRLZ);

    EDIT** Sorry..... but you will also have to NULL terminate...

    Its best to use

    serial(0x1A);
     
  14. Cicero

    Cicero Active Member

    Joined:
    Nov 21, 2014
    Messages:
    335
    Likes:
    36
    Location:
    UK
    Actually you're wrong Ian. I admitted I was slightly mistaken.

    Please reread my posts, and the edits.
     
  15. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
    I know I know... I have just realized myself...

    The CTRLZ should be used as you said.... I missed that post for some reason....

    I was wrong as well with my second statement.... If he had just terminated the CTRLZ string with a NULL all this would be academic....
     
  16. Cicero

    Cicero Active Member

    Joined:
    Nov 21, 2014
    Messages:
    335
    Likes:
    36
    Location:
    UK
    True, that would be fine.

    It confused me (clearly) as well. As I understand it now...
    CTRLZ we agree was a pointer. But as a pointer, it was assigned to 0x1A. So CTRLZ was in fact 0x1A. However, *CTRLZ was whatever is at address 0x1A - probably an access violation. Just feels a bit messy.

    Like you said, he could've just gone:
    Code (text):
    unsigned char CTRLZ[] = {0x1A, 0x00};
    And it would've been fine as well. Although I'm sure he needs LF's and CR's everywhere to match that Arduino tutorial.
     
  17. Bob Parihar

    Bob Parihar Member

    Joined:
    Oct 8, 2014
    Messages:
    87
    Likes:
    0
    sir, i edited like you suggested... see the code above
    but still got negative results
     
  18. Cicero

    Cicero Active Member

    Joined:
    Nov 21, 2014
    Messages:
    335
    Likes:
    36
    Location:
    UK
    Repost your updated code please
     
  19. Bob Parihar

    Bob Parihar Member

    Joined:
    Oct 8, 2014
    Messages:
    87
    Likes:
    0
    #include<reg51.h>
    unsigned char *command = "AT";
    unsigned char *echo = "ATE0";
    unsigned char *msgConfig = "AT+CMGF=1";
    unsigned char *number = "AT+CMGS=\"8283******\"";
    unsigned char *message = "hello";
    unsigned char CTRLZ = 0x1A; // now as a variable
    void serial_init(void);
    void serial(unsigned char);
    void puts(unsigned char *p );
    void delay(void);

    void main()
    {
    serial_init();
    puts(command);
    serial(0x0D); // /r after each command
    delay(); // delay of approx 1 sec

    puts(echo);
    serial(0x0D);
    delay();

    puts(msgConfig);
    serial(0x0D);
    delay();

    puts(number);
    serial(0x0D);
    delay();

    puts(message);
    serial(0x0D);
    delay();

    serial(CTRLZ); //editing here

    while(1);

    }
    void serial_init(void)
    {

    TMOD=0x20; //timer 1, mode 2(8-bit autoreload) to set baud rate
    TH1=0xFD; //-3 to TH1 for 9600 baud rate
    SCON=0x50; // 8 bit txion, 1 start 1 stop bit, REN enable for both txfr and rxve
    TR1=1; // start timer
    }

    void puts(char *p)
    {
    char *temp = p; /*temp pointer so that the actual pointer is not displaced */
    while(*temp != 0x00)
    {
    serial(*temp);
    temp++;
    }
    }

    void serial(unsigned char x)
    {

    SBUF=x;
    while(TI==0);
    TI=0;

    }
    void delay(void) // delay for approx 1 sec
    {
    int i;
    TMOD=0x01; // timer 0 in mode 1
    for(i=0;i<142;i++)
    {
    TL0=0x00; // starting value from 0
    TH0=0x00;
    TR0=1; // sart timer
    while(TF0==0); // polling TF flag for high
    TR0=0; // stop timer
    TF0=0; // clear flag TF0
    //}
    }
    }
     
  20. Cicero

    Cicero Active Member

    Joined:
    Nov 21, 2014
    Messages:
    335
    Likes:
    36
    Location:
    UK
    Yeah, you still haven't added any LF characters? You've only changed the pointer issue.

    Another thing in the Arduino code, is that the number is in international format...not sure if thats necessary?

    Try the code below which follows the Arduino code virtually as close as possible.
    Code (text):
    #include<reg51.h>
    unsigned char *command = "AT";
    unsigned char *echo = "ATE0";
    unsigned char *msgConfig = "AT+CMGF=1";
    unsigned char *number = "AT + CMGS =\"8283******\"";
    unsigned char *message = "hello";
    unsigned char CTRLZ = 0x1A; // now as a variable
    void serial_init(void);
    void serial(unsigned char);
    void puts(unsigned char *p );
    void delay(void);

    void main()
    {
    serial_init();
    /* Not running this as its not in the Arduino code */
    #ifdef ECHO_CMD
      puts(command);
      serial(0x0D); // /r after each command
      delay(); // delay of approx 1 sec

      puts(echo);
      serial(0x0D);
      delay();
    #endif

    puts(msgConfig);  // SIM900.print("AT+CMGF=1\r");
    serial(0x0D);
    delay();

    puts(number); // SIM900.println("AT + CMGS = \"+12128675309\"");
    serial(0x0D);
    serial(0x0A);
    delay();

    puts(message); //SIM900.println("Hello, world. This is a text message from an Arduino Uno.");
    serial(0x0D);
    serial(0x0A);
    delay();

    serial(CTRLZ); // SIM900.println((char)26);
    serial(0x0D);
    serial(0x0A);
    delay();

    serial(0x0D); // SIM900.println();
    serial(0x0A);
    while(1);

    }
    void serial_init(void)
    {

    TMOD=0x20; //timer 1, mode 2(8-bit autoreload) to set baud rate
    TH1=0xFD; //-3 to TH1 for 9600 baud rate
    SCON=0x50; // 8 bit txion, 1 start 1 stop bit, REN enable for both txfr and rxve
    TR1=1; // start timer
    }

    void puts(char *p)
    {
    char *temp = p; /*temp pointer so that the actual pointer is not displaced */
    while(*temp != 0x00)
    {
    serial(*temp);
    temp++;
    }
    }

    void serial(unsigned char x)
    {

    SBUF=x;
    while(TI==0);
    TI=0;

    }
    void delay(void) // delay for approx 1 sec
    {
    int i;
    TMOD=0x01; // timer 0 in mode 1
    for(i=0;i<142;i++)
    {
    TL0=0x00; // starting value from 0
    TH0=0x00;
    TR0=1; // sart timer
    while(TF0==0); // polling TF flag for high
    TR0=0; // stop timer
    TF0=0; // clear flag TF0
    //}
    }
    }
     
  21. Bob Parihar

    Bob Parihar Member

    Joined:
    Oct 8, 2014
    Messages:
    87
    Likes:
    0


    not getting what you have edited..
    after each command i added LF Characters

    serial(0x0D); // /r
    serial(0x0A); // /n
     

Share This Page