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

GSM Modem Interfacing with PIC Microcontroller

Discussion in 'Microcontrollers' started by Sathiesh Kumar, Jan 31, 2010.

  1. haxan

    haxan New Member

    Joined:
    Nov 17, 2009
    Messages:
    387
    Likes:
    0
    Can you send the code you updated. Some one who might have same problem coming after you will be benefited or at least mention what the issue was with your code. (i believe it was the LCD issue being updated before Rx commands missing a few bytes of data on the serial port)

    As for your problem regarding SMS storage, i think storing in array is what i would do. Will keep on pushing in array unless last (ending) byte is received and then will parse the message stripping data which is required.

    For microcontroller based projects using GSM, i believe you should have a maximum limit of bytes in SMS. Exceeding the limit, your array will be ruined so you need to count total bytes received and if the SMS is too long, you will need to take some measures (maybe start filling a second array, or send SMS back saying Message too long).
     
  2. Sathiesh Kumar

    Sathiesh Kumar New Member

    Joined:
    Oct 13, 2009
    Messages:
    58
    Likes:
    1
    Location:
    Chennai
    Check the RX and TX pin of GSM Modem.. its different from PC Hyperterminal... when i changed those things.. i got output... i will send the code shortly... i am working on receiving a message in PIC .. Once it gets completed... i will send the entire coding....


    Sathiesh Kumar.V
     
  3. upand_at_them

    upand_at_them Member

    Joined:
    Apr 23, 2005
    Messages:
    609
    Likes:
    8
    Location:
    Pennsylvania, U.S.
    Are you able to connect the PC and the PIC to the modem's TX pin at the same time to see if the modem really does send a response?
     
  4. dave

    Dave New Member

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


     
  5. Wilksey

    Wilksey Member

    Joined:
    Aug 16, 2009
    Messages:
    553
    Likes:
    20
    Location:
    Somerset, UK

    RX goes to TX and vice versa. Unless you have a null modem cable.

    Wilksey
     
  6. Sathiesh Kumar

    Sathiesh Kumar New Member

    Joined:
    Oct 13, 2009
    Messages:
    58
    Likes:
    1
    Location:
    Chennai
    Hi...
    i still working on reading a sms from gsm modem in pic microcontroller... i cant able to read the message... whats the logic behind it... i can able to send sms from pic microcontroller via gsm modem.. there is no problem in it... reading a message is a problem... can any one help me....


    Sathiesh Kumar.V
     
  7. haxan

    haxan New Member

    Joined:
    Nov 17, 2009
    Messages:
    387
    Likes:
    0
    I think you should first start off from Hyperterminal.

    It would be much easier to sort out all issue from PC and then once you have accomplished that, you can write the code for PIC .

    What i remember for reading SMS was the commands

    Code (text):

    AT+CMGF=1
    AT+CNMI=2,3,2,0,0
    AT+CMGR=0
     
    The AT+CMGF=1 sets the format to text mode
    Not sure about CNMI
    AT+CMGR=0 initiates the SMS read for message at index 0 (0 meaning mostly latest message [read/unread]). After the AT+CMGR is sent, the GSM module should start sending the SMS byte by byte in through the serial link. So after successfully sending AT+CMGR=0, you should wait for data bytes on hyperterminal and in PIC, your UART should be in read mode :)
     
  8. habibtel@gmail.com

    habibtel@gmail.com New Member

    Joined:
    Feb 16, 2010
    Messages:
    4
    Likes:
    0
    Help.

    Hi,

    i saw your useful posts there.
    i am working on GSM based prepiad electric meter.

    my part is make two way communication b/w GSM modem and PIC.

    After a regular interval PIC will take reading from meter and will send to server through modem.

    server will send sms to Modem for Activation/deactivation etc.


    NO LCD
    NO KeyPad is used.


    Plz helpe, i am totally blind.

    i don't know how to start??????


    plz tell me structure to do this.....

    how AT commands will be used in PIC code????????

    Help plz


    Alyan
     
  9. habibtel@gmail.com

    habibtel@gmail.com New Member

    Joined:
    Feb 16, 2010
    Messages:
    4
    Likes:
    0
    Help.

    Hi,

    i saw your useful posts there.
    i am working on GSM based prepiad electric meter.

    my part is make two way communication b/w GSM modem and PIC.

    After a regular interval PIC will take reading from meter and will send to server through modem.

    server will send sms to Modem for Activation/deactivation etc.


    NO LCD
    NO KeyPad is used.


    Plz helpe, i am totally blind.
     
  10. haxan

    haxan New Member

    Joined:
    Nov 17, 2009
    Messages:
    387
    Likes:
    0
    habibtel you can start a new thread :)

    Have you done any PIC based coding previously or is this your first time?

    What PIC are you using and also the compiler name.

    How is the PIC going to read the value from meter?
     
  11. habibtel@gmail.com

    habibtel@gmail.com New Member

    Joined:
    Feb 16, 2010
    Messages:
    4
    Likes:
    0
    Haxan.

    i appreciate your swift reply.

    I am totally newbie.............

    have done no coding in past.

    we are using mikroC.

    PIC is 16f87(U can suggest also)

    a sensor will be trigged on each meter disc revolution and will send interpt to to the PIC .

    after doing calculation PIC will send status to server trough modem.

    Thanx a lot.......


    Alyan.
    Pakistan
     
  12. haxan

    haxan New Member

    Joined:
    Nov 17, 2009
    Messages:
    387
    Likes:
    0
    habibtel please kindly start a new thread. It seems you will need quite some help in order to start.

    when you say modem, you mean GSM modem right?

    you can attach a serial based phone aswell.

    the PIC16F87 is ok i guess but i personally have no experience with mikroC compiler.

    Which sensor are you using to detect rotation in disk?

    Please kindly start a new thread and mention all the stuff said in this post.
     
    Last edited: Feb 16, 2010
  13. habibtel@gmail.com

    habibtel@gmail.com New Member

    Joined:
    Feb 16, 2010
    Messages:
    4
    Likes:
    0
    Haxan.

    yes its GSM modem,
    its a U shaped IR sensor.


    i hav no problem with mikroC.
    the real problem is i don't understand the structure to use AT commands.
     
  14. Sathiesh Kumar

    Sathiesh Kumar New Member

    Joined:
    Oct 13, 2009
    Messages:
    58
    Likes:
    1
    Location:
    Chennai
    Hi.. i am attaching a code to read a sms using PIC microcontroller... but it does not produce any output ... i am just receiving a blank message... the main motto is to read an sms from sim and send it to a destination mobile number..

    Please check the code... and say whats wrong in that...

    The compiler is MicroC
    GSM Modem:Wavecom

    Code (text):
    void sms_send();
    void sms_del();
    void sms_read();
    char sms[11];
    char output[65];
    unsigned char* MSG4 = "AT+CNMI=2,1,0,0,0";
    void main() {
         UART1_Init(9600);              // Initialize UART module at 9600 bps
         Delay_ms(100);                 // Wait for UART module to stabilize
        // sms_del();
         sms_read();
     }
    void sms_send()
    {
    int i;
    /*UART1_Write_Text("AT\n\r");
    delay_ms(500);
    UART1_Write_Text("AT+CMGF=1\n\r");
    delay_ms(500); */
    UART1_Write_Text("AT+CMGS=");
    UART1_Write(0x22);
    UART1_Write_Text("+919444721638");
    UART1_Write(0x22);
    UART1_Write_Text("\n\r");
    delay_ms(500);
    for(i=0;i<64;i++)
    {
    if(output[i]=='\0') break;//ask for null character of sms vector when find it go out of for() loop
    UART1_Write(sms[i]);
    }
    delay_ms(500);
    UART1_Write(26);// send ctrl + Z
    return;
    }
    void sms_del()
    {
    UART1_Write_Text("AT+CMGD=1\n\r");
    delay_ms(500);
    }


    void sms_read()
    {
    int i;
    //char sms[11];
    UART1_Write_Text("AT\n\r");
    delay_ms(500);
    UART1_Write_Text(MSG4);    //cpms
    Delay_ms(1000);
    UART1_Write(0X0D);        // ENTER
    Delay_ms(1000);
    UART1_Write_Text("AT+CMGF=1\n\r");
    delay_ms(500);
    UART1_Write_Text("AT+CMGR=7\n\r");
    //delay_ms(500);
    while (UART1_Data_Ready() != 1);
    UART1_Read_Text(output, "OK", 65);    // reads text until 'OK' is found
    sms_send();
    //return;
    }
    Help me to retrieve the result...

    Sathiesh Kumar.V
     
  15. haxan

    haxan New Member

    Joined:
    Nov 17, 2009
    Messages:
    387
    Likes:
    0
    Have you tested it by attaching your PIC on PC's hyperterminal ?

    Also have you tested reading SMS successfully from PC.

    Is there an SMS at index 7?

    Also does the AT+CMGR responds by "OK" once the message has been sent on serial port?

    EDIT:

    If you call send_sms() function, and specify some output predefined, is it going out fine? If it is, then the output from sms_read is not populating the output array correctly.

    I have not worked with MicroC compiler so cannot say about UART1_Read_Text function.

    Can you use the AT commands using hyperterminal to read the SMS and paste the output here, that would be much helpful :)
     
    Last edited: Feb 17, 2010
  16. Sathiesh Kumar

    Sathiesh Kumar New Member

    Joined:
    Oct 13, 2009
    Messages:
    58
    Likes:
    1
    Location:
    Chennai
    Hi

    i have not checked my code by connecting with hyperterminal... i will do it soon and reply you...
     
  17. Wilksey

    Wilksey Member

    Joined:
    Aug 16, 2009
    Messages:
    553
    Likes:
    20
    Location:
    Somerset, UK
    Why have you set the character read interval to 65?

    Also, this is a blocking call, so if you get "ERROR" then it will not return true.

    Does the code get past your "while data ready" loop?

    You can set an output high when it gets past and put a meter across to see if it has gotten this far.
    Kind of like a "debug" feature I use when developing.

    Does the modem care for the sequence of the carriage return line feed? I noticed on one write you only send carriage return, and you are putting line feeds before carriage return, the sequence is generally CRLF carriage return line feed, it doesn't really matter as long as the modem isn't picky about this.

    Also check that the output is terminated, otherwise this can yeild unexpected results.

    Wilksey
     
    Last edited: Feb 17, 2010
  18. Sathiesh Kumar

    Sathiesh Kumar New Member

    Joined:
    Oct 13, 2009
    Messages:
    58
    Likes:
    1
    Location:
    Chennai
    Hi iam attaching a code which works perfectly when i interface PIC Microcontroller with PC hyperterminal to send and receive messages... But it doesnt give any output when i connect PIC with GSM Modem... The Hardware connections is also right... when i try to send sms from PIC it works perfectly... the problem in reading the message in Sim memory.. what might be the problem... check this code ....

    Code (text):

    void sms_read();
    void sms_send();
    char output[70];
    void main() {
         UART1_Init(9600);              // Initialize UART module at 9600 bps
         Delay_ms(100);                 // Wait for UART module to stabilize
         sms_read();
    }
    void sms_read()
    {
    UART1_Write_Text("AT\n\r");
    delay_ms(500);
    UART1_Write_Text("AT+CMGF=1\n\r");
    delay_ms(500);
    UART1_Write_Text("AT+CMGR=1\n\r");
    delay_ms(500);
    while (1) {
      if (UART1_Data_Ready() == 1) {          // if data is received
        UART1_Read_Text(output, "OK", 80);    // reads text until 'OK' is found
        /*UART1_Write_Text(output);             // sends back text
        delay_ms(500);
        UART1_Write_Text("\n");
        delay_ms(500);  */
        sms_send();
     }
    }
    }

    void sms_send()
    {
    int i;
    /*UART1_Write_Text("AT\n\r");
    delay_ms(500);
    UART1_Write_Text("AT+CMGF=1\n\r");
    delay_ms(500); */
    UART1_Write_Text("AT+CMGS=");
    UART1_Write(0x22);
    UART1_Write_Text("+919444721638");
    UART1_Write(0x22);
    UART1_Write_Text("\n\r");
    delay_ms(500);
    UART1_Write_Text(output);
    UART1_Write(26);// send ctrl + Z

    }
     
  19. haxan

    haxan New Member

    Joined:
    Nov 17, 2009
    Messages:
    387
    Likes:
    0
    Is the code going beyond the line
    Code (text):
    UART1_Read_Text(output, "OK", 80);    // reads text until 'OK' is found
    I think there is some fault in this statement.

    Have you tested reading the GSM SMS from hyperterminal ? Does it give out 'OK' after reading all the content ?
     
  20. Sathiesh Kumar

    Sathiesh Kumar New Member

    Joined:
    Oct 13, 2009
    Messages:
    58
    Likes:
    1
    Location:
    Chennai

    When i connect GSM Modem with PC Hyperterminal... and i send AT+CMGR=1
    The modem responds by giving...

    +CMGR:"REC READ","+9444721638",,"09/11/12,22:15:32+22" Hi How are you
    OK

    This is the message format which i see in hyperterminal.. "Hi How are you " is the message which is at location 1

    Sathiesh Kumar.V
     
  21. haxan

    haxan New Member

    Joined:
    Nov 17, 2009
    Messages:
    387
    Likes:
    0
    I think you should read the data character by character (byte by byte) and populate it into the output variable using a loop (for now). Give it a test run for example, try to read 60 bytes and then send them to output using your send_sms() function.
     
    Last edited: Feb 19, 2010

Share This Page