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.

Device control using GSM

Status
Not open for further replies.
Advanced Member level 6
Joined
Dec 4, 2012
Messages
4,280
Helped
822
Reputation
1,654
Reaction score
791
Trophy points
1,393
Location
Bangalore, India
Visit site
Activity points
0
Device control using GSM. Not tested in hardware. Test and reply.

Sens SMS like *xxxxxxxxxx*# without double quotes. Number will be stored in EEPROM. reset the micro. Micro reads number from EEPROM. Send commands like...

*PUMP ON*

*LIGHT ON*

*PUMP OFF*

*LIGHT OFF*

Use LED instead of relay for testing. Test and reply.
 

Attachments

  • GSM Control.rar
    23.2 KB · Views: 100

do you want someone to test the code for you?
don't you have a GSM module?

from the GSM command,i guess you control the pump (motor) and light.
 

I have GSM modem but it is not sending SMS. I want someone to test it for me and also for themselves. If it works then I will post the code so that others can modify and use if needed. Yes, it controls PUMP and Light. User can also send commands like...

*PUMP ON*
*LIGHT OFF*

It will be treated as one command and both device will be operated by one SMS. It will be useful for farmers.
 

I guess you would want to post some problem related C code instead of a Proteus project and a hex file.
 

There is no problem. I have problem with GSM model. It is not sending SMS. So, I can't test. I have ordered a new modem. If someone could test it will be helpful. The code is not complete yet.

I have disabled ISR. It is not used. 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
typedef unsigned char u8int_t;
typedef unsigned int u16int_t;
 
u8int_t sms[64] = "", cmd[4][10], number[12];
u8int_t initialized = 0, i = 0, k = 0, j = 0, m = 0, parse = 0, readSMS = 0, control = 0;
u16int_t time = 0;
u8int_t ATCMGS[10] = "AT+CMGS=";
 
//Timer0
//Prescaler 1:8; TMR0 Preload = 3035; Actual Interrupt Time : 250 ms
 
//Place/Copy this part in declaration section
void InitTimer0(){
      T0CON        = 0x82;
      TMR0H        = 0x0B;
      TMR0L        = 0xDB;
      GIE_bit = 1;
}
 
//Timer1
//Prescaler 1:8; TMR1 Preload = 3035; Actual Interrupt Time : 250 ms
 
//Place/Copy this part in declaration section
void InitTimer1(){
      T1CON = 0x31;
      TMR1IF_bit = 0;
      TMR1H = 0x0B;
      TMR1L = 0xDB;
      TMR1IE_bit = 1;
      INTCON = 0xC0;
}
 
 
void Interrupt() {
 
      if(RCIF_bit){
            if(readSMS == 2){
                  sms[i++] = RCREG;
                  sms[i] = '\0';
            }
            
            RCIF_bit = 0;
      }
      
      if (TMR0IF_bit){
            //Enter your code here
            if(++time == 32){
                   time = 0;
                   parse = 1;
                   TMR0IE_bit = 0;
            }
            
            TMR0IF_bit = 0;
            TMR0H = 0x0B;
            TMR0L = 0xDB;
 
      }
      
      if (TMR1IF_bit){
            //Enter your code here
            
            TMR1IF_bit = 0;
            TMR1H = 0x0B;
            TMR1L = 0xDB;
      }
}
 
void main() {
 
     TRISA = 0xff;
     TRISB = 0x00;
     TRISC = 0x80;
     TRISD = 0x00;
     
     UART1_Init(9600);
     Delay_ms(200);
     RCIE_bit = 1;
     
     InitTimer0();
     
     initialized = EEPROM_Read(0x00);
     
     while(1){
 
             if(initialized = 0xFF){
                   if(!readSMS){
                          while((sms[0] != 'A') && (sms[1] != 'T') && (sms[2] != '\r') && (sms[3] != '\n') && (sms[4] != '\r') && (sms[5] != '\n') && (sms[6] != 'O') && (sms[7] != 'K')){
                                  UART1_Write_Text("AT\r\n");
                                  UART1_Read_Text(sms, "OK", 10);
                          }
 
                          i = 0;
                          while(sms[i])sms[i++] = '\0';
                          i = 0;
                          while((sms[0] != 'A') && (sms[1] != 'T') && (sms[2] != '\r') && (sms[3] != '\n') && (sms[4] != '\r') && (sms[5] != '\n') && (sms[6] != 'O') && (sms[7] != 'K')){
                                  UART1_Write_Text("AT+CMGF=1\r\n");
                                  UART1_Read_Text(sms, "OK", 10);
                          }
 
 
                          i = 0;
                          readSMS = 1;
                          UART1_Write_Text("AT+CMGR=1\r\n");
                          UART1_Read_Text(sms, "*#", 20);
                          TMR0IE_bit = 1;
                   }
             }
             
             if(parse){
             
                    if(initialized == 0xFF){
                              while(sms[k++] != '*');
                              ++k;
                              j = 0;
                              while(sms[k] != '*')sms[j++] = sms[k++];
                              ++j;
                              sms[j] = '\0';
                              
                              for(j = 0; j < strlen(sms) - 1; j++){
                                      EEPROM_Write(0x02 + j, sms[j]);
                              }
                              
                                                         
                              while((sms[0] != 'A') && (sms[1] != 'T') && (sms[2] != '\r') && (sms[3] != '\n') && (sms[4] != '\r') && (sms[5] != '\n') && (sms[6] != 'O') && (sms[7] != 'K')){
                                      UART1_Write_Text("AT\r\n");
                                      UART1_Read_Text(sms, "OK", 10);
                              }
 
                              while((sms[0] != 'A') && (sms[1] != 'T') && (sms[2] != '\r') && (sms[3] != '\n') && (sms[4] != '\r') && (sms[5] != '\n') && (sms[6] != 'O') && (sms[7] != 'K')){
                                      UART1_Write_Text("AT+CMGF=1\r\n");
                                      UART1_Read_Text(sms, "OK", 10);
                              }
                              
                              i = 0;
                              for(j = 0; sms[j]; j++)
                                    EEPROM_Write(0x02 + j, sms[j]);
                              
                              UART1_Write_Text(ATCMGS);
                              UART1_Write(0x22);
                              
                              for(i = 0; sms[i]; i++) {
                                                UART1_Write(EEPROM_Read(0x02 + i));
                                                Delay_ms(20);
                              }
                              Delay_ms(100);
                              UART1_Write(0x22);
                              UART1_Write(13);
                              UART1_Write(10);
                              Delay_ms(2000);
 
                              UART_Write_Text("Number saved to memory...Send SMS *RESET*");
                              UART_Write(0x1A);
 
                              while((sms[0] != 'A') && (sms[1] != 'T') && (sms[2] != '\r') && (sms[3] != '\n') && (sms[4] != 'O') && (sms[5] != 'K')){
                                      UART1_Write_Text("AT\r\n");
                                      UART1_Read_Text(sms, "OK", 10);
                              }
 
                              i = 0;
                              while((sms[0] != 'A') && (sms[1] != 'T') && (sms[2] != '\r') && (sms[3] != '\n') && (sms[4] != 'O') && (sms[5] != 'K')){
                                      UART1_Write_Text("AT+CMGF=1\r\n");
                                      UART1_Read_Text(sms, "OK", 10);
                              }
                              readSMS = 1;
 
                              UART1_Write_Text("AT+CMGR=1\r\n");
                              TMR0IE_bit = 1;
                              Delay_ms(5000);
                              k = 0;
                              while(sms[k++] != '*');
                              ++k;
                              j = 0;
                              while(sms[k] != '*')sms[j++] = sms[k++];
                              ++j;
                              sms[j] = '\0';
                              if((sms[0] != 'R') && (sms[1] != 'E') && (sms[2] != 'S') && (sms[3] != 'E') && (sms[4] != 'T')) {
                                       EEPROM_Write(0x00, 1);
                                       //reset WDT
                              }
                              else {
                                    readSMS = 0;
                              }
                    }
                    else if(initialized == 1){
                              k = 0;
                              while((sms[k++] != '*') && (k < 64));
                              ++k;
                              j = 0;
                              m = 0;
                              while((sms[k] != '#') && (k < 64)){
                                            while((sms[k] != '*') && (k < 64))
                                                       cmd[m][j++] = sms[k++];
 
                                            ++m;
                                            j = 0;
                              }
                              
                              control = 1;
                    
                    }
                    
                    parse = 0;
             }
             
             if(control){
             
                     if(((cmd[0][0] = 'P') && (cmd[0][1] = 'U') && (cmd[0][2] = 'M') && (cmd[0][3] = 'P') && (cmd[0][4] = ' ') && (cmd[0][5] = 'O') && (cmd[0][6] = 'N')) ||
                           ((cmd[1][0] = 'P') && (cmd[1][1] = 'U') && (cmd[1][2] = 'M') && (cmd[1][3] = 'P') && (cmd[1][4] = ' ') && (cmd[1][5] = 'O') && (cmd[1][6] = 'N'))){
                                               PORTB.F1 = 1;
                     }
                     if(((cmd[0][0] = 'L') && (cmd[0][1] = 'I') && (cmd[0][2] = 'G') && (cmd[0][3] = 'H') && (cmd[0][4] = 'T') && (cmd[0][5] = ' ') && (cmd[0][6] = 'O')  && (cmd[0][7] = 'N')) ||
                           ((cmd[1][0] = 'L') && (cmd[1][1] = 'I') && (cmd[1][2] = 'G') && (cmd[1][3] = 'H') && (cmd[1][4] = 'T') && (cmd[1][5] = ' ') && (cmd[1][6] = 'O')  && (cmd[1][7] = 'N'))) {
                                               PORTB.F2 = 1;
                     }
                     if(((cmd[0][0] = 'P') && (cmd[0][1] = 'U') && (cmd[0][2] = 'M') && (cmd[0][3] = 'P') && (cmd[0][4] = ' ') && (cmd[0][5] = 'O') && (cmd[0][6] = 'F') && (cmd[0][7] = 'F')) ||
                           ((cmd[1][0] = 'P') && (cmd[1][1] = 'U') && (cmd[1][2] = 'M') && (cmd[1][3] = 'P') && (cmd[1][4] = ' ') && (cmd[1][5] = 'O') && (cmd[1][6] = 'F') && (cmd[0][7] = 'F'))){
                                               PORTB.F1 = 0;
                     }
                     if(((cmd[0][0] = 'L') && (cmd[0][1] = 'I') && (cmd[0][2] = 'G') && (cmd[0][3] = 'H') && (cmd[0][4] = 'T') && (cmd[0][5] = ' ') && (cmd[0][6] = 'O')  && (cmd[0][7] = 'F') && (cmd[0][8] = 'F')) ||
                           ((cmd[1][0] = 'L') && (cmd[1][1] = 'I') && (cmd[1][2] = 'G') && (cmd[1][3] = 'H') && (cmd[1][4] = 'T') && (cmd[1][5] = ' ') && (cmd[1][6] = 'O')  && (cmd[1][7] = 'F') && (cmd[1][8] = 'F'))) {
                                               PORTB.F2 = 0;
                     }
                     
                     if(((cmd[0][0] = 'E') && (cmd[0][1] = 'R') && (cmd[0][2] = 'A') && (cmd[0][3] = 'S') && (cmd[0][4] = 'E')) ||
                           ((cmd[1][0] = 'E') && (cmd[1][1] = 'R') && (cmd[1][2] = 'A') && (cmd[1][3] = 'S') && (cmd[1][4] = 'E'))) {
                                             for(j = 0; j < 15; j++){
                                                   EEPROM_Write(0x00 + j, 0xFF);
                                                   Delay_ms(20);
                                             }
                                             
                     }
                     
                     
                     for(j = 0; j < 10; j++)cmd[0][j] = '\0';
                     for(j = 0; j < 10; j++)cmd[1][j] = '\0';
             }
     }
}

 
Last edited:

As far as I see, readSMS is never set to 2, so no data will be ever received in the UART interrupt.

Generally, I doubt that the method of parsing modem responses will work. Modem responses have leading <CR><LF> which isn't handled by the code.

Personally I don't like the mixture of interrupt based UART receiption and non-interrupt built-in functions usage. A straightforward scheme would receive all UART data trough interrupt and a circular buffer.
 
Thank you. I will implement the interrupt code. As I said earlier I am not using interrupt in the code I have posted. The problem using interrupt is if I want to test response for AT command then response will be

AT

OK

So, it will be A T \r \n \r \n O K \r \n

but if I parse for \r \n then I have to skip the first 2 \r and \n's.

If I use mikroC library function then it reads until "OK" is received.

Edit: I am attaching Sim Video. I am entering \r \n A T \r \n \r \n O K. So there are leading CR LF.
 

Attachments

  • gsmsim.rar
    261.9 KB · Views: 91
Last edited:

So, it will be A T \r \n \r \n O K \r \n
According to modem AT handbooks and 3GPP TS 07.07 AT command set for GSM Mobile Equipment (ME), the response is <CR><LF>OK<CR><LF>.

If you are using <LF> (0xa) as receive delimiter, you should also ignore <CR> respectively remove it from the response. Then the leading <CR><LF> is just an empty string which can be ignored as well.
 
OK. I am using my Mobile as modem as my SIM900 modem was not sending SMS. When I used my mobile and send AT then it shows response

AT (sent by me)
AT (sent by mobile)

OK

So, I used \r \n, A T \r \n \r \n O K.


I will check it again and reply.

Edit: See image. Green is sent command, yellow is modem response.
 

Attachments

  • gsmresponse.png
    gsmresponse.png
    64.8 KB · Views: 128
Last edited:

You don't need all the delimiters when sending commands, only a trailing <CR> is required.

<LF> may be used. Leading delimiters can be helpful to indicate start of a new command line.
 
Sorry but that's no way to write a code for a GSM module, or for any serial-based devices.. At the least, you need to use serial interrupts, circular buffer, string processing. Else, your code is hopelessly unwieldy and unreliable.

- - - Updated - - -

In this **broken link removed**, there is code for interrupt-driven circular FIFO buffer. Its an excellent C code and I have used it a few times. Just use Google translate if you want to follow the discussion. But if you want the code, look for the ser.c and ser.h.

You can also find this code posted in Microchipc.com. If you want to learn how to use it, and understands its code line by line, I can teach you.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top