[PIC] Reading sms using gsm modem with pic microcontroller

Status
Not open for further replies.

manuva

Newbie level 5
Joined
Jun 16, 2014
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
87
Hi everyone in this forum.Again I come searching for help.On my first post I got direction on how I can write program to interface gsm modem to read and send sms. Now from different tutorials I learn different things in this forum and I wrote the following program using MikroC pro PIC.


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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
unsigned char i=0,yesgot=0,msg[100];
unsigned int storedloc=0,gotit;
unsigned char ph[14];
unsigned char *ph1[]="+255657741768";
unsigned char rx_incoming();
unsigned char rx_incoming()
 {
  while(PIR1.RCIF==0);
  return RCREG;
 }
 
void main()
{
  trisc.f6=0;trisc.f7=1;
  trisd=0x00;portd=0x00;
  trisb.f4=0;trisb.f5=0; portb=0;
  uart1_init(9600);delay_ms(100);
  RCSTA.SPEN=1;RCSTA.CREN=1;
  delay_ms(50);
  uart1_write_text("ate0");
  uart1_write(0x0d);
  delay_ms(40);
  uart1_write_text("at+cmgf=1");uart1_write(0x0d);
  loop1:
  storedloc=0;
   while(storedloc==0)
  {
   gotit= rx_incoming();
   if(gotit=='+')
   {
 
    gotit= rx_incoming();
     if(gotit=='C')
      {
       gotit=rx_incoming();
        if(gotit=='M')
         {
 
          gotit=rx_incoming();
          if(gotit=='T')
           {
            gotit=rx_incoming();
           if(gotit=='I')
           {
 
            gotit=rx_incoming();
            if(gotit==':')
            {
 
             gotit=rx_incoming();
             while(gotit!=',')
             gotit=rx_incoming();
             storedloc=rx_incoming();
            }
           }
         }
      }
    }
  }
 }
  delay_ms(100);
  uart1_write_text("at+cmgr=");
  uart1_write(storedloc);
  uart1_write(0xd);
 
while(yesgot==0)
{
 
 gotit=rx_incoming();
 if(gotit=='+')
 {
 
  gotit=rx_incoming();
   if(gotit=='C')
   {
 
    gotit=rx_incoming();
     if(gotit=='M')
      {
 
       gotit=rx_incoming();
        if(gotit=='G')
         {
           gotit=rx_incoming();
           if(gotit=='R')
           {
              gotit=rx_incoming();
              if(gotit==':')
              {
               gotit=rx_incoming();
               while(gotit!=','){gotit=rx_incoming();};
               if(gotit='"')
                {
                 for(i=0;i<13;i++){gotit=ph[i]=rx_incoming();}
                }
               while(gotit!=0x0a){gotit=rx_incoming();}
               for(i=0;gotit!=0x0d;i++){gotit=msg[i]=rx_incoming();}
               uart1_write_text("at+cmgd=");
               uart1_write(storedloc);uart1_write(0x0D);
               yesgot=1;
              }
             }
            }
           }
         }
       }
     }
 
 
 
 if(strcmp(ph,ph1)==0)
 {
 if(msg[0]=='O')
  {
   if(msg[1]=='N')
   {
     if(msg[2]==32)
     {
      portb.f5=1;portb.f4=0;
     }
   }
  }
  if(msg[0]=='O')
  {
   if(msg[1]=='F')
   {
    if(msg[2]=='F')
    {
     if(msg[3]==32)
     {
     portb.f4=1;
     portb.f5=0;
     }
   }
  }
 }
 }
 
 
else if(strcmp(ph,ph1)!=0)
{
portd.f7=0;delay_ms(500);
uart1_write_text("at+cmgf=1");
uart1_write(0x0D);
delay_ms(100);
uart1_write("at+cmgs=");
delay_ms(100);
uart1_write(0x22);
delay_ms(100);
uart1_write(ph);
delay_ms(100);
uart1_write(0x22);
uart1_write(0x0D);
delay_ms(500);
uart1_write_text("Your not Authorized.");
uart1_write(0x0D);delay_ms(500);
uart1_write_text("For help contact :");
uart1_write(0x0D);delay_ms(500);
uart1_write_text("+255657741768: J.E.MASSAM.");
uart1_write(0x0D);delay_ms(1000);
uart1_write(0x1A);delay_ms(500);
 }
  yesgot=0;storedloc=0;gotit=0;
  delay_ms(1000);
  goto loop1;
 
}



but when I interface with physical hardware by using compim does not perform what I programmed please any one to correct me or give another alternative to this.
I appreciate your comments.................................
Welcome.
 

Attachments

  • circuit.png
    146.5 KB · Views: 230

@Manuva,

1. What is the GSM modem that you are using?
2. Are you sure that the baud rate is 9600??? How did you confirm it??
3. In order to get +CMTI notification you have to set the gsm module to get this notification using AT+CNMI command.
4. Instead of if statement everywhere, user strstr function to check the string that you get from the UART..
 

@manuva
in addition to the suggestions by kgshankar1980

please verify few other things also
1. try to communicate your GSM modem using PC ( hyperterminal or similar software) to check it is functioning properly
2. check whether your modem required full HW control ( if yes you can use null modem DB9 configuration at modem side)
3. check the voltage level your modem is working on ( you may need to use MAX232 if the modem is using standard electrical specifications)
 

ok thanks for contribution , but I'm doing this by using proteus simulation with only modem a physical hardware.So i think no need to use MAX232.And I going to use AT+CNMI to enable for new sms notification and strstr instead of if statement. And I will inform where I reached.
 

In case of baud rate I'm sure because firstly it was not working and I flashed it with baud rate of 9600.
 

@Manuva,

Generally all the modems comes with a default baud rate at 115200,8,n,1...But of us not aware of it and try to reflash the 9600 br..
 

the modem works fine because when I load program to microcontroller for only sending sms I got the following results.

after few changes on my above posted program the following results I get.

It seems like there is a problem with AT commands to sent to the modem. Please help what to do.
 

what is your objective??
what you want to achieve?

and what new AT commands you have included in your code?

as your thread says SMS interface, and in last point you said that code for only sending SMS works fine.
so now what are you trying to add?
 

actually what I need is to write a program to check for new incoming sms ,read it ,compare with my specified string and switch on LED in portb and send feedback to mobile phone.
 

1. You always check in Serial interrupt loop +CMTI coming r not. when +CMTI will come, U Got a new message. Then Set a Msg_Recv_flag=1.

2.Then send message read command. Then response will come +CMGR:........... etc

3.when +CMGR will come, U read a new message.Then Set a Msg_Read_flag=1.

4.New message also check example- ON coming or not,
 
Reactions: manuva

    manuva

    Points: 2
    Helpful Answer Positive Rating
@rparthiban69
sorry see my program above in the post and please correct me where I mistaken.
Your welcome....................
 

You write Serial Receive interrupt and Receive datas store in a array. Then Strings compare.
 
Reactions: manuva

    manuva

    Points: 2
    Helpful Answer Positive Rating
Hi every one , thanks very for your suggestions in my post. And now I'm succesful done to write the code and simulated on proteus by using sim900a module interfaced in comport of my computer.
I into implementation on hardware , I made the circuit and I tested it works fine in case of receiving sms and performing the specified task. But now I wonder why it can't send back to me status sms while in simulation it does. Thanks........
I appreciate your reply......
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…