-
Notifications
You must be signed in to change notification settings - Fork 0
/
2SP_ESP32_ENTRY_WITHOUT_BUSY_STATE.ino
1576 lines (1232 loc) · 35.6 KB
/
2SP_ESP32_ENTRY_WITHOUT_BUSY_STATE.ino
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
//{"gid":"1","mid":"1","sc":"0000000010110011","at":"I","rid":"1234567876","dt":"10092021","st":"00:00","et":"23:00"}
/*
* INCLUDE NTP CLIENT LIBRARY
* IF YOU ARE UPLOADING THE CODE FOR FIRST TIME,DIRECTLY UPLOAD THE CODE WITHOUT ANY CHANGES AND send the commands below and press reset
*
*
*/
/*
********COMMANDS NEED TO BE SEND TO SERIAL PORT FOR CONFIGURING*******************
erase // to erase the eeprom and spiffs if any unwanted values are there,this is done when first time flashing code to esp32 or for explicitly erasing
{"ssid":"stijo"} //your wifi ssid
{"password":"stijojoseph"} //your wifi password
{"devid":"0001"} //set the device id
{"gymid":"1"} //set the gymid
{"serid":"0000000010110011"} //set the service id
{"clockset":"16;20;23;05;07;21"} //set the clock manually
inclock //set the internet clock to rtc
clock //printout current rtc clock datas
load //printout the ssid,password,deviceid,gymid,servicecode stored in eeprom
********************************************************************************************************************
*
*AFTER SETTING UP ALL PARAMETERS PRESS RESET
*/
//FORMAT-{"gid":"1","mid":"1","sc":"0000000010110011","at":"I","rid":"1234567876","dt":"DDMMYY","st":"HH:MM","et":"HH:MM"}
//EXAMPLE BELOW OF INPUT JSON FORMAT MAKE SURE YOU ENTER THE CURRENT DAY'S DATE .THE START TIME AND END TIME SHOULD BE BETWEEN YOUR CURRENT TIME ELSE PRINTOUT WILL BE INVALID QRCODE
//{"gid":"1","mid":"1","sc":"0000000010110011","at":"I","rid":"1234567876","dt":"08072021","st":"08:00","et":"20:00"}
//{"gid":"1","mid":"1","sc":"0000000010110011","at":"I","rid":"1234567876","dt":"09092021","st":"13:00","et":"20:00"} //wrong date wrong start time end time simulation -invalid qrcode
//{"gid":"1","mid":"1","sc":"0000000010110011","at":"I","rid":"1234567876","dt":"09092021","st":"13:00","et":"20:00"} //JSON PACKET TO BE SENT OUTPUT -VALID QRCODE
//{"gid":"12","mid":"1","sc":"0000000010110011","at":"I","rid":"1234567876","dt":"09092021","st":"13:00","et":"20:00"} //gymid error SIMULATION JSON PACKET TO BE SENT OUTPUT -INVALID QRCODE
//{"gid":"1","mid":"1","sc":"0000000000110011","at":"I","rid":"1234567876","dt":"09092021","st":"13:00","et":"20:00"} //sevice code error SIMULATION JSON PACKET TO BE SENT OUTPUT -INVALID QRCODE
//Storage data format on spiffs
//P*{status}{memberid}{time date}*
//P*{not allowed timeerror}{1}{8:59:4 8/7/21}*
#define FORMAT_SPIFFS_IF_FAILED true
char inByte;
int count=0;
#include <WiFi.h>
#include <ESP32Ping.h> //edit
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include "EEPROM.h"
#include "uRTCLib.h"
#include <NTPClient.h>
#include <WiFiUdp.h>
#include "FS.h"
#include "SPIFFS.h"
String QRCode = "";
#define DOOR_OPEN_TIME 1 //SECONDS
#define INTERNET_CHECK_TIMEOUT 2
#define ONLINE 1
#define OFFLINE 0
#define WIFI_CHECK 12 //SECONDS
#define INTERNET_CHECK 8 //SECONDS
#define DATA_COUNT 2 //NUMBER OF DATAS TO BE SENT IN BULK
#define READY 1
#define NOT_READY 0
#define EEPROM_SIZE 140
#define SSID_ADDR 0
#define PASSWORD_ADDR 30
#define DEVID_ADDR 60
#define GYMID_ADDR 90
#define SERVICE_ADDR 110
#define DEVID_SIZE 10
#define GYMID_SIZE 10
#define PSWD_SIZE 30
#define SSID_SIZE 30
#define LED1 32
#define LED2 33
#define LED3 25
#define RELAY 15
#define ONLINE 1
#define OFFLINE 0
#define LOG_NAME1 "/Validlog.txt"
#define LOG_NAME2 "/Validlog2.txt"
String log_file_name;
int first_send();
int data_conversion(char *datas);
/*Function to chnage the data storage file
* if 1 time we use "/Validlog.txt" next time after uploading data to web "/Validlog2.txt" is created then "/Validlog.txt" is deleted
* vicerversa if we use "/Validlog2.txt" we create "/Validlog.txt"
* this function returns out 1 or 2 value which indeed decides which file need to be deleted or created
*
* checking at the beggining if "/Validlog.txt" or "/Validlog2.txt" files exits and if a==0
* allocating current file
if file exists it means the device is not intialized first time,and this fucntion is to change the data log writting to a
new file,so to decrease future memory related issues,if a==1
*/
int file_check(int a)
{
int found=0;
File root = SPIFFS.open("/");
File file = root.openNextFile();
String file_name;
while(file){
Serial.print("FILE: ");
file_name=file.name();
Serial.println(file_name);
if(file_name==LOG_NAME1)
{
//a=0 means device restart occured so filename need to updated to the system
if(a==0)
log_file_name=LOG_NAME1;
else
log_file_name=LOG_NAME2;
return 1;
}
else
if(file_name==LOG_NAME2)
{
if(a==0)
log_file_name=LOG_NAME2;
else
log_file_name=LOG_NAME1;
return 2;
}
file = root.openNextFile();
}
//this happens only one time when you erase the flash new file will be created upon restaring for the first time
CreateFile(SPIFFS, LOG_NAME1);
Serial.print("File Created");
log_file_name=LOG_NAME1;
return 0;
}
int extract(int a,int b)
{
int j=((QRCode[a]-'0')*10+(QRCode[b]-'0'));
return j;
}
void wifi_status_led(int sta)
{
if(sta==1){
device_ready_status_led(1);
}
else
{
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,HIGH);
}
}
void internet_status_led(int sta)
{
if(sta==1){
device_ready_status_led(1);
}
else
{
digitalWrite(LED1,LOW);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,LOW);
}
}
void door_status_led(int sta)
{
if(sta==1){
digitalWrite(LED1,LOW);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,LOW);
}
else
{
digitalWrite(LED1,LOW);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,HIGH);
}
}
void device_ready_status_led(int sta)
{
if(sta==1){
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,HIGH);
}
else
{
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
}
}
int WIFI_STATUS=OFFLINE; //STATUS OF WIFI BH DEFAULT OFFLINE
long int wifi_status1=0;
int internet_status=0;
int DEVICE_STATUS=0;
int totalInterruptCounter;
volatile int interruptCounter;
hw_timer_t * timer = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
int totalInterruptCounter2;
volatile int interruptCounter2;
hw_timer_t * timer2 = NULL;
portMUX_TYPE timerMux2 = portMUX_INITIALIZER_UNLOCKED;
uRTCLib rtc;
HTTPClient http;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "asia.pool.ntp.org");
String mygym = "";
String myuserid = "";
String myusertype = "";
String mydeviceid = "";
String qrdatetime;
String qrservicecode;
String qrremoteid;
String qrgymid;
String myserviceid="";
String qrusertype;
String mydatetime;
String qruserid;
String qraccesstype;
String start_time;
String end_time;
int INTERNET_STATUS=0;
String ssid = "";
String password = "";
String current_date_time="";
/*
const char* ssid = "iot"; // your network SSID (name of wifi network)
const char* password = "iotdemo1"; // your network password
*/
//"https://admin.gymshim.com/api/v2/turnstile/user_access?qr_code_number=GQR131220173586465_G996788716&tripod_uniq_id=32TID1"
//String UrlStart = "https://admin.gymshim.com/api/v2/turnstile/user_access?qr_code_number=";
//String UrlEnd = "&tripod_uniq_id="; // String UrlEnd = "&tripod_uniq_id=80TID1";
//String HealthStatusQRcode = "GQR131220173586465_G996788716";
int wifi_count=0;
void (*reset) (void) = 0;
char *str_to_char(String strng);
char *str_to_char(String strng)
{
int len=strng.length()+1;
char car[len];
int i=0;
strcpy(car,strng.c_str());
//Serial.println(car);
return car;
}
/*
* TIMER1 INTERUPT FOR WIFI STATUS CHECKING
*/
void IRAM_ATTR internet_timer() {
portENTER_CRITICAL_ISR(&timerMux);
wifi_status1=1; //setting to 1 to check wifi connection in the superloop
portEXIT_CRITICAL_ISR(&timerMux);
}
/*
* TIMER2 INTERUPT FOR INTERNET STATUS CHECKING
*/
void IRAM_ATTR wifi_timer() {
portENTER_CRITICAL_ISR(&timerMux2);
internet_status=1; //setting to 1 to check internet connection in the superloop
portEXIT_CRITICAL_ISR(&timerMux2);
}
/*
* TO CHECK THE ENTIRES CREDENTIAL ARE CORRECT LEDS ARE GIVEN AS STATUS OUTPUT
*/
void verifyString(JsonDocument& doc) {
int len=QRCode.length()+1;
char delimArr[len];
int i=0;
Serial.print(str_to_char(QRCode));
/* DeserializationError error = deserializeJson(doc,QRCode);
// Test if parsing succeeds.
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
*/
//TAKING OUT THE REQUIRED DATAS FROM JSON DATA
String qrgymid1=doc["gid"];
String qruserid1 = doc["mid"];
String qraccesstype1 =doc["at"];
String qrdatetime1=doc["dt"];
String start_time1=doc["st"];
String end_time1=doc["et"];
String qrservicecode1=doc["sc"];
String qrremoteid1=doc["rid"];
qrgymid=qrgymid1;
qrdatetime=qrdatetime1;
qruserid =qruserid1;
qraccesstype =qraccesstype1;
qrdatetime=qrdatetime1;
start_time=start_time1;
end_time=end_time1;
qrremoteid=qrremoteid1;
qrservicecode=qrservicecode1;
Serial.println(qruserid);
Serial.println(qraccesstype);
Serial.println(qrdatetime);
Serial.println(qrservicecode);
Serial.println(qrremoteid);
Serial.println(start_time);
Serial.println(end_time);
//TAKING AND COVERTING THE DATE,START TIME AND AND END TIME TO INTEGER FOR LATER COMPARISON WITH RTC TIMER
String min_string1=(String)start_time[3] +(String) start_time[4];
int hr=(int)start_time.toInt();
int mns=(int)min_string1.toInt();
String min_string=(String)end_time[3] +(String) end_time[4];
int hr1=(int)end_time.toInt();
int mns1=(int)min_string.toInt();
String date_in=(String)qrdatetime[0]+(String)qrdatetime[1];
String month_in=(String)qrdatetime[2]+(String)qrdatetime[3];
String year_in=(String)qrdatetime[4]+(String)qrdatetime[5]+(String)qrdatetime[6]+(String)qrdatetime[7];
int date_int=(int)date_in.toInt();
int month_int=(int)month_in.toInt();
int year_int=(int)year_in.toInt()-2000;
rtc.refresh();
int now_hr=rtc.hour();
int now_min=rtc.minute();
int now_sec=rtc.second();
int now_day=rtc.day();
int now_month=rtc.month();
int now_year=rtc.year();
//ACESS STATUS VARIABLE
int access=1;
current_date_time=(String)now_hr+":"+(String)now_min+":"+(String)now_sec+" "+(String)now_day+"/"+(String)now_month+"/"+(String)now_year;
//CHECKING WHEATHER THE INOMMING GYMID EQUALS STORED GYMID OR ATLEAST WITH REMOTE ID IF YES STATUS UPDATED
if((mygym.equals(qrgymid)||(mygym.equals(qrremoteid))))
{
//TAKING OUT CURRENT RTC DATE AND TIME
int now_tot=now_hr*100+now_min;
int st_tot=hr*100+mns;
int en_tot=hr1*100+mns1;
//CHECKING DATE SAME OR NOT
if(date_int==now_day && month_int==now_month && year_int==now_year)
{
// Serial.println("date same");
access=1;
}
else
{
access=0;
qraccesstype="not allowed dateerror"; //UPDATE DATE ERROR AS STATUS
}
if(now_tot>st_tot && now_tot<en_tot && access==1)
{
access=1;
}
else
{access=0;
qraccesstype="not allowed timeerror"; //UPDATE TIME ERROR AS STATUS
}
if(qrservicecode.length()<16 && access==1)
{
access=0;
qraccesstype="not allowed servcode err"; //UPDATE SERVICE CODE ERROR AS STATUS
}
else
{
for(int i=0;i<myserviceid.length();i++)
{
if(qrservicecode[i]=='0' && myserviceid[i]=='1')
{
qraccesstype="not allowed servcode err"; //UPDATE SERVICE CODE ERROR AS STATUS
Serial.print("service code failed");
access=0;
}
}
}
}
else
{
access=0;
qraccesstype="not allowed gymid err"; //UPDATE GYMID ERROR AS STATUS
}
//AFTER ALL THE CHECKS ,STILL THE STATUS REMAINS 1 MEANS ID CREDENTIALS ARE CORRECT PERSONA LLOWED TO ENTER
if(access==1)
{
Serial.println("Valid Qrcode");
qraccesstype="allowed";
//RELAYON
door_status_led(access);
digitalWrite(RELAY,HIGH);
delay(DOOR_OPEN_TIME*1000);
digitalWrite(RELAY,LOW);
device_ready_status_led(1);
}
else
{
//RELAYOFF
door_status_led(access);
Serial.println("Invalid Qrcode");
delay(500);
device_ready_status_led(1);
//a qraccesstype="not allowed";
}
int send_status=0;
//CHECKING THE PREVIOUSLY UPDATED WIFI AND INTERNET STAUS FOR DATA SENDING TO SERVER
if(INTERNET_STATUS==ONLINE && WIFI_STATUS==ONLINE)
{
// Serial.println("try first send");
send_status=first_send();
if(send_status==1)
Serial.println("DATA recievd by server");
}
if(send_status==0)
{
//THIS MEANS SERVER DIDNT ACKNOWLEDGE SO WE ARE WRIITING THE DATA TO THE FILE FOR UPDATING WHEN CONNECTIVITY BECOMES FINE
//Storage data format on spiffs
//P*{status}{memberid}{time date}*
//P*{not allowed timeerror}{1}{8:59:4 8/7/21}*
String recodedata = "P*{" + (String)qraccesstype + "}{" + (String)qruserid + "}{" + (String)current_date_time + "}*";
Serial.println(recodedata);
int data_pack_len=recodedata.length();
char data_pack[data_pack_len];
strcpy(data_pack,recodedata.c_str());
//DISABLING THE TIMERS WHILE WRITTING TO FLASH OTHERWISE IT WILL CORRUPT THE WRITTING
timerAlarmDisable(timer);
timerAlarmDisable(timer2);
if(log_file_name== LOG_NAME1)
appendFile(SPIFFS, LOG_NAME1,data_pack);
else if(log_file_name== LOG_NAME2)
appendFile(SPIFFS, LOG_NAME2,data_pack);
//ENABLING BACK THE DISABLED TIMERS
timerAlarmEnable(timer2);
if( WIFI_STATUS==ONLINE)
timerAlarmEnable(timer);
}
}
void readFile(fs::FS &fs, const char * path){
Serial.printf("Reading file: %s\r\n", path);
int k=1;
File file = fs.open(path);
// File file_temp = fs.open("/Validlogtemp.txt",FILE_APPEND);
if(!file || file.isDirectory()){
Serial.println("- failed to open file for reading");
k=0;
}
char da[1000],s;
int i=0;
int fp;
int count=0;
Serial.println("File present verified");
while(file.available()){
s=(char)file.read();
//Serial.println(s);
if(s=='F')
{
count=1;
fp=file.position()-1;
}
else if(s=='P')
count=0;
if(count!=1)
{
da[i]=s;
i++;
}
}
da[i]='\0';
//file_temp.print(da);
//file_temp.close();
file.close();
Serial.println(da);
//deleteFile(SPIFFS, LOG_NAME1);
}
void read_get_File(fs::FS &fs, const char * path){
Serial.printf("Reading file: %s\r\n", path);
File file = fs.open(path);
if(!file || file.isDirectory()){
Serial.println("- failed to open file for reading");
}
String dat="";
Serial.println("- read from file:");
while(file.available())
{
dat=file.readString();
}
Serial.println(dat);
//Serial.println(file.size());
file.close();
}
void writeFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Writing file: %s\r\n", path);
File file = fs.open(path, FILE_WRITE);
if(!file){
Serial.println("- failed to open file for writing");
return;
}
if(file.print(message)){
Serial.println("- file written");
} else {
Serial.println("- write failed");
}
file.close();
}
void appendFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Appending to file: %s\r\n", path);
File file = fs.open(path, FILE_APPEND);
if(!file){
Serial.println("- failed to open file for appending");
return;
}
if(file.print(message)){
Serial.println("- message appended");
} else {
Serial.println("- append failed");
}
file.close();
}
void CreateFile(fs::FS &fs, const char * path){
Serial.printf("Appending to file: %s\r\n", path);
File file = fs.open(path, FILE_APPEND);
if(!file){
Serial.println("- failed to open file for appending");
return;
}
Serial.println("file created");
file.close();
}
void deleteFile(fs::FS &fs, const char * path){
Serial.printf("Deleting file: %s\r\n", path);
if(fs.remove(path)){
Serial.println("- file deleted");
} else {
}
}
/*
* CHECK ANY DATA ON SPIFFS NEEED TO BE SYNCED WITH SERVER
*/
void updateweb(fs::FS &fs, const char * path){
int p;
// EEPROM.get(129,p);
p=get_location(SPIFFS,"/location.txt");
// Serial.printf("Reading file: %s\r\n", path);
//CHECKING IF THE FILE SIZE IS EQUAL TO EEPROM ROM DATA SIZE IF YES NOT DATAS ARE THERE TO BE SYNCED WITH SERVER
File file = fs.open(path);
if(file.size()==p)
{
file.close();
return ;
}
else
{
int deletes=0;
p=get_location(SPIFFS,"/location.txt");
File file = fs.open(path);
// timerAlarmDisable(timer);
//timerAlarmDisable(timer2);
//File file_temp = fs.open("/Validlogtemp.txt",FILE_APPEND);
int k=1;
int position_cur=p;
k=file.size();
file.seek(position_cur,fs::SeekSet);
Serial.println("TOTAL FILE SIZE "+(String)k);
Serial.println("TOTAL DATA SEND SIZE "+(String)position_cur);
Serial.println("REMAINING DATA TO BE SEND "+(String)(k-position_cur));
if(!file || file.isDirectory()){
Serial.println("- failed to open file for reading");
}
char da[1000],s;
int i=0;
int fp=0;
int count=0;
Serial.println("File present verified");
while(file.available()){
s=(char)file.read();
//Serial.println(s);
//COUNTING NO OF P ,NO OF P IS EQUAL TO NO OF DATAS ON FILE,SO THAT WE CAN DO BULK DATA SYNCING BASED ON NO OF P PRESENT LOGIC
if(s=='P')
{
count++;
fp=0;
}
//DEPENDING ON USER NO OF DATA SEND IN BULK TO SERVER WILL DECIDED BY DATA_COUNT VARIABLE
if(count< (DATA_COUNT+1) && fp==0)
{
da[i]=s;
i++;
}
}
da[i]='\0';
//CONVERTING THE LOADED DATA FROM FILE TO JSON DATA FOR SENDING
if(data_conversion(da)==1)
{
p=strlen(da)+p;
// //file_temp.print(da);
if(p==file.size() &&file.size()!=0)
{
Serial.println(p);
Serial.println( file.size());
if(file_check(1)==1)
{
CreateFile(SPIFFS, LOG_NAME2);
deleteFile(SPIFFS, LOG_NAME1);
//Serial.print("########################Foundfile");
}
else if(file_check(1)==2)
{
//Serial.print("######################## NOOOOT Foundfile");
CreateFile(SPIFFS, LOG_NAME1);
deleteFile(SPIFFS, LOG_NAME2);
}
Serial.println("All datas send file deleted");
p=0;
}
Serial.println("data send length");
Serial.println(p);
deleteFile(SPIFFS, "/location.txt");
char st[16];
itoa(p,st, 10);
appendFile(SPIFFS, "/location.txt",st);
////file_temp.close();
file.close();
Serial.println("appended data");
//Serial.println(da);
// data_conversion(da);
// deleteFile(SPIFFS, LOG_NAME1);
}
else
{
Serial.println("INTERNET CONNECTIVITY BAD OR SERVER ISSUE");
INTERNET_STATUS=OFFLINE;
}
file.close();
}
//timerAlarmEnable(timer);
//timerAlarmEnable(timer2);
}
//edit
char *int_to_char(int c)
{
char s[100];
int rem;
int i=0;
if(c==0)
return "0";
while(c!=0)
{
rem=c%10;
s[i]=rem + '0';
c=c/10;
i++;
}
s[i]='\0';
//printf(" ********%s******* ",s);
char u[10];
char *k=u;
for(int i=0;s[i]!='\0';i++){}
//printf(" %c ",s[i]);
//printf(" %s ",s);
i--;
int j=0;
for(j=0;i>=0;i--,j++)
{
u[j]=s[i];
}
u[j]='\0';
//printf(" %s ",k);
return k;
}
int char_to_int(char *f)
{
int p=0;
int i=0;
while(f[i]!='\0')
{
p=p*10+(f[i]-'0');
i++;
}
return p;
}
int get_location(fs::FS &fs, const char * path){
File file = fs.open(path);
// Serial.println("location TOTAL FILE SIZE "+(String)file.size());
char s,num[100];
int i=0;
while(file.available()){
s=(char)file.read();
num[i]=s;
i++;}
num[i]='\0';
//Serial.println(num);
return char_to_int(num);
}
/*
* FOR SENDING THE DATA TO SERVER AFTER CONVERTING TO JSON FORMAT
*
*/
int first_send()
{
mydeviceid=EEPROM.readString(DEVID_ADDR);
mygym=EEPROM.readString(GYMID_ADDR);
myserviceid=EEPROM.readString(SERVICE_ADDR);
//DynamicJsonDocument resp(5120);
//
StaticJsonDocument<3000> doc;
doc["did"] = mydeviceid;
doc["gid"] =mygym;
JsonArray data = doc.createNestedArray("rc");
StaticJsonDocument<200> doc1;
doc1["mid"]=qruserid;
doc1["event"]=qraccesstype;
doc1["timestamp"]=current_date_time;
//data.add(48.756080);
//data.add(2.302038);
data.add(doc1);
String jsondata;
serializeJson(doc, jsondata);
// The above line prints:
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
// Start a new line
Serial.println("**************");
Serial.println( jsondata);
Serial.println("**************");
int p=data_send(jsondata);
Serial.println(p);
if(p!=1)
p=0;
return p;
}
/*
* CONVERTING THE DATA FROM FILE TO JSON FORMAT
*/
int data_conversion(char *datas)
{
mydeviceid=EEPROM.readString(DEVID_ADDR);
mygym=EEPROM.readString(GYMID_ADDR);
myserviceid=EEPROM.readString(SERVICE_ADDR);
//DynamicJsonDocument resp(5120);
//MAKING JSON DOCUMENT FOR BULK DATA SEND
StaticJsonDocument<3000> doc;
doc["did"] = mydeviceid;
doc["gid"] =mygym;
JsonArray data = doc.createNestedArray("rc");
//P*{status}{memberid}{time date}*
//P*{not allowed timeerror}{1}{8:59:4 8/7/21}*
qruserid="";
mydatetime="";
qraccesstype="";
current_date_time="";
int star=1;
int i=0;
int entry=0;
/*LOGIC FOR EXTRACTING DATA FROM THE FORMAT P*{status}{memberid}{time date}*
CHARCTER '*' AFTER 'P' DENOTES THE BEGGINING OF A DATA AND THE NEXT STAR DENOTES THE END OF ONE MEMEBR DATA
SO THE FIRST DATA WITHIN THE '{' '}" WILL BE THE STATUS AND SECOND "{" AND "}" WILL BE MEMBER ID AND THIRD "{" AND "}" WILL BE TIME AND DATE
*/
for(i=0;i<strlen(datas);i++)
{
if(datas[i]=='*')
star++;
if(datas[i]=='P')
entry=1;
if(entry==2)
{
if(datas[i]=='}')
entry=3;
else
qraccesstype+=datas[i];
}
if(datas[i]=='{' && entry==1)
entry=2;
if(entry==4)
{
if(datas[i]=='}')
entry=5;
else
qruserid+=datas[i];
}
if(datas[i]=='{' && entry==3)
entry=4;
if(entry==6)
{
if(datas[i]=='}')
entry=7;
else
mydatetime+=datas[i];
}
if(datas[i]=='{' && entry==5)
entry=6;
if((star%3)==0)
{
star++;
// ADDING THE MEMBER SPECIFIC DATA TO JSON ARRAY,ATTACHING JSON ARRAY TO JSON DOCUMENT
StaticJsonDocument<200> doc1;
doc1["mid"]=qruserid;
doc1["event"]=qraccesstype;
doc1["timestamp"]=mydatetime;
//data.add(48.756080);
//data.add(2.302038);
data.add(doc1);
qruserid="";
mydatetime="";
qraccesstype="";
}
}
String jsondata;
serializeJson(doc, jsondata);
// The above line prints:
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
// Start a new line
Serial.println("**************");
Serial.println( jsondata);
return data_send(jsondata);
}
/*
* SENDING THE JSON DATA
*/
int data_send(String jsondata)
{