-
Notifications
You must be signed in to change notification settings - Fork 1
/
CellularIoT.cpp
700 lines (561 loc) · 16.9 KB
/
CellularIoT.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
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/*
____ ____ _ __ __ ____ ___
* | _ \| _ \ / \ | \/ |/ ___/ _ \
* | | | | |_) | / _ \ | |\/| | | | | | |
* | |_| | _ < / ___ \| | | | |__| |_| |
* |____/|_| \_\/_/ \_\_| |_|\____\___/
* research group
* dramco.be/
*
* KU Leuven - Technology Campus Gent,
* Gebroeders De Smetstraat 1,
* B-9000 Gent, Belgium
CellularIoT.cpp
-
BG96 Library based on Sixfab Arduino CellularIoT Library (Yasin Kaya (selengalp))
Created by Dramco
*/
#include "CellularIoT.h"
#define BG96_AT Serial1
// default
CellularIoT::CellularIoT()
{
}
/******************************************************************************************
*** Base Functions : Set or Clear Hardwares - Status Controls - Helper AT Functions *****
******************************************************************************************/
// function for initializing BG96 module.
void CellularIoT::init()
{
// setting pin directions
pinMode(DTR, OUTPUT);
pinMode(RING_INDICATOR, INPUT);
enable();
// setting serials
BG96_AT.begin(9600);
DEBUG.begin(115200);
powerUp();
DEBUG.println("Module initializing");
delay(500); // wait until module ready.
delay(3000);
sendATComm("ATE1","OK\r\n");
sendATComm("ATE1","OK\r\n");
sendATComm("AT","OK\r\n");
}
// power up BG96 module and all peripherals from voltage regulator
void CellularIoT::enable()
{
pinMode(BG96_ENABLE, OUTPUT);
digitalWrite(BG96_ENABLE,HIGH);
}
// power down BG96 module and all peripherals from voltage regulator
void CellularIoT::disable()
{
digitalWrite(BG96_ENABLE,LOW);
}
// power up or down BG96 module
void CellularIoT::powerUp()
{
pinMode(BG96_POWERKEY,OUTPUT);
delay(10);
digitalWrite(BG96_POWERKEY,HIGH);
delay(500);
digitalWrite(BG96_POWERKEY,LOW);
delay(10);
digitalWrite(BG96_RESETKEY,HIGH);
delay(500);
digitalWrite(BG96_RESETKEY,LOW);
}
// power up or down BG96 module
/*uint8_t CellularIoT::getModemStatus()
{
//pinMode(STATUS,INPUT);
//delay(10);
return digitalRead(STATUS);
}*/
// send at comamand to module
void CellularIoT::sendATCommOnce(const char *comm)
{
DEBUG.println(comm);
BG96_AT.print(comm);
BG96_AT.print("\r");
//DEBUG.println(comm);
}
// function for sending at command to BG96_AT.
const char* CellularIoT::sendATComm(const char *command, const char *desired_reponse)
{
uint32_t timer;
char response[AT_RESPONSE_LEN]; // module response for AT commands.
memset(response, 0 , AT_RESPONSE_LEN);
BG96_AT.flush();
sendATCommOnce(command);
timer = millis();
while(true){
if(millis()-timer > timeout){
sendATCommOnce(command);
timer = millis();
}
char c;
int i = 0;
while(BG96_AT.available()){
c = BG96_AT.read();
DEBUG.write(c);
response[i++]=c;
delay(2);
}
if(strstr(response, desired_reponse)){
return response;
memset(response, 0 , strlen(response));
break;
}
}
}
// function for sending at command to BG96_AT.
void CellularIoT::sendATCommBetter(const char *command, const char *desired_reponse, char * response)
{
uint32_t timer;
BG96_AT.flush();
sendATCommOnce(command);
timer = millis();
while(true){
if(millis()-timer > timeout){
sendATCommOnce(command);
timer = millis();
}
char c;
int i = 0;
while(BG96_AT.available()){
c = BG96_AT.read();
DEBUG.write(c);
response[i++]=c;
delay(2);
}
if(strstr(response, desired_reponse)){
return;
}
}
}
// function for sending data to BG96_AT.
const char* CellularIoT::sendDataComm(const char *command, const char *desired_reponse)
{
uint32_t timer;
char response[AT_RESPONSE_LEN]; // module response for AT commands.
memset(response, 0 , AT_RESPONSE_LEN);
BG96_AT.flush();
BG96_AT.print(command);
int i=0 ;
timer = millis();
while(true){
if(millis()-timer > timeout){
BG96_AT.print(command);
timer = millis();
i = 0;
}
char c;
while(BG96_AT.available()){
c = BG96_AT.read();
DEBUG.write(c);
response[i++]=c;
delay(1);
if(strstr(response, desired_reponse)){
return response;
memset(response, 0 , strlen(response));
break;
}
}
if(strstr(response, desired_reponse)){
return response;
memset(response, 0 , strlen(response));
break;
}
}
}
// function for reset BG96_AT module
void CellularIoT::resetModule()
{
saveConfigurations();
delay(200);
digitalWrite(BG96_ENABLE,LOW);
delay(200);
digitalWrite(BG96_ENABLE,HIGH);
delay(200);
powerUp();
}
// Function for save configurations that be done in current session.
void CellularIoT::saveConfigurations()
{
sendATComm("AT&W","OK\r\n");
}
// Function for getting IMEI number
const char* CellularIoT::getIMEI()
{
return sendATComm("AT+CGSN","OK\r\n");
}
// Function for getting firmware info
const char* CellularIoT::getFirmwareInfo()
{
return sendATComm("AT+CGMR","OK\r\n");
}
//Function for getting hardware info
const char* CellularIoT::getHardwareInfo()
{
return sendATComm("AT+CGMM","OK\r\n");
}
// Function for setting GSM Band
void CellularIoT::setGSMBand(const char *gsm_band)
{
strcpy(compose, "AT+QCFG=\"band\",");
strcat(compose, gsm_band);
strcat(compose, ",");
strcat(compose, LTE_NO_CHANGE);
strcat(compose, ",");
strcat(compose, LTE_NO_CHANGE);
sendATComm(compose,"OK\r\n");
clear_compose();
}
// Function for setting Cat.M1 Band
void CellularIoT::setCATM1Band(const char *catm1_band)
{
strcpy(compose, "AT+QCFG=\"band\",");
strcat(compose, GSM_NO_CHANGE);
strcat(compose, ",");
strcat(compose, catm1_band);
strcat(compose, ",");
strcat(compose, LTE_NO_CHANGE);
sendATComm(compose, "OK\r\n");
clear_compose();
}
// Function for setting NB-IoT Band
void CellularIoT::setNBIoTBand(const char *nbiot_band)
{
strcpy(compose, "AT+QCFG=\"band\",");
strcat(compose, GSM_NO_CHANGE);
strcat(compose, ",");
strcat(compose, LTE_NO_CHANGE);
strcat(compose, ",");
strcat(compose, nbiot_band);
sendATComm(compose,"OK\r\n");
clear_compose();
}
// Function for getting current band settings
const char* CellularIoT::getBandConfiguration()
{
return sendATComm("AT+QCFG=\"band\"","OK\r\n");
}
// Function for setting scramble feature configuration
void CellularIoT::setScrambleConf(const char *scramble)
{
strcpy(compose, "AT+QCFG=\"nbsibscramble\",");
strcat(compose, scramble);
sendATComm(compose,"OK\r\n");
clear_compose();
}
//Function for setting running mode.
void CellularIoT::setMode(uint8_t mode)
{
if(mode == AUTO_MODE){
sendATComm("AT+QCFG=\"nwscanseq\",00,1","OK\r\n");
sendATComm("AT+QCFG=\"nwscanmode\",0,1","OK\r\n");
sendATComm("AT+QCFG=\"iotopmode\",2,1","OK\r\n");
DEBUG.println("Modem configuration : AUTO_MODE");
DEBUG.println(F("*Priority Table (Cat.M1 -> Cat.NB1 -> GSM)"));
}else if(mode == GSM_MODE){
sendATComm("AT+QCFG=\"nwscanseq\",01,1","OK\r\n");
sendATComm("AT+QCFG=\"nwscanmode\",1,1","OK\r\n");
sendATComm("AT+QCFG=\"iotopmode\",2,1","OK\r\n");
DEBUG.println(F("Modem configuration : GSM_MODE"));
}else if(mode == CATM1_MODE){
sendATComm("AT+QCFG=\"nwscanseq\",02,1","OK\r\n");
sendATComm("AT+QCFG=\"nwscanmode\",3,1","OK\r\n");
sendATComm("AT+QCFG=\"iotopmode\",0,1","OK\r\n");
DEBUG.println(F("Modem configuration : CATM1_MODE"));
}else if(mode == CATNB1_MODE){
sendATComm("AT+QCFG=\"nwscanseq\",03,1","OK\r\n");
sendATComm("AT+QCFG=\"nwscanmode\",3,1","OK\r\n");
sendATComm("AT+QCFG=\"iotopmode\",1,1","OK\r\n");
DEBUG.println(F("Modem configuration : CATNB1_MODE ( NB-IoT )"));
}
}
// function for getting ip_address
const char* CellularIoT::getIPAddress()
{
return ip_address;
}
// function for setting ip_address
void CellularIoT::setIPAddress(char *ip)
{
strcpy(ip_address, ip);
}
// function for getting domain_name
const char* CellularIoT::getDomainName()
{
return domain_name;
}
// function for setting domain name
void CellularIoT::setDomainName(char *domain)
{
strcpy(domain_name, domain);
}
// function for getting domain_name
const char* CellularIoT::getPort()
{
return port_number;
}
// function for setting port
void CellularIoT::setPort(char *port)
{
strcpy(port_number, port);
}
// get timout in ms
uint16_t CellularIoT::getTimeout()
{
return timeout;
}
// set timeout in ms
void CellularIoT::setTimeout(uint16_t new_timeout)
{
timeout = new_timeout;
}
// Function for enabling sleep
void CellularIoT::sleep(){
sendATComm("AT+QSCLK=1","OK\r\n");
digitalWrite(DTR, HIGH);
}
// Function for waking up
void CellularIoT::wake(){
digitalWrite(DTR, LOW);
}
// Function for enabling sleep
void CellularIoT::enableEIDRX(uint8_t edrx){
sendATComm("AT+CEDRXS=1,5,\"0000\"","OK\r\n");
}
/******************************************************************************************
*** Network Service Functions ************************************************************
******************************************************************************************/
//
const char* CellularIoT::getSignalQuality(){
return sendATComm("AT+CSQ","OK\r\n");
}
//
const char* CellularIoT::getQueryNetworkInfo(){
return sendATComm("AT+QNWINFO","OK");
}
// connect to base station of operator
void CellularIoT::selectOperator(unsigned int op){
printf(compose, "AT+COPS=1,2,\"%d\",9", op);
sendATComm(compose,"OK\r\n");
Serial.println("done");
}
// connect to base station of operator
void CellularIoT::setPDPContextProximus(){
sendATComm("AT+CGDCONT=1,\"IP\",\"m2minternet.proximus.be\"","OK\r\n");
}
// connect to base station of operator
void CellularIoT::connectToOperator(){
DEBUG.println(F("Trying to connect base station of operator..."));
sendATComm("AT+CGATT?","+CGATT: 1\r\n");
getSignalQuality();
}
/******************************************************************************************
*** GNSS Functions ***********************************************************************
******************************************************************************************/
// Function for turning on GNSS
void CellularIoT::turnOnGNSS(){
sendATComm("AT+QGPS=1","OK\r\n");
}
// Function for turning of GNSS
void CellularIoT::turnOffGNSS(){
sendATComm("AT+QGPSEND","OK\r\n");
}
// Function for getting fixed location
void CellularIoT::getFixedLocation(float * latitudePointer, float * longitudePointer){
char response[AT_RESPONSE_LEN]; // module response for AT commands.
memset(response, 0 , AT_RESPONSE_LEN);
sendATCommBetter("AT+QGPSLOC?","+QGPSLOC:",response);
char * startLocation = strstr(response, "+QGPSLOC: ");
char tempBuffer[10];
strncpy(tempBuffer, startLocation+19, 2);
tempBuffer[2] = '\0';
int latitudeDegrees = atoi(tempBuffer);
strncpy(tempBuffer, startLocation+21,8);
tempBuffer[8] = '\0';
float latitudeMinutes = atof(tempBuffer);
int latitudeSign;
if(*(startLocation + 28) == 'N'){
latitudeSign = 1;
}else{
latitudeSign = -1;
}
strncpy(tempBuffer, startLocation+30,3);
tempBuffer[3] = '\0';
int longitudeDegrees = atoi(tempBuffer);
strncpy(tempBuffer, startLocation+33,8);
tempBuffer[6] = '\0';
float longitudeMinutes = atof(tempBuffer);
int longitudeSign;
if(*(startLocation + 40) == 'E'){
longitudeSign = 1;
}else{
longitudeSign = -1;
}
*latitudePointer = (latitudeDegrees + latitudeMinutes/60)*latitudeSign;
*longitudePointer = (longitudeDegrees + longitudeMinutes/60)*longitudeSign;
}
/******************************************************************************************
*** TCP & UDP Protocols Functions ********************************************************
******************************************************************************************/
// function for configurating and activating TCP context
void CellularIoT::activateContext(){
sendATComm("AT+QICSGP=1","OK\r\n");
delay(1000);
sendATComm("AT+QIACT=1","\r\n");
}
// function for deactivating TCP context
void CellularIoT::deactivateContext(){
sendATComm("AT+QIDEACT=1","\r\n");
}
// function for connecting to server via UDP
void CellularIoT::startUDPService(){
char *port = "3005";
strcpy(compose, "AT+QIOPEN=1,1,\"UDP SERVICE\",\"");
strcat(compose, ip_address);
strcat(compose, "\",0,");
strcat(compose, port_number);
strcat(compose, ",0");
sendATComm(compose,"OK\r\n");
clear_compose();
sendATComm("AT+QISTATE=0,1","\r\n");
}
// fuction for sending data via udp.
void CellularIoT::sendDataUDP(const char *data){
char data_len[5];
sprintf(data_len, "%d", strlen(data));
strcpy(compose, "AT+QISEND=1,");
strcat(compose, "100");
strcat(compose, ",\"");
strcat(compose, ip_address);
strcat(compose, "\",");
strcat(compose, port_number);
sendATComm(compose,">");
sendATComm(data,"SEND OK");
clear_compose();
}
void CellularIoT::changeBaudRate(unsigned int rate){
clear_compose();
sprintf(compose, "AT+IPR=%u;&W", rate);
sendATComm(compose,"OK");
BG96_AT.end();
BG96_AT.begin(rate);
}
void CellularIoT::sendDataATT(const char *address, const char *key, const char *field, const char *value ){
timeout = 20000;
char data_len[4];
uint8_t l= strlen(address)+1+strlen(key)+1+strlen(field)+4+strlen(value)+11; //
sprintf(data_len, "%d", l);
clear_compose();
strcpy(compose, "AT+QISEND=1,");
strcat(compose, data_len);
strcat(compose, ",\"");
strcat(compose, ip_address);
strcat(compose, "\",");
strcat(compose, port_number);
sendATComm(compose,">");
strcpy(compose, address);
strcat(compose, "\n");
strcat(compose, key);
strcat(compose, "\n");
BG96_AT.print(compose);
clear_compose();
//sprintf(compose, "{\"%s\":{\"value\":%s}}", field, value);
sprintf(compose, "{\"%s\":{\"value\":%s}}", field, value);
sendDataComm(compose,"SEND OK");
clear_compose();
delay(2000);
}
// function for closing server connection
void CellularIoT::closeConnection(){
sendATComm("AT+QICLOSE=1","\r\n");
}
/******************************************************************************************
*** HTTP Protocols Functions ********************************************************
******************************************************************************************/
// In development
void CellularIoT::getDataHTTP(){
timeout = 20000;
sendATComm("AT+QHTTPCFG=\"contextid\",1","OK\r\n");
sendATComm("AT+QHTTPCFG=\"responseheader\",1","OK\r\n");
sendATComm("AT+QIACT?","OK\r\n");
sendATComm("AT+QICSGP=1,1,\"m2minternet.proximus.be\",\"\",\"\",0","OK\r\n");
sendATComm("AT+QIACT=1","OK\r\n");
sendATComm("AT+QIACT?","OK\r\n");
sendATComm("AT+QIDNSCFG=1,\"8.8.8.8\",\"8.8.4.4\"","OK\r\n");
sendATComm("AT+QHTTPURL=22,80","CONNECT\r\n");
//BG96_AT.print("http://www.google.com/");
sendATComm("http://www.google.com/","OK\r\n");
sendATComm("AT+QHTTPGET=80","OK\r\n");
delay(1000);
sendATComm("AT+QHTTPREAD=80","OK\r\n");
/*
char data_len[4];
uint8_t l= strlen(address)+1+strlen(key)+1+strlen(field)+4+strlen(value)+11; //
sprintf(data_len, "%d", l);
clear_compose();
strcpy(compose, "AT+QISEND=1,");
strcat(compose, data_len);
strcat(compose, ",\"");
strcat(compose, ip_address);
strcat(compose, "\",");
strcat(compose, port_number);
sendATComm(compose,">");
strcpy(compose, address);
strcat(compose, "\n");
strcat(compose, key);
strcat(compose, "\n");
BG96_AT.print(compose);
clear_compose();
//sprintf(compose, "{\"%s\":{\"value\":%s}}", field, value);
sprintf(compose, "{\"%s\":{\"value\":%s}}", field, value);
sendDataComm(compose,"SEND OK");
clear_compose();
delay(2000);*/
}
void CellularIoT::postDataHTTP(){
timeout = 20000;
sendATComm("AT+QHTTPCFG=\"contextid\",1","OK\r\n");
sendATComm("AT+QHTTPCFG=\"responseheader\",1","OK\r\n");
sendATComm("AT+QIACT?","OK\r\n");
sendATComm("AT+QICSGP=1,1,\"m2minternet.proximus.be\",\"\",\"\",0","OK\r\n");
sendATComm("AT+QIACT=1","OK\r\n");
sendATComm("AT+QIACT?","OK\r\n");
sendATComm("AT+QIDNSCFG=1,\"8.8.8.8\",\"8.8.4.4\"","OK\r\n");
sendATComm("AT+QHTTPURL=30,80","CONNECT\r\n");
//BG96_AT.print("http://www.google.com/");
sendATComm("https://postman-echo.com/post/","OK\r\n");
sendATComm("AT+QHTTPPOST=20,80,80","CONNECT\r\n");
sendATComm("Message=HelloQuectel","OK\r\n");
delay(1000);
sendATComm("AT+QHTTPREAD=80","OK\r\n");
/*
char data_len[4];
uint8_t l= strlen(address)+1+strlen(key)+1+strlen(field)+4+strlen(value)+11; //
sprintf(data_len, "%d", l);
clear_compose();
strcpy(compose, "AT+QISEND=1,");
strcat(compose, data_len);
strcat(compose, ",\"");
strcat(compose, ip_address);
strcat(compose, "\",");
strcat(compose, port_number);
sendATComm(compose,">");
strcpy(compose, address);
strcat(compose, "\n");
strcat(compose, key);
strcat(compose, "\n");
BG96_AT.print(compose);
clear_compose();
//sprintf(compose, "{\"%s\":{\"value\":%s}}", field, value);
sprintf(compose, "{\"%s\":{\"value\":%s}}", field, value);
sendDataComm(compose,"SEND OK");
clear_compose();
delay(2000);*/
}