-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGPRS.cpp
593 lines (554 loc) · 22.4 KB
/
GPRS.cpp
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
#include "GPRS.h"
//#define DEBUG_TERMINAL_CHAR
//#define DEBUG_TERMINAL_STRING
//#define DEBUG_TERMINAL_BREAK
//#define DEBUG_TERMINAL_RETURNING
//#define DEBUG_RUN_COUNT
//#define DEBUG_TRIMMING
#define DEBUG_BUFFER
#define DEBUG_SEND_PRINT
#define DEBUG_OK_PRINT
GPRS::GPRS(HardwareSerial *modemPort, user * users, GPS *gps, RFID *rfid, LCD *lcdin)
{
modempin = modemPort;
userdata = users;
gpsdata = gps;
rfiddata = rfid;
lcd = lcdin;
waiting = OFF;
count =0;
ATindex = 0;
runcount = 0;
ok_variable = 0;
ATsetupcommand[0] = "AT";
ATsetupcommand[1] = "AT+IPR=19200";
ATsetupcommand[2] = "AT+CMEE=2";
ATsetupcommand[3] = "AT+CREG?";
ATsetupcommand[4] = "AT+FLO=0";
ATsetupcommand[5] = "AT+CGDCONT=1,\"IP\",\"airtelgprs.com\"";
ATsetupcommand[6] = "AT#GPRS=1";
//ATsendcommand[0] = "AT#SKTD=0,80,\"www.utkarshsins.com\",255,0";
ATsendcommand[0] = "AT#SD=2,0,80,\"www.boozepanda.com\"";
ATsendcommand[1] = "POST /terms/priority/listener.php HTTP/1.1\r\nHOST: www.boozepanda.com\r\nUser-Agent: HTTPTool/1.1\r\nContent-Type: application/x-www-form-urlencoded";
//ATsendcommand[2] = "";
//ATsendcommand[3] = "AT#SH=2";
}
void GPRS::begin()
{
modempin->begin(19200);
}
void GPRS::reset()
{
ATindex=0;
waiting=OFF;
timestart=0;
count =0;
}
boolean GPRS::checktimeout(long timeout1)
{
// if(ATindex ==1 && (millis() - timestart) <= timeout1)
// Serial.println("Still no time out");
if((millis() - timestart) > timeout1)
Serial.println("TIMEOUT!!");
return (millis() - timestart) <= timeout1;
}
int GPRS::readterminal()
{
int x=0;
while(modempin->available())
{
//Serial.println("Modem is available");
char check = modempin->read();
checkstring += check;
#ifdef DEBUG_TERMINAL_CHAR
Serial.print("[TERMINAL] reading : ");
if(check == '\r')
Serial.println("\\r");
else if(check == '\n')
Serial.println("\\n");
else
Serial.println(check);
#endif
x++;
}
#ifdef DEBUG_BUFFER
{
String debugbuffer = checkstring;
debugbuffer.replace("\r","\\r");
debugbuffer.replace("\n","\\n");
Serial.println("[BUFFER] " + debugbuffer);
}
#endif
if(gprsstate == sendstate && ATindex == 2)
{
if((checkstring.indexOf("OK")!=-1 || checkstring.indexOf("NO CARRIER")!=-1) && ok_variable == 0)
{
ok_variable++;
if(checkstring.indexOf("OK")!=-1)
{
checkstring = checkstring.substring(checkstring.indexOf("OK") +2);
}
else if(checkstring.indexOf("NO CARRIER")!=-1)
{
checkstring = checkstring.substring(checkstring.indexOf("NO CARRIER") +10);
}
}
}
if(checkstring.indexOf("\r\n")!=-1)
{
// Serial.println("Found : " + checkstring);
#ifdef DEBUG_TRIMMING
Serial.println(checkstring.substring(0,checkstring.indexOf("\r\n")) + "\\r\\n" + checkstring.substring(checkstring.indexOf("\r\n")+2));
#endif
String checkconds = checkstring.substring(0,checkstring.indexOf("\r\n"));
if(checkconds.indexOf("NO") == -1 && checkconds.indexOf("ERROR") == -1 && checkconds.indexOf("OK") == -1 && checkconds.indexOf("CONNECT") == -1 && checkconds.indexOf("activated") == -1 && checkconds.indexOf("failed") == -1&& checkconds.indexOf("resolve DN") == -1&& checkconds.indexOf("inserted") == -1)
{
checkstring = checkstring.substring(checkstring.indexOf("\r\n")+2);
#ifdef DEBUG_TRIMMING
Serial.println("Trimmed First : "+checkstring);
Serial.println("Trimming End");
#endif
}
}
#ifdef DEBUG_TERMINAL_STRING
Serial.print("[TERMINAL] string : ");
Serial.println(checkstring);
#endif
#ifdef DEBUG_TERMINAL_BREAK
Serial.println("[TERMINAL] break");
#endif
if(checkstring.indexOf("NO") != -1)
{
#ifdef DEBUG_TERMINAL_RETURNING
Serial.println("[TERMINAL] Returning : 0");
#endif
if(ok_variable == 1)
{
checkstring = checkstring.substring(checkstring.indexOf("NO CARRIER") +10);
return 1;
}
if(checkstring.indexOf("\r\n")!=-1)
{
#ifdef DEBUG_TRIMMING
Serial.println("Trimming : " + checkstring + "\t=>\t" + checkstring.substring(checkstring.indexOf("\r\n")+2));
Serial.println("Trimming End");
#endif
checkstring = checkstring.substring(checkstring.indexOf("\r\n")+2);
}
return 0;
}
else if(checkstring.indexOf("ERROR\r\n") != -1)
{
#ifdef DEBUG_TERMINAL_RETURNING
Serial.println("[TERMINAL] Returning : 0");
#endif
#ifdef DEBUG_TRIMMING
Serial.println("Trimming : " + checkstring + "\t=>\t" + checkstring.substring(checkstring.indexOf("\r\n")+2));
Serial.println("Trimming End");
#endif
checkstring = checkstring.substring(checkstring.indexOf("\r\n")+2);
return 0;
}
else if(checkstring.indexOf("OK\r\n") != -1 && !(gprsstate== sendstate && ATindex ==1))
{
#ifdef DEBUG_OK_PRINT
Serial.println("OK");
#endif
#ifdef DEBUG_TERMINAL_RETURNING
Serial.println("[TERMINAL] Returning : 1");
#endif
if(checkstring.indexOf("\r\n")!=-1)
{
#ifdef DEBUG_TRIMMING
Serial.println("Trimming : " + checkstring + "\t=>\t" + checkstring.substring(checkstring.indexOf("\r\n")+2));
Serial.println("Trimming End");
#endif
checkstring = checkstring.substring(checkstring.indexOf("OK\r\n")+4);
}
return 1;
}
else if(checkstring.indexOf("CONNECT\r\n") != -1)
{
#ifdef DEBUG_OK_PRINT
Serial.println("CONNECT");
#endif
#ifdef DEBUG_TERMINAL_RETURNING
Serial.println("[TERMINAL] Returning : 3");
#endif
if(checkstring.indexOf("\r\n")!=-1)
{
#ifdef DEBUG_TRIMMING
Serial.println("Trimming : " + checkstring + "\t=>\t" + checkstring.substring(checkstring.indexOf("\r\n")+2));
Serial.println("Trimming End");
#endif
checkstring = checkstring.substring(checkstring.indexOf("CONNECT\r\n")+9);
}
return 3;
}
else if(checkstring.indexOf("activated\r\n") != -1)
{
Serial.println("................. in the loop... already activated.......................");
//if(checkstring.indexOf("\r\n") != -1)
//{
checkstring = checkstring.substring(checkstring.indexOf("activated")+11);
//}
return 4;
}
else if(checkstring.indexOf("failed\r\n") != -1)
{
Serial.println("................ in the loop ......failed .......... ");
checkstring = checkstring.substring(checkstring.indexOf("failed")+8);
return 5;
}
else if(checkstring.indexOf("resolve DN\r\n") != -1)
{
Serial.println("................ in the loop ......resolve DN .......... ");
checkstring = checkstring.substring(checkstring.indexOf("resolve DN")+12);
return 6;
}
else if(checkstring.indexOf("inserted\r\n") != -1)
{
checkstring = checkstring.substring(checkstring.indexOf("inserted")+8);
return 7;
}
else
{
if(checkstring.indexOf("Server Error")!=-1)
{
if(checkstring.indexOf("</html>")!=-1)
{
checkstring = checkstring.substring(checkstring.indexOf("</html>")+7);
return 1;
}
}
if(checkstring.indexOf("Not Found")!=-1)
{
if(checkstring.indexOf("</html>")!=-1)
{
checkstring = checkstring.substring(checkstring.indexOf("</html>")+7);
return 1;
}
}
if(checkstring.indexOf("Not Implemented")!=-1)
{
if(checkstring.indexOf("</html>")!=-1)
{
checkstring = checkstring.substring(checkstring.indexOf("</html>")+7);
return 1;
}
}
if(checkstring.indexOf("[") != -1)
{
if(checkstring.indexOf("]") != -1)
{
checkstring = checkstring.substring(checkstring.indexOf("["),checkstring.indexOf("]")+1);
Serial.println("PARSING : " + checkstring);
long_parse(checkstring);
checkstring = checkstring.substring(checkstring.indexOf("]")+1);
return 1;
}
}
#ifdef DEBUG_TERMINAL_RETURNING
Serial.print("[TERMINAL] String : ");
Serial.println(checkstring);
Serial.println("[TERMINAL] Returning : 2");
#endif
return 2;
}
}
void GPRS::long_parse(String FullString)
{
String temp = FullString;
String str;
size_t pos1;
size_t pos2;
int count = 0;
while(temp!= "]" && temp!="[]")
{
pos1 = temp.indexOf("{");
pos2 = temp.indexOf("}");
str = temp.substring(pos1+1,pos2);
userdata[count++].parse(str);
temp = temp.substring(pos2+1);
}
}
void GPRS::setup()
{
if(waiting==OFF)
{
//char buf[BUF_LENGTH];
requestModem(ATsetupcommand[ATindex], 1000);
timestart = millis();
waiting = ON;
timeout = setuptimeout;
}
else
{
if(modempin->available())
{
//Serial.println("GPRS modem pin is here");
int variable = readterminal();
//Serial.print("variable = ");
//?Serial.println(variable);
if(variable==1 || variable==4) {
waiting = OFF;
count =0;
if(ATindex == 4)
Serial.println("Started GM865");
else if(ATindex == setupcount -1)
{
Serial.println("Connected to Server");
gprsstate = sendstate;
Serial.println("[STATE CHANGE] state changed from 0 to 1");
}
if(ATindex< setupcount-1)
ATindex++;
else
{
ATindex = 0;
}
}
else if(variable==0 || variable == 5 || variable == 6 || variable ==7)
{
if(count < 3)
{
waiting = OFF;
count++;
}
else
reset();
}
}
if(!checktimeout(timeout))
{
reset();
}
}
}
// TODO:
// @Saurabh - Think of a way to find out that the http request
// should have ended and now the modem should switch the state
// to terminate the connection
void GPRS::send()
{
if(waiting==OFF)
{
//char buf[BUF_LENGTH];
timestart = millis();
if(ATindex<2)
requestModem(ATsendcommand[ATindex], 1000);
else if(ATindex== 2)
{
// TODO: ESCAPE SEQUENCE + TERMINATION AT COMMAND
// @Akshay - Put this in the state machine
// Replace the delays with timeouts
Serial.println("TERMINATING CONNECTION");
delay(2000);
Serial.println("+++");
modempin->print("+++");
delay(2000);
lcd->invalidate();
lcd->display(3);
requestModem("AT#SH=2", 100);
//gprsstate=waitingtosendstate;
}
if(ATindex==1)
{
ok_variable = 0;
cycletimestart = millis();
}
// TODO: ESCAPE SEQUENCE + TERMINATION AT COMMAND
// @Akshay - Put this in the state machine
// Replace the delays with timeouts
//gprsstate=waitingtosendstate;
waiting = ON;
if(ATindex == 1)
{
Serial.println("sending request ...");
String temp_string;
int k,j,DATA_LENGTH = 0, temp_length = 0;
for(k=0;k<N_GPS;k++)
{
// Serial.print("k = ");
// Serial.println(k);
myPosition *temp = gpsdata->positions(k);
temp_string = "&latitude"+String(k, DEC)+"="+String((long)(temp->lat), DEC)+"&longitude"+String(k, DEC)+"="+String((long)(temp->lon), DEC)+"&date"+String(k, DEC)+"="+String((long)(temp->date), DEC)+"&gpstime"+String(k, DEC)+"="+String((long)(temp->time), DEC) + "&busid"+String(k, DEC)+"=1";
temp_length += temp_string.length();
}
for(j=0;j<N_RFID;j++)
{
int count = 1;
if(rfiddata[j].to_send == true){
temp_string = "uid"+String(count++, DEC)+"="+String(rfiddata[j].uid, DEC)+"&rfidtime"+String(count++, DEC)+"="+String(rfiddata[j].time, DEC);
temp_length += temp_string.length();
}
}
if((gpsdata->positions(0))->lat!=0)
DATA_LENGTH = temp_length;
{
String lengthed = "Content-Length: " + String(DATA_LENGTH, DEC) + "\r\n\r\n";
char buff[lengthed.length()+1];
lengthed.toCharArray(buff,lengthed.length()+1);
modempin->print(buff);
Serial.print(buff);
}
if((gpsdata->positions(0))->lat!=0)
{
for(k=0;k<N_GPS;k++)
{
// Serial.print("k = ");
// Serial.println(k);
myPosition *temp = gpsdata->positions(k);
temp_string = "&latitude"+String(k, DEC)+"="+String((long)(temp->lat), DEC)+"&longitude"+String(k, DEC)+"="+String((long)(temp->lon), DEC)+"&date"+String(k, DEC)+"="+String((long)(temp->date), DEC)+"&gpstime"+String(k, DEC)+"="+String((long)(temp->time), DEC) + "&busid"+String(k, DEC)+"=2";
char buff[temp_string.length()+1];
temp_string.toCharArray(buff,temp_string.length()+1);
modempin->print(buff);
Serial.print(buff);
}
// Serial.println(string);
// Serial.println(string.length());
for(j=0;j<N_RFID;j++)
{
int count = 0;
if(rfiddata[j].to_send == true){
temp_string = "uid"+String(count, DEC)+"="+String(rfiddata[j].uid, DEC)+"&rfidtime"+String(count++, DEC)+"="+String(rfiddata[j].time, DEC);
char buff[temp_string.length()+1];
temp_string.toCharArray(buff,temp_string.length()+1);
modempin->print(buff);
Serial.println(buff);
}
}
}
// Serial.println(string);
// Serial.println(string.length());
//modempin->print("\r\n");
}
timeout = sendtimeout;
}
else
{
if(modempin->available())
{
int variable = readterminal();
if(variable==3)
{
waiting = OFF;
ATindex++;
count = 0;
}
else if(variable==0 || variable == 6)
{
if(count < 3)
{
waiting = OFF;
count++;
}
else
{
if(ATindex==1)
{
ATindex++;
waiting = OFF;
}
else
reset();
}
}
else if(variable==1)
{
count =0;
if(ATindex<sendcount-1)
ATindex++;
else{
ATindex=0;
gprsstate = waitingtosendstate;
}
waiting = OFF;
}
}
if(!checktimeout(timeout))
{
if(ATindex==1)
{
ATindex++;
waiting = OFF;
}
else
reset();
}
}
}
void GPRS::requestModem(const String command, uint16_t timeout)
{
// byte count = 0;
//char *found = 0;
//*buf = 0;
Serial.println("[SENDING] " + command + "\n[state] = " + gprsstate);
modempin->print(command);
modempin->print('\r');
modempin->print('\n');
/*count = getsTimeout(buf, timeout);
if (count) {
if (check) {
found = strstr(buf, "\r\nOK\r\n");
if (found) {
Serial.println("->ok");
}
else {
Serial.print("->not ok: ");
Serial.println(buf);
}
}
else {
Serial.print("->buf: ");
Serial.println(buf);
}
}
else {
Serial.println("->no respone");
}*/
//return count;
}
boolean GPRS::run()
{
#ifdef DEBUG_RUN_COUNT
runcount++;
Serial.print("[RUNCOUNT] ");
Serial.println(runcount);
#endif
if(gprsstate==setupstate)
{
setup();
}
else if(gprsstate==sendstate)
{
//cycletimestart = millis();
send();
}
else if(gprsstate==waitingtosendstate)
{
if(millis()-cycletimestart>=cycletimeout)
gprsstate = sendstate;
}
return gprsstate!=sendstate;
}
/*byte getsTimeout(char *buf, uint16_t timeout) {
byte count = 0;
long timeIsOut = 0;
char c;
*buf = 0;
timeIsOut = millis() + timeout;
while (timeIsOut > millis() && count < (BUF_LENGTH - 1)) {
if (modem->available()) {
count++;
c = modem->read();
*buf++ = c;
timeIsOut = millis() + timeout;
}
}
if (count != 0) {
*buf = 0;
count++;
}
return count;
}*/