-
Notifications
You must be signed in to change notification settings - Fork 0
/
SIM800.c~
621 lines (560 loc) · 12.8 KB
/
SIM800.c~
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#include "SIM800.h"
extern char rec_buff[rec_buff_size];
extern long int buff_counter;
extern int val[8];
extern char VAR_1[7];
extern char VAR_2[7];
extern char VAR_3[7];
extern char VAR_4[7];
extern char VAR_5[7];
extern char VAR_6[7];
extern char VAR_7[7];
extern char VAR_8[7];
extern char _SERVER_[20];
extern char _APN_[20];
extern char _HOST_[20];
extern char _PAGEADDRESS_[50];
extern char _SUCCESSSIGN_[20];
//Functions:
//This Function is for emptying any array || buff_2_empty: The Array || cells_2_empty: the number of cells should be cleaned.
void emp_str(char *buff_2_empty,int cells_2_empty)
{
int ii;
for (ii=0;ii<cells_2_empty;ii++)
{
buff_2_empty[ii]='\0';
}
}
//This Function is for searching the recieved buffer from GSM Module for a specific string. if the string found, it will return 1. else 0.
//Headers Needed: string.h
//Global vars needed: rec_buff
int search(char *str_to_search)
{
if(strstr(rec_buff,str_to_search))
{
return 1;
}
else
{
return 0;
}
}
//This function waites until it saw a specefic string in recieved buffer.
//Input paramiters: str_2_s: string to see, time_out: time out in sec
//output: if saw before time out:1 else 0
//headers needed: delay.h
int wait_until(char* str_2_s,int time_out)
{
int t_out=1;
while(!search(str_2_s) && t_out<=10*time_out)
{
t_out++;
delay_ms(100);
}
if(t_out<10*time_out)
{
return 1;
}
else
{
return 0;
}
}
//This function clears All cell in rec_buff (data from module)
//Global Vars Needed: buff_counter, rec_buff
void clear_rec_buff(void)
{
emp_str(rec_buff,rec_buff_size-1);
buff_counter=0;
}
//AT Command send Function.
void at_command(char* at_cmnd)
{
printf("%s%c",at_cmnd,0x0d);
}
//õSend Ctrl+Z to Modem:
//Headers Needed: delay.h
ctrl_z(void)
{
delay_ms(100);
printf("%c",0x1a);
delay_ms(50);
}
//This Function initialize the sim800 Module || pin_code: the simcard pin
//if pin code is deactivated, let it be free like this: sim800_init("")
//Headers Needed: delay.h
void sim800_init(char* pin_code)
{
char at_buff[12];
at_command("ATZ"); //Reset Module
delay_ms(100);
at_command("AT&F"); //Factory reset Module
delay_ms(200);
at_command("AT+CMEE=1"); //Deactive echo
delay_ms(50);
//at_command("ATE0"); //Deactive echo
delay_ms(50);
clear_rec_buff();
//check pin code:
at_command("AT+CPIN?");
delay_ms(50);
if(!search("READY"))
{
sprintf(at_buff,"AT+CPIN=\"%s\"",pin_code);
at_command(at_buff);
delay_ms(100);
}
}
//This Function initialize sending sms in module.
//headers needed: delay.h
void sms_init(void)
{
at_command("AT+CMGF=1");
delay_ms(50);
at_command("AT+CSMP=17,196,0,0");
delay_ms(50);
at_command("AT+CSCS=\"GSM\"");
delay_ms(50);
}
//This Function will check if the module is respond.
int sim_800_ping(void)
{
clear_rec_buff();
at_command("AT");
if(wait_until("OK",1))
{
delay_ms(50);
return 1;
}
else
{
delay_ms(50);
return 0;
}
}
//This Function checks the Module registration in network
//Headers needed: delay.h
int check_reg(void)
{
clear_rec_buff();
at_command("AT+CREG?");
delay_ms(100);
if(search("0,1"))
{
return 1;
}
else
{
return 0;
}
}
//This Function sends sms
//headers needed: delay.h
int send_sms(char* sms_text, char* phone_number)
{
char at_buff[25];
int time_out=0;
int stat=0;
sms_init();
clear_rec_buff();
sprintf(at_buff,"AT+CMGS=\"+%s\"",phone_number);
at_command(at_buff);
delay_ms(100);
while(!search(">") && time_out<50)
{
time_out++;
delay_ms(100);
}
if(time_out<50) //means that it saw >
{
printf("%s",sms_text);
delay_ms(1000);
ctrl_z();
time_out=0;
clear_rec_buff();
while(!search("OK") && time_out<10)
{
time_out++;
delay_ms(1000);
}
if(time_out<10)
{
stat=1;
}
}
return stat;
}
//This function initializes socket connection in module
//Example: socket_init("Irancell-GPRS")
//headers needed: delay.h
int socket_init(char* APN_name)
{
char at_buff_[35];
int step=1;
clear_rec_buff();
//lcd_clear();
if(step==1)
{
//lcd_puts("1 ");
at_command("AT");
if(wait_until("OK",1))
{
step++;
delay_ms(500);
}
}
clear_rec_buff();
if(step==2)
{
//lcd_puts("2 ");
at_command("AT+CGATT=1");
wait_until("OK",10);
sprintf(at_buff_,"AT+CGDCONT=1,\"IP\",\"%s\"",APN_name);
at_command(at_buff_);
delay_ms(100);
if(!search("ERROR"))
{
step++;
}
}
clear_rec_buff();
if(step==3)
{
//lcd_puts("3 ");
sprintf(at_buff_,"AT+CSTT=\"%s\",\"\",\"\"",APN_name);
at_command(at_buff_);
if(wait_until("OK",10))
{
step++;
}
}
clear_rec_buff();
if(step==4)
{
//lcd_puts("4 ");
at_command("AT+CIICR");
if(wait_until("OK",15))
{
step++;
}
}
clear_rec_buff();
if(step==5)
{
//lcd_puts("5 ");
at_command("AT+CIFSR");
delay_ms(500);
step++;
}
if(step<6)
{
//lcd_puts("NOT");
return 0;
}
else
{
//lcd_puts("6 ");
return 1;
}
}
//This Function will open socket to inputed ip (or domain) and port 80
int open_socket(char *server)
{
char at_buff[50];
clear_rec_buff();
at_command("AT+CIPHEAD=1");
wait_until("OK",3);
clear_rec_buff();
sprintf(at_buff,"AT+CIPSTART=\"TCP\",\"%s\",\"80\"",server);
at_command(at_buff);
wait_until("OK",3);
wait_until("CONNECT",20);
if(search("ALREADY"))
{
//lcd_clear();
//lcd_puts("ALREADY");
return 2;
}
else if(search("CONNECT OK"))
{
//lcd_clear();
//lcd_puts("c OK");
return 1;
}
else if(search("FAIL"))
{
//lcd_clear();
//lcd_puts("fail");
return 0;
}
else
{
return 0;
}
}
//Function fot ready to send DATA in socket
//headers: delay.h, string.h
//success_sign: means the string that is sign for beggining of the return value of the server, ex: success
int socket_send_data(char* socket_data, char* success_sign)
{
int stat=0;
clear_rec_buff();
delay_ms(100);
at_command("AT+CIPSEND");
if(wait_until(">",20))
{
//lcd_clear();
//lcd_puts(">");
clear_rec_buff();
printf("%s",socket_data);
delay_ms(500);
ctrl_z();
delay_ms(50);
ctrl_z();
if(wait_until("SEND OK",20))
{
//lcd_puts("Send OK");
stat=1;
if(wait_until(success_sign,30))
{
//lcd_puts("succs");
delay_ms(1000);
//lcd_clear();
//lcd_puts(ret);
}
}
}
return stat;
}
//this function will init the program data variables
void prog_init(void)
{
//Data Var naming:
sprintf(VAR_1, "l");
sprintf(VAR_2, "t");
sprintf(VAR_3, "m");
sprintf(VAR_4, "b");
sprintf(VAR_5, "c");
sprintf(VAR_6, "s");
sprintf(VAR_7, "ant");
sprintf(VAR_8, "n");
//Server Paramiters:
sprintf(_SERVER_, "104.28.25.5");
sprintf(_APN_, "Irancell-GPRS");
sprintf(_HOST_, "gologram.com");
sprintf(_PAGEADDRESS_,"/api/v1/probes/process");
sprintf(_SUCCESSSIGN_,"success\":true");
}
//this function will close socket
void close_socket(void)
{
at_command("AT+CIPClOSE");
wait_until("OK",3);
}
//this function disconnects GPRS connection
void gprs_dis(void)
{
at_command("AT+CIPSHUT");
wait_until("OK",5);
}
//this function will upload variables to server by http post method.
int post_data(int vars_value[8])
{
int _time_out=0;
int _time_out1=0;
char post_buff[200];
char data_value[100];
//strlen
sprintf(data_value,"%s=%d&%s=%d&%s=%d&%s=%d&%s=%d&%s=%d&%s=%d&%s=%d",VAR_1,vars_value[0],VAR_2,vars_value[1],VAR_3,vars_value[2],VAR_4,vars_value[3],VAR_5,vars_value[4],VAR_6,vars_value[5],VAR_7,vars_value[6],VAR_8,vars_value[7]);
sprintf(post_buff,"POST %s HTTP/1.0\r\nHost: %s\r\nUser-Agent: HTTPTool/1.0\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n\r\n%s\r\n\r\n",_PAGEADDRESS_,_HOST_,strlen(data_value),data_value);
_time_out1=0;
socketstart:
while(socket_init(_APN_)!=1 && _time_out<=5)
{
//wait until connect to socket
_time_out++;
}
open_socket(_SERVER_);
if(socket_send_data(post_buff,_SUCCESSSIGN_))
{
close_socket();
delay_ms(500);
gprs_dis();
return 1;
}
else if(_time_out1<=5)
{
//lcd_puts("retry");
_time_out1++;
close_socket();
gprs_dis();
delay_ms(1000);
goto socketstart;
}
else
{
close_socket();
delay_ms(500);
gprs_dis();
return 0;
}
}
//This function turns on module
int sim800_on(void)
{
clear_rec_buff();
at_command("AT");
delay_ms(100);
if(!search("OK"))
{
PORTC.3=1;
delay_ms(3000);
PORTC.3=0;
}
clear_rec_buff();
at_command("AT");
if(wait_until("OK",1))
{
return 1;
}
else
{
PORTC.3=1;
delay_ms(3000);
PORTC.3=0;
return 0;
}
}
//This function turns OFF module
int sim800_off(void)
{
PORTC.3=1;
delay_ms(3000);
PORTC.3=0;
clear_rec_buff();
at_command("AT");
if(wait_until("OK",1))
{
PORTC.3=1;
delay_ms(3000);
PORTC.3=0;
return 0;
}
else
{
return 1;
}
}
//This Function will reset sim800
void sim800_reset(void)
{
sim800_off();
sim800_on();
}
//signal quality:
int signal_q(void)
{
char sig_buff[3];
int i_=0;
int j_=0;
emp_str(sig_buff,3);
clear_rec_buff();
at_command("AT+CSQ");
wait_until("CSQ",1);
while(rec_buff[i_]!=':' && i_<=20)
{
i_++;
}
i_++;
while(rec_buff[i_]!=',' && j_<=2)
{
sig_buff[j_]=rec_buff[i_];
i_++;
j_++;
}
while(j_<=2)
{
sig_buff[j_]='\0';
j_++;
}
return atoi(sig_buff);
}
// get server time
void get_server_time(int *s_year, int* s_mon, int* s_dat, int* s_hour, int* s_min, int* s_sec)
{
volatile int ii=0;
volatile int jj=0;
char YEAR[5];
char MONTH[3];
char DATE[3];
char HOUR[3];
char MIN[3];
char SEC[3];
while(ii<=rec_buff_size)
{
if(rec_buff[ii]=='t' && rec_buff[ii+1]=='i' && rec_buff[ii+2]=='m' && rec_buff[ii+3]=='e' && rec_buff[ii+4]=='"')
{
ii=ii+7;
while(jj<4)
{
YEAR[jj]=rec_buff[ii];
jj++;
ii++;
}
YEAR[4]='\0';
ii++;
jj=0;
while(jj<2)
{
MONTH[jj]=rec_buff[ii];
jj++;
ii++;
}
MONTH[2]='\0';
ii++;
jj=0;
while(jj<2)
{
DATE[jj]=rec_buff[ii];
jj++;
ii++;
}
DATE[2]='\0';
ii++;
jj=0;
while(jj<2)
{
HOUR[jj]=rec_buff[ii];
jj++;
ii++;
}
HOUR[2]='\0';
ii++;
jj=0;
while(jj<2)
{
MIN[jj]=rec_buff[ii];
jj++;
ii++;
}
MIN[2]='\0';
ii++;
jj=0;
while(jj<2)
{
SEC[jj]=rec_buff[ii];
ii++;
jj++;
}
SEC[2]='\0';
*s_year=atoi(YEAR);
*s_mon= atoi(MONTH);
*s_dat= atoi(DATE);
*s_hour=atoi(HOUR);
*s_min= atoi(MIN);
*s_sec= atoi(SEC);
break;
}
ii++;
}
}