Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Problem on interfacing gsm to pic16f877a to turn on/off led through sms

Status
Not open for further replies.

Emmanuel1992

Junior Member level 2
Joined
Nov 24, 2016
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
253
Dear member any one who know how to interface gsm modem with pic16f877a to turn on/off LED because I have trying to program a code for this stuff by using mikroC compiler but i doesn't work as am expected to work.

My code it's below.


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
sbit LED at RC1_bit;
 char lock[] = "LED ON";
char Read;
void main() {
 
 UART1_Init(9600);
 delay_ms(1);
   UART1_Write_Text("AT");
 UART1_Write(0x0D);
 Delay_Ms(100);
 UART1_Write_Text("ATE0");
 UART1_Write(0x0D);
 Delay_Ms(100);
 UART1_Write_Text("AT+CMGF=1");
 UART1_Write(0x0D);
 Delay_Ms(100);
   UART1_Write_Text("AT+CMGS=717677776");
UART1_WRITE(0X0D); 
 Delay_Ms(100);
 
 TRISA=0Xff;
 TRISC.F1=0;
 
 
 
while(Read!= '\0' || Read != '\n' )
        {
        unsigned short int Index=0;
        char Byte[20];
        Byte[Index] = Read;
        Index++;
        }
 
 
while(1){
 
 
 
if(UART1_Data_Ready()==1)
{
 Read = UART1_Read();
if(strcmp(Read,"LED ON")==0)
{ delay_ms(1);
LED==1 ; //lock on.
delay_ms(2000);
UART1_WRITE_TEXT("AT+CMGS=");
delay_ms(1);
UART1_WRITE(0X22); //"
delay_ms(1);
UART1_WRITE_TEXT("0123456789"); //MY NUMBER
delay_ms(1);
UART1_WRITE(0X22); //"
UART1_WRITE(0X0D); //<CR> means enter
delay_ms(1);
UART1_WRITE_TEXT("LED IS ON");
UART1_WRITE(0X0D); //<CR> means enter
delay_ms(1);
UART1_WRITE(26);
delay_ms(1);
UART1_WRITE(0X0D); //<CR> means enter
delay_ms(1);
}
}
}



Any one who can correct my code please because am suppose to submit it soon.
 
Last edited by a moderator:

Which GSM modem ? SIM900 ? Where is AT+CNMI=.... command for new SMS notification ?

Code:
UART1Read()

returns only a byte.

What is the Crystal frequency ?

I can give you the code but not for 16F877A because it has less RAM and mikroC PRO PIC doesn't generate bank switching code. To receive AT+CMGR response you need a big array of say 200 bytes. RAM is only 3xx bytes. If you can use PIC18F46K22 then I will give you full working code.
 
Last edited:

Ok Can you give me that code please because am suppose to finish it soon as possible please give that code.
 

Is it ok if I post the code here because your instructor or project guide may see it. If it is ok then I will post the code here. I ported the code to PIC16F877A but not sure if it works or not because the uart buffer size is small.
 

Ok, you can post it here no problem.
 

Thank you very much let's me try it then i will give you a feedback once I tested it.
 

Here is the project. Test it in hardware and reply.

You have to send SMS like

LED ON

LED OFF

it will then turn ON/OFF the Led at RD0.

If incorrect SMS is sent then it will SMS "Invalid Command Received" to number mentioned in code.

Change mobile number here

Code:
const char atCommand9[] = "AT+CMGS=\"0000000000\"\r";

and then compile the code and use it.

- - - Updated - - -

Here is the code.


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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#define ON   1
#define OFF  0
 
#define HIGH 1
#define LOW  0
 
#define TRUE 1
#define FALSE 0
 
// GSM module connections
sbit gsmReset at RD7_bit;
sbit gsmReset_Direction at TRISD7_bit;
// End of GSM module connections
 
sbit LED at RD0_bit;
 
char gsmBuffer[80];
char buffer1[30];
char sms[30];
char smsIndex[4];
 
unsigned char myFlags = 0;
 
char gsmAttempt = 0;
unsigned int gsmBufferIndex = 0;
 
sbit sendSmsFlag at myFlags.B0;
 
//GSM Constant Strings
const char atCommand1[] = "AT\r";
const char atCommand2[] = "ATE0\r";
const char atCommand3[] = "ATE1\r";
const char atCommand4[] = "AT+IPR=9600\r";
const char atCommand5[] = "AT+CMGF=1\r";
const char atCommand6[] = "AT+CPMS="SM","SM","SM"\r";
const char atCommand7[] = "AT+CNMI=2,1\r";
const char atCommand8[] = "AT+CMGR=";
const char atCommand9[] = "AT+CMGS="0000000000"\r";
const char atCommandA[] = "AT+CMGD=1,4\r";
 
const char atResponse1[] = "\r\nOK\r\n";
const char atResponse2[] = "ERROR";
const char atResponse3[] = "+CMTI: "SM",";
const char atResponse4[] = "> ";
const char atResponse5[] = "RING";
const char atResponse6[] = "NO CARRIER";
const char atResponse7[] = "NO ANSWER";
const char atResponse8[] = "+CFUN: 1";
const char atResponse9[] = "+CPIN: READY";
 
const char msg1[] = "LED ON";
const char msg2[] = "LED OFF";
 
const char sms1[] = "LED is ON";
const char sms2[] = "LED is OFF";
const char sms3[] = "Invalid Command Received";
 
//Function Prototypes
void DelayXSec(unsigned long int sec);
char *CopyConst2Ram(char *dest, const char *src);
void GSM_RESET();
void Extract_SMS(char *s1, char *s2);
void Get_SMS_gsmBufferIndex(char *s1, char *s2);
void Get_GSM_Time(char *s1, char *s2);
void Get_Code(char *s1, char *s2);
void GSM_Send_Const_Command(char *s1, char *s2, const char *s3, const char *s4, unsigned int *idx, char *gsmAttempt, char clr);
void GSM_Get_SMS(char *s1, char *s2, const char *s3, const char *s4, char *s5, unsigned int *idx, char *gsmAttempt, char *s6, char clr);
void Send_SMS(char *s1, char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7, char *s8, unsigned int *idx, char *gsmAttempt, char clr);
 
void interrupt() {
    
    if((RCIE_bit) && (RCIF_bit)) {
        if(OERR_bit) {
              OERR_bit = 0;
              CREN_bit = 0;
              CREN_bit = 1;
        }
 
        gsmBuffer[gsmBufferIndex++] = UART1_Read();
        gsmBuffer[gsmBufferIndex] = '\0';
 
        RCIF_bit = 0;
    }     
}
 
char *CopyConst2Ram(char *dest, const char *src){
    char *d;
    
    asm clrwdt
    d = dest;
    
    for(;*dest++ = *src++;)asm clrwdt;
 
    return d;
}
 
void DelayXSec(unsigned long int sec) {
    while(sec != 0) {
        Delay_ms(1000);
        asm clrwdt
        --sec;
    }
}
 
void GSM_RESET() {
    asm clrwdt
    gsmReset = 1;    
    Delay_ms(20);
    gsmReset = 0;    
}
 
void Extract_SMS(char *s1, char *s2) {
    unsigned int i = 0;
    
    asm clrwdt
    
    while(*s1) {
        if(*s1 == '\n')
            ++i;
        asm clrwdt
        *s1++;
 
        if(i == 2)break;
    }
    
    asm clrwdt
    
    while((*s1) && (*s1 != '\r')) {
            *s2++ = *s1++;
            asm clrwdt
    }
 
    *s2 = '\0';                         
    asm clrwdt
}
 
void Get_SMS_Index(char *s1, char *s2) {
 
    while(*s1 != 'C') {
           *s1++;
           asm clrwdt
    }
 
    while(*s1 != 'M') {
           *s1++;
           asm clrwdt
    }
 
    while(*s1 != 'T') {
           *s1++;
           asm clrwdt
    }
 
    while(*s1 != 'I') {
           *s1++;
           asm clrwdt
    }
 
    while(*s1 != ',') {
           *s1++;
           asm clrwdt
    }
 
    *s1++;
    while(*s1 != '\r') {
        *s2++ = *s1++;
        asm clrwdt
    }
 
    *s2 = '\0';
    asm clrwdt
}
 
void GSM_Send_Const_Command(char *s1, char *s2, const char *s3, const char *s4, unsigned int *idx, char *gsmAttempt, char clr) {
    asm clrwdt
    
    while(strstr(s1, CopyConst2Ram(s2, s3)) == 0) {
        UART1_Write_Text(CopyConst2Ram(s2, s4));        
        DelayXSec(2);        
        if((*gsmAttempt)++ == 3) {
            GSM_RESET();            
            memset(gsmBuffer, '\0', sizeof(gsmBuffer));
            (*gsmAttempt) = 0;
            (*idx) = 0;
        }
    }
 
    if(clr)memset(gsmBuffer, '\0', sizeof(gsmBuffer));    
    (*gsmAttempt) = 0;
    (*idx) = 0;
}
 
void GSM_Get_SMS(char *s1, char *s2, const char *s3, const char *s4, char *s5, unsigned int *idx, char *gsmAttempt, char *s6, char clr) {
    asm clrwdt
    
    while(strstr(s1, CopyConst2Ram(s2, s3)) == 0) {
        CopyConst2Ram(s2, s4);
        strcat(s2, s5);
        strcat(s2, "\r");
        UART1_Write_Text(s2);
        DelayXSec(2);
        
        if((*gsmAttempt)++ == 3) {
            GSM_RESET() ;            
            (*gsmAttempt) = 0;
            (*idx) = 0;
            memset(gsmBuffer, '\0', sizeof(gsmBuffer));
        }
    }
 
    (*idx) = 0;
 
    Extract_SMS(s1, s6);
    
    if(clr)memset(gsmBuffer, '\0', sizeof(gsmBuffer));
    asm clrwdt
}
 
void Send_SMS(char *s1, char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7, char *s8, unsigned int *idx, char *gsmAttempt, char clr) {
    asm clrwdt
    
    while(strstr(s1, CopyConst2Ram(s2, s3)) == 0) {
        asm clrwdt
        GSM_Send_Const_Command(s1, s2, s3, s5, idx, gsmAttempt, 1);
        GSM_Send_Const_Command(s1, s2, s3, s6, idx, gsmAttempt, 1);
        GSM_Send_Const_Command(s1, s2, s4, s7, idx, gsmAttempt, 1);
        
        UART1_Write_Text(s8);
        Delay_ms(500);
        UART1_Write(0x1A);
        
        DelayXSec(6);
    }
 
    memset(gsmBuffer, '\0', sizeof(gsmBuffer));
    (*idx) = 0;
    asm clrwdt
}
 
void main() {
 
    asm clrwdt
    OPTION_REG = 0x87;
    
    CMCON = 0x07;
    ADCON1 = 0x87;
 
    TRISA = 0x00;
    TRISB = 0x00;
    TRISC = 0x80;
    TRISD = 0x00;
    TRISE = 0x00;
    
    PORTA = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
    PORTD = 0x00;
    PORTE = 0x00;
        
    UART1_Init(9600);
    Delay_ms(100);
    
    RCIF_bit = 0;
    RCIE_bit = 1;
    
    PEIE_bit = 1;
    GIE_bit = 1;    
 
    GSM_Send_Const_Command(gsmBuffer, buffer1, atResponse1, atCommand1, &gsmBufferIndex, &gsmAttempt, 1);
    GSM_Send_Const_Command(gsmBuffer, buffer1, atResponse1, atCommand4, &gsmBufferIndex, &gsmAttempt, 1);
    GSM_Send_Const_Command(gsmBuffer, buffer1, atResponse1, atCommand5, &gsmBufferIndex, &gsmAttempt, 1);
    GSM_Send_Const_Command(gsmBuffer, buffer1, atResponse1, atCommand6, &gsmBufferIndex, &gsmAttempt, 1);
    GSM_Send_Const_Command(gsmBuffer, buffer1, atResponse1, atCommand7, &gsmBufferIndex, &gsmAttempt, 1);
    GSM_Send_Const_Command(gsmBuffer, buffer1, atResponse1, atCommandA, &gsmBufferIndex, &gsmAttempt, 1);    
 
    sendSmsFlag = 0;
    
    while(1) {
 
        asm clrwdt
                
        if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse3))) {   //If new SMS  received
            DelayXSec(3);          //Wait for a while - needed piece of code else 
                                   //when you read SMS index you will see missing SMS index
            Get_SMS_Index(gsmBuffer, smsIndex);            
            GSM_Send_Const_Command(gsmBuffer, buffer1, atResponse1, atCommand1, &gsmBufferIndex, &gsmAttempt, 1);             
            GSM_Send_Const_Command(gsmBuffer, buffer1, atResponse1, atCommand5, &gsmBufferIndex, &gsmAttempt, 1);
            GSM_Get_SMS(gsmBuffer, buffer1, atResponse1, atCommand8, smsIndex, &gsmBufferIndex, &gsmAttempt, sms, 1);
            
            if(strcmp(sms, CopyConst2Ram(buffer1, msg1)) == 0) {  //If received message is "LED ON"
                 LED = ON;                       //Turn ON LED
                 CopyConst2Ram(sms, sms1);       //Copy sms1 to ram string
                 sendSmsFlag = 1;
            }
            else if(strcmp(sms, CopyConst2Ram(buffer1, msg2)) == 0) { //If received message is "LED OFF"
                 LED = OFF;                      //Turn OFF LED
                 CopyConst2Ram(sms, sms2);       //Copy sms2 to ram string
                 sendSmsFlag = 1;
            }
            else {
                 CopyConst2Ram(sms, sms3);       //Copy sms2 to ram string
                 sendSmsFlag = 1;                                
            }            
            
            if(sendSmsFlag) {
                //Send SMS
                Send_SMS(gsmBuffer, buffer1, atResponse1, atResponse4, atCommand1, atCommand5, atCommand9, sms, &gsmBufferIndex, &gsmAttempt, 1);
                
                sendSmsFlag = 0;
            }
            
            //Delete all SMS from SIM     
            GSM_Send_Const_Command(gsmBuffer, buffer1, atResponse1, atCommandA, &gsmBufferIndex, &gsmAttempt, 1);
            gsmBufferIndex = 0;   //Clear Uart buffer index
            memset(sms, '\0', sizeof(sms));   //Clear sms buffer     
        }        
    }
}

 

Attachments

  • GSM Based Device Control.rar
    36.6 KB · Views: 97

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top