-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSIM7600MQTT.cpp
More file actions
1139 lines (1086 loc) · 34.8 KB
/
SIM7600MQTT.cpp
File metadata and controls
1139 lines (1086 loc) · 34.8 KB
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
SIM7600MQTT
This library only allows sending MQTT messages.
We make no guarantees.
philippe.chaumeil@inrae.fr _ Univ. Bordeaux, INRAE, BIOGECO, F-33610, Cestas, France
*/
#include "SIM7600MQTT.h"
SIM7600MQTT::SIM7600MQTT() {
myserial = &Serial1;
}
void SIM7600MQTT::printSimState() {
Serial.print(F("processMQTT: "));
Serial.print(processMQTT);
Serial.print(F("; sslMQTT: "));
Serial.print(sslMQTT);
Serial.print(F("; beginMQTT: "));
Serial.print(beginMQTT);
Serial.print(F("; closeMQTT: "));
Serial.print(closeMQTT);
Serial.print(F("; msgMQTT: "));
Serial.print(msgMQTT);
Serial.print(F("; netready :"));
Serial.print(networkready);
Serial.print(F("; executeStep:"));
Serial.print(executeStep);
Serial.print(F("; acqRespSIM:"));
Serial.print(acqRespSIM);
Serial.print(F("; beginstep:"));
Serial.print(currentStepBeginMQTT);
Serial.print(F("; closestep:"));
Serial.print(currentStepCloseMQTT);
Serial.print(F("; sslstep:"));
Serial.print(currentStepSslMQTT);
Serial.print(F("; msgstep:"));
Serial.print(currentStepMsgMQTT);
Serial.print(F("; statusError:"));
Serial.print(statusError);
Serial.print(F("; statusOk:"));
Serial.print(statusOk);
Serial.print(F("; statusPlus:"));
Serial.print(statusPlus);
Serial.print(F("; errorCode:"));
Serial.println(errorCode);
}
//####################################### SIM7600 FUNCTIONS ###########################################
void SIM7600MQTT::initSIM7600() {
//____SIM7600____
//----power relay----
pinMode(RELAY_SIM_PIN, OUTPUT);
//----start module----
myserial->begin(115200); //unstable default serial speed of SIM7600 module
Serial.println(F("starting module at 115200..."));
pinMode(12, OUTPUT);
digitalWrite(12, HIGH); //boot sim7600 module
//--- configure SIM7600 ---
serialBuffer[SERIAL_BUFFER_SIZE - 1] = '\0'; //secure read
executeStep = false;
//--- initialize timers ---
netTestTimer = millis();
intervalPubTimer = millis();
//---initialize software rtc ---
DateTime now = rtc_sim7600.now();
if(!now.isValid()){rtc_sim7600.begin(defaultDate);}
rtc_sim7600.adjust(defaultDate);
}
//function to hard reset module switching off power with relay
void SIM7600MQTT::hard_reset_SIM7600(){
if(allow_powerOFF()){
Serial.println(F("--Hard reset Module--"));
lastPowerOnTimer = 0;
digitalWrite(RELAY_SIM_PIN, HIGH);
resetBootState();
}
}
bool SIM7600MQTT::allow_powerOFF(){
if (lastPowerOnTimer == 0){
return false;
} else if((millis() - lastPowerOnTimer) > HARD_RESET_INTERVAL){
return true;
}
return false;
}
//return current processMQTT code status
byte SIM7600MQTT::get_status() {
//#0 not ready to send MQTT #1 ready to send MQTT #2 MQTT msg sent and ready to send new MQTT
if (!networkready) {
return 0;
} else if (processMQTT == 0) {
return 1;
} else if (processMQTT == 2) {
return 2;
} else {
return 0;
}
}
DateTime *SIM7600MQTT::get_gsm_datetime() {
// get current soft RTC datetime
DateTime now = rtc_sim7600.now();
// check if datetime retrieve is default or from gsm provider
if (now.year() == 2080) {
if (gsm_datetime != nullptr) {
delete gsm_datetime; // release old object if necessary
gsm_datetime = nullptr;
}
return nullptr;
}
// update or create DateTime global object
if (gsm_datetime == nullptr) {
gsm_datetime = new DateTime(now); // Allocate only once
} else {
*gsm_datetime = now; // update existing object
}
return gsm_datetime;
}
int SIM7600MQTT::get_utc_offset() {
return UTC_offset;
}
//function to call in main loop to listenSerial
void SIM7600MQTT::listenSerialSIM7600() {
while (myserial->available()) {
int inByte = myserial->read();
serialTimer = millis();
acqRespSIM = 1;
if (bufferIndex >= SERIAL_BUFFER_SIZE) {
bufferIndex = 0;
Serial.println(F("Buffer overflow!"));
} //overflow buffer
if (inByte != -1) {
char inChar = (char)inByte;
if ((inChar == '\n') || (inChar == '\r')) {
serialBuffer[bufferIndex] = '\0'; //adding string termination
if (strlen(serialBuffer) > 0) { scanResponse(serialBuffer); } //response analysis
bufferIndex = 0;
} else {
serialBuffer[bufferIndex] = inChar;
bufferIndex++;
}
//check if wait for entry
if (serialBuffer[0] == '>') {
serialBuffer[bufferIndex] = '\0';
scanResponse(serialBuffer);
bufferIndex = 0;
}
}
}
}
//-- parse response to search patterns --
void SIM7600MQTT::scanResponse(char *buffer) {
bool debug = 1;
char *result;
if (debug) { Serial.print(F("buffer:")); Serial.println(buffer); }
//search "CME" message
result = strstr(buffer, "CME");
if (result != NULL) {
Serial.print(F("buffer:")); Serial.println(buffer);
}
//search ICCID
result = strstr(buffer, "ICCID");
if (result != NULL) {
if (debug) { Serial.print(F("buffer:")); Serial.println(buffer); }
Serial.println(F("SIM card ID response"));
statusError = 0;
errorCode = 0;
}
// search "ERROR"
result = strstr(buffer, "ERROR");
if (result != NULL) {
statusError = 1;
if (debug) { Serial.println(F("found ERROR")); }
}
// search "OK"
result = strstr(buffer, "OK");
if (result != NULL) {
statusOk = 1;
if (debug) { Serial.println(F("found OK")); }
}
//search ">"
result = strstr(buffer, ">");
if (result != NULL) {
statusOk = 1;
if (debug) { Serial.println(F("found >")); }
}
// search response "+XXX"
result = strstr(buffer, "+C");
if (result != NULL) {
if (debug) { Serial.print(F("found motif +C: ")); Serial.println(result); }
//search error code
if (strstr(buffer, "START:") != NULL) {
errorCode = atoi(&result[13]);
statusPlus = 1; //Note: no before because the command is echoed on the serial port
if (errorCode != 0) {
statusError = 1;
Serial.print(F("--scan--")); Serial.print(F("buffer:")); Serial.println(buffer);
Serial.print(F("found start error code: ")); Serial.println(errorCode);
printSimState(); Serial.println(F("--end--"));
}
} else if (strstr(buffer, "STOP:") != NULL) {
errorCode = atoi(&result[12]);
statusPlus = 1;
if (errorCode != 0) {
statusError = 1;
Serial.print(F("--scan--")); Serial.print(F("buffer:")); Serial.println(buffer);
Serial.print(F("found stop error code: ")); Serial.println(errorCode);
printSimState(); Serial.println(F("--end--"));
}
} else if (strstr(buffer, "REG:") != NULL) {
errorCode = atoi(&result[9]); //not an errorcode but a stat index
statusPlus = 1;
checkNetwork = millis();
switch (errorCode) {
case 0:
Serial.print(F("--scan--")); Serial.print(F("buffer:")); Serial.println(buffer);
Serial.println(F("Not registered, not searching operator!"));
printSimState();
Serial.println(F("--end--"));
statusError = 1;
errorCode = 99;
break;
case 1:
statusError = 0;
errorCode = 0;
break;
case 2:
Serial.println(F("Not yet registered, searching operator..."));
statusError = 1;
errorCode = 99;
break;
case 5:
Serial.print(F("--scan--")); Serial.print(F("buffer:")); Serial.println(buffer);
Serial.print(F("Registered, roaming!")); printSimState();
Serial.println(F("--end--"));
statusError = 0;
errorCode = 0;
break;
default:
Serial.print(F("--scan--")); Serial.print(F("buffer:")); Serial.println(buffer);
Serial.print(F("Registration pb!")); printSimState();
Serial.println(F("--end--"));
statusError = 1;
errorCode = 99;
break;
}
} else if (strstr(buffer, "CCLK:") != NULL) {
//parse DateTime returned by SIM7600
int Year, Month, Day, Hour, Minute, Second, Timezone;
sscanf(buffer, "+CCLK: \"%d/%d/%d,%d:%d:%d%d\"", &Year, &Month, &Day, &Hour, &Minute, &Second, &Timezone);
//default date if no date supplied by gsm operator is 80/xxx
DateTime sim7600_date = DateTime(Year, Month, Day, Hour, Minute, Second);
rtc_sim7600.adjust(sim7600_date);
if (Year != 80) {
UTC_offset = Timezone / 4; //sim7600 return timezone in quarter of hour
}
Serial.print(F("-- SIM7600 soft RTC updated with -- ")); Serial.println(buffer);
} else if (strstr(buffer, "NTP:") != NULL) {
errorCode = atoi(&result[7]);
statusPlus = 1;
if (errorCode != 0) {
statusError = 0; //force not blocking ntp status code
Serial.print(F("--scan--")); Serial.print(F("buffer:")); Serial.println(buffer);
Serial.print(F("NTP failed code: ")); Serial.println(errorCode); Serial.println(F("--end--"));
} else {
Serial.println(F("-- NTP request passed --"));
resetAtState();
soloMQTT = true;
currentStepSoloMQTT = 1;
periodicStep(currentStepSoloMQTT);
last_nettime_request = millis();
}
} else if (strstr(buffer, ": 0,") != NULL) {
result = strstr(buffer, ": 0,");
errorCode = atoi(&result[4]);
statusPlus = 1;
if (errorCode != 0) {
statusError = 1;
Serial.print(F("--scan--")); Serial.print(F("buffer:")); Serial.println(buffer);
Serial.print(F("found error code: ")); Serial.println(errorCode);
printSimState(); Serial.println(F("--end--"));
}
}
}
}
//set module to max stable speed with arduino uno
void SIM7600MQTT::setSerialSpeed() {
Serial.println(F("--setSerialSpeed 57600--"));
myserial->println(F("AT+IPR=57600"));
delay(100);
myserial->end();
myserial->begin(57600);
delay(250);
}
//check timeout and completeness responses return false if problem
bool SIM7600MQTT::dialogCheck() {
//Serial.println("--dialogCheck--");
// response times are too long
if (requestTimer != 0 && SIM7600ready) {
if ((millis() - requestTimer) > requestTimerLimit) {
Serial.println(F("No resp to request"));
resetBootState();
return false;
}
}
// Serial port response considered complete if acqRespSIM #2. acqRespSIM #1 means that data has already been detected on the serial port.
if (serialTimer != 0 && acqRespSIM == 1) {
// Serial.print("--check serialTimer--");
// Serial.println(millis() - serialTimer);
//step at+connect must receive both responses
if (beginMQTT && currentStepBeginMQTT == 4) {
if ((statusOk || statusError) && statusPlus) {
acqRespSIM = 2;
requestTimer = 0;
serialTimer = 0;
// Serial.println(F("--serial complete #--"));
}
} else if (closeMQTT && (currentStepCloseMQTT <= 1)) {
if ((statusOk || statusError) && statusPlus) {
acqRespSIM = 2;
requestTimer = 0;
serialTimer = 0;
// Serial.println(F("--serial complete #--"));
}
} else if (closeMQTT && (currentStepCloseMQTT == 2)) {
if (statusOk || statusError) {
acqRespSIM = 2;
requestTimer = 0;
serialTimer = 0;
// Serial.println(F("--serial complete #--"));
}
} else if (soloMQTT && (currentStepSoloMQTT == 0)) {
if (statusOk && statusPlus) {
acqRespSIM = 2;
requestTimer = 0;
serialTimer = 0;
}
//other command based on duration
} else if ((millis() - serialTimer) > MAX_SERIAL_TIMER) {
acqRespSIM = 2;
requestTimer = 0;
serialTimer = 0;
serialBuffer[bufferIndex] = '\0'; //adding string termination
// Serial.println(F("--serial complete--"));
}
}
return true;
}
void SIM7600MQTT::resetStatus() {
acqRespSIM = 0;
statusError = 0;
statusOk = 0;
statusPlus = 0;
errorCode = -1;
bufferIndex = 0;
}
void SIM7600MQTT::resetStep() {
currentStepSettings = 0;
currentStepBeginMQTT = 0;
currentStepCloseMQTT = 0;
currentStepSslMQTT = 0;
currentStepMsgMQTT = 0;
currentStepSoloMQTT = 0;
executeStep = 0;
onlyGetGsmDate = 0;
}
void SIM7600MQTT::resetAtState() {
sslMQTT = false;
beginMQTT = false;
closeMQTT = false;
msgMQTT = false;
soloMQTT = false;
}
void SIM7600MQTT::resetTimers() {
netTestTimer = 0;
serialTimer = 0;
requestTimer = 0;
timeoutTimer = 0;
retryTimer = 0;
checkNetwork = 0;
last_nettime_request = 0;
}
// close current connexion
void SIM7600MQTT::resetCnx() {
resetStatus();
resetStep();
resetAtState();
resetTimers();
bufferIndex = 0;
processMQTT = 1; //TODO to check
closeMQTT = true;
currentStepCloseMQTT = 1;
executeStep = true;
}
//reset to Boot conditions
void SIM7600MQTT::resetBootState() {
Serial.println(F("--- Reset to Boot State ---"));
resetStatus();
resetStep();
resetAtState();
resetTimers();
bufferIndex = 0;
SIM7600ready = false;
SIMready = false;
networkready = false;
processMQTT = 0;
myserial->println(F("AT+CRESET"));
myserial->end();
initSIM7600();
netTestTimer = millis();
}
//reset to search network conditions
void SIM7600MQTT::resetSearchNetState() {
resetStatus();
resetStep();
resetAtState();
resetTimers();
bufferIndex = 0;
SIM7600ready = true;
networkready = false; // SIMready = false;
processMQTT = 0;
beginMQTT = true;
executeStep = true;
netTestTimer = millis();
}
//-- management / startup verification SIM7600 --
void SIM7600MQTT::startupSIM() {
bool debug = 0;
static unsigned long tempo_HW_start = 0;
static bool attente = false;
if (!SIM7600ready) {
//relay state to power on
if (lastPowerOnTimer == 0){
if(!attente) {
tempo_HW_start = millis();
attente = true;
} else {
if (millis() - tempo_HW_start >= 3000) {
digitalWrite(RELAY_SIM_PIN, LOW);
lastPowerOnTimer = millis();
attente = false;
}
}
}
if (acqRespSIM == 2 && statusOk) { // response management
//Serial.println(F("--acqRespSIM=2 & statusOk--"));
if (currentStepSettings == 0) { //first item
setSerialSpeed(); //switch to 57600bps
waitingTry = 0;
currentStepSettings++;
settingsStep(currentStepSettings);
} else if (currentStepSettings == 3) { //last item
SIM7600ready = 1;
Serial.println(F("--- Last settingStep ---"));
} else {
currentStepSettings++;
settingsStep(currentStepSettings);
}
acqRespSIM = 0;
if (debug) { Serial.print("--startupSIM--"); printSimState();}
} else if (currentStepSettings == 0 && millis() - netTestTimer > NET_TEST_TIMER_DELTA) { // test module every x sec
waitingTry++;
if (waitingTry == 24) { //If there is no response, it's possible that the Arduino was rebooted, but not the module which is listening for 57600.
Serial.print(F("## Try 57600bps ##"));
setSerialSpeed();
}
if (waitingTry > 48) {
initSIM7600();
waitingTry = 0;
}
Serial.print(F("##waitingTry:"));
Serial.println(waitingTry);
resetStatus();
processMQTT = 0;
Serial.println(F("waiting SIM7600 ready..."));
settingsStep(currentStepSettings);
netTestTimer = millis();
if (debug) { Serial.print(F("--startupSIM--")); printSimState(); }
}
}
}
void SIM7600MQTT::checkSIM() {
bool debug = 1;
if (SIM7600ready && !SIMready) {
if (acqRespSIM == 2 && statusOk && errorCode == 0) { // reponse management
SIMready = 1;
resetStatus();
netTestTimer = 0;
Serial.println(F("SIM card detected"));
if (debug) { Serial.print("--checkSIM--"); printSimState(); }
} else if (millis() - netTestTimer > NET_TEST_TIMER_DELTA) { // test module every x sec
resetStatus();
processMQTT = 0;
Serial.println(F("checking SIM..."));
myserial->println("AT+CICCID");
netTestTimer = millis();
if (debug) { Serial.print(F("--checking SIM--")); printSimState(); }
}
}
}
//-- network acquisition --
void SIM7600MQTT::networkSearch() {
if (SIM7600ready && SIMready) {
if (!networkready) {
processMQTT = 0;
// Serial.print(F("--networkSearch-- ")); printSimState();
// -- réponse netstatus --
if (acqRespSIM == 2 && statusOk && errorCode == 0) {
resetStatus();
netTestTimer = 0;
networkready = 1;
Serial.println(F("Network connected..."));
checkNetwork = millis();
//launch get gsm date
onlyGetGsmDate = true;
processMQTT = 1;
beginMQTT = true;
currentStepBeginMQTT++;
executeStep = true;
} else if (millis() - netTestTimer > NET_TEST_TIMER_DELTA) { //test network every x sec
Serial.println(F("--networkSearch-- "));
resetSearchNetState();
}
} else if (onlyGetGsmDate && acqRespSIM == 2) {
resetStatus();
onlyGetGsmDate = false;
processMQTT = 0;
} else if (processMQTT == 0 && (millis() - checkNetwork > CHECK_NETWORK)) {
resetSearchNetState();
checkNetwork = millis();
}
}
}
//prepare and format AT command for SSL steps
bool SIM7600MQTT::settingsStep(byte step) {
bool debug = 1;
requestTimer = 0;
requestTimerLimit = MAX_REQUEST_TIMER;
size_t buffer_size = 128;
if (step > 3) { return false; Serial.println(F("Err#overTAB")); }
char buffer[buffer_size];
processMQTT = 1;
strcpy_P(buffer, (char *)pgm_read_ptr(&(AT_settings_cmd[step])));
if(debug){Serial.print(F("--- launch settingStep : ")); Serial.println(step); }
myserial->println(buffer);
requestTimer = millis();
return true;
}
bool SIM7600MQTT::periodicStep(byte step){
bool debug = 1;
requestTimer = 0;
requestTimerLimit = MAX_REQUEST_TIMER;
size_t buffer_size = 128;
if (step > 1) { return false; Serial.println(F("Err#overTAB")); }
if (step == 0) { requestTimerLimit = 10000; } //to obtain response frome NTP server
char buffer[buffer_size];
processMQTT = 1;
strcpy_P(buffer, (char *)pgm_read_ptr(&(AT_periodic_cmd[step])));
if(debug){Serial.print(F("--- launch periodicStep : ")); Serial.println(step); }
myserial->println(buffer);
requestTimer = millis();
return true;
}
//prepare and format AT command for begin steps
bool SIM7600MQTT::beginAtMQTT(byte step) {
bool debug = 1;
requestTimer = 0;
size_t buffer_size = 128;
if (step > 4) { return false; Serial.println(F("Err#overTAB")); }
char buffer[buffer_size];
char bufout[buffer_size];
if (debug) { Serial.print("#stepBegin:"); Serial.println(step); }
processMQTT = 1;
switch (step) {
case 3:
requestTimerLimit = MAX_REQUEST_TIMER;
strcpy_P(buffer, (char *)pgm_read_ptr(&(AT_beginMQTT_cmd[step])));
snprintf(bufout, buffer_size, buffer, CLIENT_ID, SSLMODE);
myserial->println(bufout);
requestTimer = millis();
if (debug) { Serial.print("launching:"); Serial.println(bufout); }
break;
case 4:
requestTimerLimit = 120000;
strcpy_P(buffer, (char *)pgm_read_ptr(&(AT_beginMQTT_cmd[step])));
snprintf(bufout, buffer_size, buffer, SERVER_URL, PORT, LOGIN, PASSWD);
myserial->println(bufout);
requestTimer = millis();
if (debug) { Serial.print("launching:"); Serial.println(bufout); }
break;
default:
requestTimerLimit = MAX_REQUEST_TIMER;
strcpy_P(buffer, (char *)pgm_read_ptr(&(AT_beginMQTT_cmd[step])));
myserial->println(buffer);
requestTimer = millis();
if (debug) { Serial.print("launching:"); Serial.println(buffer); }
break;
}
return true;
}
//prepare and format AT command for close steps
bool SIM7600MQTT::closeAtMQTT(byte step) {
size_t buffer_size = 128;
bool debug = 1;
requestTimer = 0;
if (step > 3) { return false; Serial.println(F("Err#overTAB")); }
if (step < 3) {
requestTimerLimit = 120000;
} else {
requestTimerLimit = MAX_REQUEST_TIMER;
}
char buffer[buffer_size];
processMQTT = 1;
if (debug) { Serial.print("StepClose:"); Serial.println(step); }
strcpy_P(buffer, (char *)pgm_read_ptr(&(AT_closeMQTT_cmd[step])));
myserial->println(buffer);
requestTimer = millis();
return true;
}
//prepare and format AT command for SSL steps
bool SIM7600MQTT::sslAtMQTT(byte step) {
bool debug = 1;
requestTimer = 0;
requestTimerLimit = MAX_REQUEST_TIMER;
size_t buffer_size = 128;
if (step > 5) { return false; Serial.println(F("Err#overTAB")); }
char buffer[buffer_size];
processMQTT = 1;
if (debug) { Serial.print("#stepSSL:"); Serial.println(step); }
strcpy_P(buffer, (char *)pgm_read_ptr(&(AT_sslMQTT_cmd[step])));
myserial->println(buffer);
requestTimer = millis();
return true;
}
//MQTT sending sequence trigger management
byte SIM7600MQTT::publishMQTT(char *topic, char *payload) {
bool debug = 0;
//process status of current MQTT action #0 nothing running #1 running MQTT request #2 finished & success #3 failed to process MQTT request #4 Must relaunch process
mqttTopic = topic;
mqttPayload = payload;
if (debug) {
Serial.println(F("--launch publishMQTT--"));
Serial.print(F("#topic: "));
Serial.println(mqttTopic);
Serial.print(F("#payload: "));
Serial.println(mqttPayload);
}
if (strlen(payload) == 0) {
Serial.println(F("--empty payload!--"));
} else if (networkready && (processMQTT == 0 || processMQTT == 2) && strlen(payload) != 0) {
//launching an MQTT sending process
if (debug) { Serial.println(F("--init publish state--")); }
resetStatus();
resetStep();
resetAtState();
resetTimers();
bufferIndex = 0;
processMQTT = 1; //#processMQTT running
beginMQTT = true;
executeStep = true;
timeoutTimer = millis();
} else if (networkready && processMQTT == 1 && timeoutTimer != 0 && (millis() - timeoutTimer) > TIMEOUT_SESSION) {
//Stuck in an incomplete sending state (may be due to insufficient time between two publishes)
Serial.println(F("--#publishMQTT error process running--"));
if (debug) { Serial.print("##"); printSimState(); }
processMQTT = 3;
}
if (debug) { Serial.print("###"); printSimState(); }
return processMQTT;
}
//send msg payload to topic
bool SIM7600MQTT::sendMsgMQTT(char *topic, char *payload, byte step) {
if (msgMQTT) {
int topicLength = strlen(topic);
int payloadLength = strlen(payload);
// Serial.print("stepMSG:");Serial.println(step);
processMQTT = 1;
requestTimer = 0;
switch (step) {
case 1:
requestTimerLimit = MAX_REQUEST_TIMER;
myserial->print(F("AT+CMQTTTOPIC=0,"));
myserial->println(topicLength);
requestTimer = millis();
break;
case 2:
requestTimerLimit = MAX_REQUEST_TIMER;
myserial->println(topic);
requestTimer = millis();
break;
case 3:
requestTimerLimit = MAX_REQUEST_TIMER;
myserial->print(F("AT+CMQTTPAYLOAD=0,"));
myserial->println(payloadLength);
requestTimer = millis();
break;
case 4:
requestTimerLimit = MAX_REQUEST_TIMER;
myserial->println(payload);
requestTimer = millis();
break;
default:
//in case no valid step
requestTimer = millis();
break;
}
}
return true;
}
//Launching AT commands for MQTT connection: parsing the previous command message and launching the next command
bool SIM7600MQTT::launchAtCmdMQTT() {
bool debug = 0;
// inconsistency check
int incCpt = 0;
if (sslMQTT) { incCpt++; }
if (beginMQTT) { incCpt++; }
if (closeMQTT) { incCpt++; }
if (msgMQTT) { incCpt++; }
if (soloMQTT) { incCpt++; }
if (incCpt > 1) {
Serial.println(F("incoherence!"));
processMQTT = 3;
return false;
}
//periodic command to launch when not busy
if (networkready && processMQTT == 0){
#if SYNC_NTP_DATETIME == 1
unsigned long mylimit = GET_NET_TIME_PERIOD;
DateTime* currentDateTime = get_gsm_datetime();
if (currentDateTime == nullptr) {mylimit = 300000;} //shortens the delay if there is no time synchronization
if(millis() - last_nettime_request > mylimit){
resetAtState();
soloMQTT = true;
currentStepSoloMQTT = 0;
periodicStep(currentStepSoloMQTT);
last_nettime_request = millis();
return true;
}
#endif
}
if (processMQTT == 2) {
Serial.println(F("--message envoyé--"));
//Note: This function works with buffer stack because loop processes the MQTT process between two calls to this function
processMQTT = 0;
return true;
}
//verification status process in progress
if (processMQTT == 3) {
if (retryTimer == 0) {
Serial.println(F("--MQTT seq failed to process!--"));
Serial.print(F("--Wait for : "));
Serial.print(ERROR_TIMER_DELAY);
Serial.println("ms --");
retryTimer = millis();
processMQTT = 3; //to block executestep
return false;
} else if ((millis() - retryTimer) > ERROR_TIMER_DELAY) {
Serial.println(F("--Try re-launch MQTT seq.--"));
//TODO finir modif !
resetBootState();
//publishMQTT(mqttTopic, mqttPayload);
return true;
}
return false;
}
if (processMQTT == 4) { //Logical process and network management available...TODO
if (retryTimer == 0) {
Serial.println(F("--MQTT cmd failed to process!--"));
retryTimer = millis();
} else if ((millis() - retryTimer) > RETRY_TIMER_DELAY) {
Serial.println(F("--Try re-launch MQTT cmd.--"));
resetStatus();
processMQTT = 1;
executeStep = true;
retryTimer = 0;
}
return true;
}
//parse common error code requiring relaunch
if (errorCode == 14) {
//client is busy
processMQTT = 4;
Serial.println(F("--client is busy, relaunch!--"));
return true;
}
//received response SIM7600
if (networkready && acqRespSIM == 2 && processMQTT == 1) {
if (debug) { Serial.print("--launchAtCmdMQTT--"); printSimState(); }
// ---- commands SSL ----
if (sslMQTT) {
if (errorCode > 0) { statusError = true; }
//switch step beginMQTT
if (statusOk && !statusError && currentStepSslMQTT == 4) {
resetStatus();
bufferIndex = 0;
sslMQTT = false;
beginMQTT = true;
currentStepBeginMQTT++;
executeStep = true;
} else if (statusOk && !statusError && currentStepSslMQTT == 5) {
resetStatus();
bufferIndex = 0;
sslMQTT = false;
beginMQTT = true;
currentStepBeginMQTT++;
executeStep = true;
}
//launch AT cmd
else if (statusOk && !statusError && currentStepSslMQTT < 6) {
if (debug) { Serial.println("--lanch ssl step--"); }
currentStepSslMQTT++;
if (sslAtMQTT(currentStepSslMQTT) == false) {
sslMQTT = false;
beginMQTT = false;
currentStepSslMQTT = 0;
processMQTT = 3;
return false;
}
resetStatus();
bufferIndex = 0;
} else {
//error
Serial.println(F("--#sslMQTT step error!--"));
printSimState();
statusOk = 0;
statusError = 1;
statusPlus = 0;
processMQTT = 3;
return false;
}
//TODO manage statusError
} else if (beginMQTT) {
//manage error cmd AT MQTT
switch (errorCode) {
case -1: //no error code detected
break;
case 0: //all is OK
statusOk = 1;
statusError = 0;
break;
case 1: //failed
statusOk = 0;
statusError = 1;
processMQTT = 3;
return false;
Serial.println(F("--E: Failed!--"));
break;
case 7: //network open failed
Serial.println(F("--E: Network open failed!--"));
statusOk = 0;
statusError = 1;
processMQTT = 3;
return false;
break;
case 9: //network not opened
Serial.println(F("--E: Network not opened!--"));
statusOk = 0;
statusError = 1;
processMQTT = 3;
return false;
break;
case 11:
Serial.println(F("--E: no connection!--"));
statusOk = 0;
statusError = 1;
processMQTT = 3;
return false;
break;
case 23: //network is opened => non-blocking next step but signs of a problem in the sequence
Serial.println(F("--E: network is opened!--"));
statusOk = 0;
statusError = 1;
processMQTT = 3;
return false;
break;
case 19: //client is used => non-blocking next step but signs of a problem in the sequence
Serial.println(F("--E: client is used!--"));
statusOk = 0;
statusError = 1;
processMQTT = 3;
return false;
break;
case 26: //socket closed by server => echec connection
Serial.println(F("--E: Server refused cnx!--"));
statusOk = 0;
statusError = 1;
processMQTT = 3;
return false;
break;
case 32: //handshake error
Serial.println(F("--E: Hanshake fail!--"));
//close and finish
statusOk = 0;
statusError = 1;
processMQTT = 3;
return false;
break;
case 99: //personal error code no relaunch mqtt seq
Serial.println(F("--E: Perso MQTT 99!--"));
statusOk = 0;
statusError = 1;
processMQTT = 3;
return false;
break;
default:
Serial.println(F("--E: ErrorMQTT!--"));
statusOk = 0;
statusError = 1;
processMQTT = 3;
return false;
break;
}
//switch step ssl if required
if (statusOk && !statusError && currentStepBeginMQTT == 1 && SSLMODE) {
resetStatus();
sslMQTT = true;
beginMQTT = false;
closeMQTT = false;
currentStepSslMQTT = 0;
executeStep = true;
if (debug) { Serial.println(F("Preparing SSL")); }
return true;
}
//switch step ssl if required
if (statusOk && !statusError && currentStepBeginMQTT == 3 && SSLMODE) {
beginMQTT = false;
sslMQTT = true;
currentStepSslMQTT++;
executeStep = true;
resetStatus();
bufferIndex = 0;
if (debug) { Serial.print("--switch-- "); printSimState(); }
}
//switch step topic & payload
if (statusOk && !statusError && currentStepBeginMQTT == 4) {
beginMQTT = false;
msgMQTT = true;
currentStepMsgMQTT++;
executeStep = true;
resetStatus();
bufferIndex = 0;
if (debug) { Serial.print(F("--init payload-- ")); printSimState(); }
}
//launch AT cmd
if (statusOk == true && !statusError && beginMQTT) {
// Serial.println("--lanch begin step--");
currentStepBeginMQTT++;
if (currentStepBeginMQTT <= 4) {
if (beginAtMQTT(currentStepBeginMQTT) == false) {
beginMQTT = false;
closeMQTT = false;
currentStepBeginMQTT = 0;