-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouseRun_web.ino
1637 lines (1482 loc) · 39.3 KB
/
mouseRun_web.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
#include <Adafruit_I2CDevice.h>
#include <Adafruit_GFX.h>
#include <Wire.h>
#include <SPI.h>
#include "RTClib.h"
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <SPIFFS.h>
#include <FS.h>
#include <ArduinoJson.h>
#include <AsyncTCP.h>
#include "DHT.h"
#include "Adafruit_Sensor.h"
#include "Freenove_WS2812_Lib_for_ESP32.h"
#define DHTPIN 23 //DHT11 DATA 数据引脚
#define DHTTYPE DHT11 //选择的类型
#define HALLPIN1 33
#define HALLPIN2 18
#define HALLPIN3 16
#define HALLPIN4 17
#define CLOCK_SDA_PIN 21
#define CLOCK_SCL_PIN 22
#define DUMP_ENERGY_PIN 32
#define RGBLUMINANCE 5
#define SLEEPTIME 1//睡眠时间
//#define wifi_sta
#define SAVELOG
#define LEDS_COUNT 1 //彩灯数目
#define LEDS_PIN 25 //ESP32控制ws2812的引脚
#define CHANNEL 0 //控制通道,最多8路
//#define test
const char* ssid = "web_show";//设置热点名称(自定义)
const char* password = "1234567890";//设置热点密码(自定义)
const char* ssid_sta = "abc";//设置热点名称(自定义)
const char* password_sta = "1234567890";//设置热点密码(自定义)
AsyncWebServer server(80);//web服务器端口号
RTC_DS3231 rtc;
DHT dht(DHTPIN , DHTTYPE);
Freenove_ESP32_WS2812 strip = Freenove_ESP32_WS2812(LEDS_COUNT , LEDS_PIN , CHANNEL , TYPE_GRB);//申请一个彩灯控制对象
//BluetoothSerial SerialBT;
#ifdef test
int mm_test = 1 , dd_test = 10 , hh_test = 0;
#endif // test
struct Time
{
uint16_t YY;
uint8_t MM;
uint8_t DD;
uint8_t week;
String time;
} time_;
struct VerificationCode
{
uint32_t code = 0;
bool state = false;
} VerificationCode_;
double sendRunData_R[4][60] = {};//4个跑轮,每个60个数据
int sendHumiture_R[2][24] = {};//温湿度,24小时数据
int time_num_mm = 0;//用于记录跑动数据存储数组的索引值
int time_num_hh = 0;//用于记录温湿度存储数组的索引值
//float mouseRunNum1 = 0 , mouseRunNum2 = 0 , mouseRunNum3 = 0 , mouseRunNum4 = 0;
bool loginSYS = false;//是否已经注册过设备(低功耗)
char daysOfTheWeek[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
volatile double runData1 = 0 , runData2 = 0 , runData3 = 0 , runData4 = 0;//老鼠跑动的数据
u8 m_color[5][3] = { {255, 0, 0}, {0, 255, 0}, {0, 0, 255}, {255, 255, 255}, {0, 0, 0} };//彩灯颜色数组
void showData(int data , bool printType = false , bool type = true);
void showData(double data , bool printType = false , bool type = true);
void showData(String data , bool printType = false , bool type = true);
void setup()
{
Serial.begin(115200);//开启串口打印
//SerialBT.begin("mouse");
//初始化闪存系统
Serial.print("正在打开闪存系统...");
Serial.print("test");
strip.begin(); //初始化彩灯控制引脚
strip.setBrightness(RGBLUMINANCE);//设置彩灯亮度
strip.setLedColorData(0 , m_color[0][0] , m_color[0][1] , m_color[0][2]);//指定彩灯显示的颜色
strip.show();//显示彩灯,不调用时彩灯不显示
while ( !SPIFFS.begin(true) )
{
Serial.print("...");
delay(1000);
}
Serial.println("OK!");
// 可用空间总和(单位:字节)
Serial.print("可用空间总和: ");
Serial.print(SPIFFS.totalBytes());
Serial.println(" Bytes");
// 已用空间(单位:字节)
Serial.print("已用空间: ");
Serial.print(SPIFFS.usedBytes());
Serial.println(" Bytes");
Wire.begin(CLOCK_SDA_PIN , CLOCK_SCL_PIN);
if ( !rtc.begin() )
{
Serial.println("时钟未连接/未连接正确");
Serial.flush();
while ( 1 ) delay(10);
}
if ( rtc.lostPower() )
{
Serial.println("时钟时间错误,现在修改,稍后请务必手动校准时间");
rtc.adjust(DateTime(F(__DATE__) , F(__TIME__)));
}
/*Serial.println(__DATE__);
Serial.println(__DATE__== "Jan 2 2022");*/
//rtc.adjust(DateTime(F(__DATE__) , F(__TIME__)));
pinMode(HALLPIN1 , INPUT_PULLUP);
pinMode(HALLPIN2 , INPUT_PULLUP);
pinMode(HALLPIN3 , INPUT_PULLUP);
pinMode(HALLPIN4 , INPUT_PULLUP);
//设置中断触发程序
dht.begin();
#ifndef wifi_sta
WiFi.softAP(ssid , password);
#endif // !1
//#ifdef wifi_sta
//#endif // wifi_sta
//savaRunData();
//saveHumitureData();
//模拟个文件,方便调试
File dataFile = SPIFFS.open("/runData/20220101" , "w");// 建立File对象用于向SPIFFS中的file对象(即/notes.txt)写入信息
dataFile.print("This is test file"); // 向dataFile写入字符串信息
dataFile.close();
saveLog("设备开机");
//更新当前索引存储数据的
DateTime now = rtc.now();
time_num_mm = now.minute();
pinMode(0 , INPUT);
WiFi.mode(WIFI_MODE_NULL);
strip.setLedColorData(0 , m_color[1][0] , m_color[1][1] , m_color[1][2]);//指定彩灯显示的颜色
strip.show();//显示彩灯,不调用时彩灯不显示
delay(3000);
strip.setLedColorData(0 , m_color[4][0] , m_color[4][1] , m_color[4][2]);//指定彩灯显示的颜色
strip.show();//显示彩灯,不调用时彩灯不显示
xTaskCreatePinnedToCore(
runData_tack //创建任务
, "runData_tack" //创建任务的名称
, 1024 * 2 //分配的运行空间大小
, NULL //任务句柄
, 2 //任务优先级
, NULL //回调函数句柄
, 1);
xTaskCreatePinnedToCore(
showWifi //创建任务
, "showWifi" //创建任务的名称
, 1024 * 2 //分配的运行空间大小
, NULL //任务句柄
, 3 //任务优先级
, NULL //回调函数句柄
, 1);
}
void showWifi(void* pvParameters)
{
int num = 0;
int rgbNum = RGBLUMINANCE;
while ( true )
{
if ( WiFi.getMode() != WIFI_MODE_NULL )
{
strip.setBrightness(rgbNum--);
if ( rgbNum < 1 )
{
rgbNum = RGBLUMINANCE;
}
strip.setLedColorData(0 , m_color[2][0] , m_color[2][1] , m_color[2][2]);//指定彩灯显示的颜色
strip.show();//显示彩灯,不调用时彩灯不显示
}
if ( !digitalRead(0) )
{
num++;
Serial.println(num);
} else
{
num = 0;
}
if ( num == 3 )
{
num = 0;
for ( size_t i = 0; i < 3; i++ )
{
strip.setLedColorData(0 , m_color[2][0] , m_color[2][1] , m_color[2][2]);//指定彩灯显示的颜色
strip.show();//显示彩灯,不调用时彩灯不显示
vTaskDelay(300);
strip.setLedColorData(0 , m_color[4][0] , m_color[4][1] , m_color[4][2]);//指定彩灯显示的颜色
strip.show();//显示彩灯,不调用时彩灯不显示
vTaskDelay(300);
}
if ( WiFi.getMode() == WIFI_MODE_NULL )
{
Serial.println(!loginSYS);
if ( !loginSYS )
{
Serial.println("正在注册设备");
WiFi.mode(WIFI_MODE_APSTA);
WiFi.softAP(ssid , password);
WiFi.begin(ssid_sta , password_sta);
Serial.print("正在连接到:");
Serial.print(ssid_sta);
int conn_try_num = 0;//尝试连接wifi的时间
while ( WiFi.status() != WL_CONNECTED )
{ // 尝试进行wifi连接。
delay(1000);
conn_try_num++;
Serial.print(".");
if ( conn_try_num > 30 )
{
Serial.println("wifi连接超时。。。");
break;
}
}
Serial.print("IP address:\t"); // 以及
Serial.println(WiFi.localIP()); // NodeMCU的IP地址
Serial.print("Access Point: "); // 通过串口监视器输出信息
Serial.println(ssid); // 告知用户NodeMCU所建立的WiFi名
Serial.print("IP address: "); // 以及NodeMCU的IP地址
Serial.println(WiFi.softAPIP()); // 通过调用WiFi.softAPIP()可以得到NodeMCU的IP地址
if ( SPIFFS.exists("/index.html") )
{
Serial.println("存在控制台文件,开启服务器");
AsyncStaticWebHandler* handler = &server.serveStatic("/log" , SPIFFS , "/").setDefaultFile("log");
handler->setAuthentication("log" , "admin");
AsyncStaticWebHandler* handler2 = &server.serveStatic("/runData" , SPIFFS , "/runData/");
handler2->setCacheControl("max-age=1");
//handler2->setAuthentication("data" , "admin");
AsyncStaticWebHandler* handler1 = &server.serveStatic("/" , SPIFFS , "/").setDefaultFile("index.html");
handler1->setAuthentication("admin" , "admin");
handler1->setLastModified("Fri, 14 Jan 2022 15:43:02 GMT");
server.on("/reqRunData" , HTTP_POST , sendRunData);//给前端反馈跑动信息
server.on("/reqHumiture" , HTTP_POST , sendHumiture);//给前端反馈温湿度信息
server.on("/getServerTime" , HTTP_POST , getServerTime);
server.on("/setServerTime" , HTTP_POST , setServerTime);
server.on("/getdumpEnergy" , HTTP_POST , getdumpEnergy);
server.on("/getAllFileName" , HTTP_POST , sendFileName);
server.on("/getVerificationCode" , HTTP_POST , getVerificationCode);
server.on("/restoreFactorySet" , HTTP_POST , restoreFactorySet);
server.begin();//开启web服务器
//WiFi.mode(WIFI_MODE_NULL);
} else
{
Serial.println("不存在控制台文件");
}
loginSYS = true;
continue;
}
if ( WiFi.status() != WL_CONNECTED )
{
Serial.print("尝试重新连接wifi中。。。");
WiFi.disconnect();
WiFi.reconnect();
} else
{
Serial.print("wifi状态正常");
}
continue;
}
/* WiFi.mode(WIFI_MODE_NULL);
vTaskDelay(1000);*/
WiFi.setSleep(false);
num = 0;
WiFi.mode(WIFI_MODE_APSTA);
WiFi.softAP(ssid , password);
WiFi.begin(ssid_sta , password_sta);
Serial.print("正在恢复wifi连接,请稍等...");
int conn_try_num = 0;//尝试连接wifi的时间
while ( WiFi.status() != WL_CONNECTED )
{ // 尝试进行wifi连接。
delay(1000);
conn_try_num++;
Serial.print(".");
if ( conn_try_num > 30 )
{
Serial.println("wifi连接超时。。。");
break;
}
}
if ( WiFi.status() == WL_CONNECTED )
{
Serial.print("IP address:\t"); // 以及
Serial.println(WiFi.localIP()); // NodeMCU的IP地址
}
//vTaskDelay(10000);
//WiFi.mode(WIFI_MODE_NULL);
}
vTaskDelay(1000);
}
}
void runData_tack1(void* pvParameters)
{
while ( true )
{
if ( digitalRead(HALLPIN1) == LOW )
{
while ( true )
{
if ( digitalRead(HALLPIN1) == HIGH )
{
runData1 += 0.25;
showData("mouseRun1 " , true , false);
showData(runData1 , true);
break;
}
vTaskDelay(25);
}
}
vTaskDelay(12);
}
}
void runData_tack2(void* pvParameters)
{
while ( true )
{
if ( digitalRead(HALLPIN2) == LOW )
{
while ( true )
{
if ( digitalRead(HALLPIN2) == HIGH )
{
runData2 += 0.25;
showData("mouseRun2 " , true , false);
showData(runData2 , true);
break;
}
vTaskDelay(25);
}
}
vTaskDelay(10);
}
}
void runData_tack3(void* pvParameters)
{
while ( true )
{
if ( digitalRead(HALLPIN3) == LOW )
{
while ( true )
{
if ( digitalRead(HALLPIN3) == HIGH )
{
runData3 += 0.25;
showData("mouseRun3 " , true , false);
showData(runData3 , true);
break;
}
vTaskDelay(25);
}
}
vTaskDelay(8);
}
}
void runData_tack4(void* pvParameters)
{
while ( true )
{
if ( digitalRead(HALLPIN4) == LOW )
{
while ( true )
{
if ( digitalRead(HALLPIN4) == HIGH )
{
runData4 += 0.25;
showData("mouseRun4 " , true , false);
showData(runData4 , true);
break;
}
vTaskDelay(25);
}
}
vTaskDelay(9);
}
}
void runData_tack(void* pvParameters)
{
volatile bool state[4] = {};
while ( true )
{
if ( digitalRead(HALLPIN1) == LOW )
{
state[0] = true;
}
if ( digitalRead(HALLPIN2) == LOW )
{
state[1] = true;
}
if ( digitalRead(HALLPIN3) == LOW )
{
state[2] = true;
}
if ( digitalRead(HALLPIN4) == LOW )
{
state[3] = true;
}
if ( state[0] && digitalRead(HALLPIN1) == HIGH )
{
runData1 += 0.25;
showData("mouseRun1 " , true , false);
showData(runData1 , true);
state[0] = false;
}
if ( state[1] && digitalRead(HALLPIN2) == HIGH )
{
runData2 += 0.25;
showData("mouseRun2 " , true , false);
showData(runData2 , true);
state[1] = false;
}
if ( state[2] && digitalRead(HALLPIN3) == HIGH )
{
runData3 += 0.25;
showData("mouseRun3 " , true , false);
showData(runData3 , true);
state[2] = false;
}
if ( state[3] && digitalRead(HALLPIN4) == HIGH )
{
runData4 += 0.25;
showData("mouseRun4 " , true , false);
showData(runData4 , true);
state[3] = false;
}
delay(10);
}
}
void getServerTime(AsyncWebServerRequest* request)
{
DateTime now = rtc.now();
String fileName = "";
fileName += now.year();
fileName += "-";
fileName += now.month();
fileName += "-";
fileName += now.day();
fileName += " ";
fileName += now.hour();
fileName += ":";
fileName += now.minute();
fileName += ":";
fileName += now.second();
fileName += " ";
/*String MM_[12] = { "Jan","Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", };
fileName += MM_[now.dayOfTheWeek()];*/
Serial.println(fileName);
request->send(200 , "text/plain" , fileName);
}
void setServerTime(AsyncWebServerRequest* request)
{
String reqDate;
time_.YY = request->arg("yy").toInt();
time_.MM = request->arg("mm").toInt();
time_.DD = request->arg("dd").toInt();
//int deldata = request->arg("deldata").toInt();
/*if ( deldata == 1 )
{
DateTime now = rtc.now();
int time_yymmdd[3] = { now.year() ,now.month(),now.day() };
//删除两个日期之间的所有文件文件
while ( true )
{
if ( time_.YY == time_yymmdd[0] && time_.MM == time_yymmdd[1] && time_.DD == time_yymmdd[2] )
{
Serial.println("成功!!!");
String fileName = "/runData/";
fileName += now.year();
if ( now.month() < 10 )
{
fileName += "0";
}
fileName += now.month();
if ( now.day() < 10 )
{
fileName += "0";
}
fileName += now.day();
if ( true )
{
}
SPIFFS.remove(fileName);
return;
} else
{
time_yymmdd[2]--;
if ( time_yymmdd[2] < 1 )
{
time_yymmdd[2] = 31;
time_yymmdd[1]--;
}
if ( time_yymmdd[1] < 1 )
{
time_yymmdd[1] = 12;
time_yymmdd[0]--;
}
if ( time_yymmdd[0] < 2022 )
{
Serial.println("系统错误!!!");
return;
}
}
}
//向后调时,如果跨天需检查跳转的时间有没有文件
String fileName = "/runData/";
fileName += now.year();
if ( now.month() < 10 )
{
fileName += "0";
}
fileName += now.month();
if ( now.day() < 10 )
{
fileName += "0";
}
fileName += now.day();
SPIFFS.remove(fileName);
if ( !SPIFFS.exists(fileName) )
{
Serial.println("当天的文件删除成功");
} else
{
Serial.println("当天的文件删除失败");
request->send(500 , "text/plain" , "FILE_DELETION_FAILED");
ESP.restart();
}
} else
{//向后调时,如果跨天需检查跳转的时间有没有文件
DateTime now = rtc.now();
if ( time_.DD != now.day() )
{
Serial.println("向后调时,如果跨天需检查跳转的时间有没有文件!!!");
return;
} else
{
Serial.println("未跨天!!!");
return;
}
}*/
Serial.print(time_.YY);
Serial.print("\t");
Serial.print(time_.MM);
Serial.print("\t");
Serial.println(time_.DD);
time_.week = request->arg("week").toInt();
time_.time = request->arg("time");
String MM_[12] = { "Jan","Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", };
String DATE = "";
DATE += MM_[time_.MM - 1];
DATE += " ";//如果时间设置失败尝试改为1/2个空格
DATE += time_.DD;
DATE += " ";
DATE += time_.YY;
Serial.println(DATE);
Serial.println(time_.time);
Serial.print("写入数据DATE:");
Serial.println(DATE);
Serial.print("写入数据TIME:");
Serial.println(time_.time);
rtc.adjust(DateTime(DATE.c_str() , time_.time.c_str()));
DateTime now = rtc.now();
if ( now.year()==2002 )
{
Serial.println("方式二写入");
DATE = "";
DATE += MM_[time_.MM - 1];
DATE += " ";//如果时间设置失败尝试改为1/2个空格
DATE += time_.DD;
DATE += " ";
DATE += time_.YY;
Serial.println(DATE);
Serial.println(time_.time);
Serial.print("写入数据DATE:");
Serial.println(DATE);
Serial.print("写入数据TIME:");
Serial.println(time_.time);
rtc.adjust(DateTime(DATE.c_str() , time_.time.c_str()));
}
Serial.print("设置时间:成功");
/*if ( deldata == 1 )
{
ESP.restart();
}*/
request->send(200 , "text/plain" , "setTime_OK");
}
void sendRunData(AsyncWebServerRequest* request)
{
String reqDate;
reqDate += "/";
if ( request->hasArg("date") )
{
reqDate += request->arg("date");
Serial.print("请求老鼠跑动数据:");
Serial.println(reqDate);
request->send(200 , "text/plain" , sendData_mouseRun(reqDate));
}
}
void sendHumiture(AsyncWebServerRequest* request)
{
String reqDate;
reqDate += "/";
if ( request->hasArg("date") )
{
reqDate += request->arg("date");
Serial.print("请求温湿度数据");
Serial.println(reqDate);
request->send(200 , "text/plain" , sendData_humiture(reqDate));
}
}
void getdumpEnergy(AsyncWebServerRequest* request)
{
request->send(200 , "text/plain" , (String) getEnergy());
}
void sendFileName(AsyncWebServerRequest* request)
{
request->send(200 , "text/plain" , getAllFileName(false));
}
void getVerificationCode(AsyncWebServerRequest* request)
{
VerificationCode_.state = true;
VerificationCode_.code = random(100000 , 999999);
Serial.print("验证码:");
Serial.println(VerificationCode_.code);
request->send(200);
}
void restoreFactorySet(AsyncWebServerRequest* request)
{
uint32_t code = request->arg("code").toInt();
Serial.print("用户输入验证码:");
Serial.println(code);
if ( VerificationCode_.state && code == VerificationCode_.code )
{
request->send(200);
getAllFileName(true);
} else
{
request->send(500);
}
}
String getAllFileName(bool delFile)
{
File myFile = SPIFFS.open("/runData");
String outData;
StaticJsonDocument<2048> doc;
JsonArray fileName = doc.createNestedArray("fileName");
//fileName.add("20220101");
bool state = true;
int i = 0;
while ( true )
{
File entry = myFile.openNextFile();
i++;
if ( !entry )
{
if ( delFile )
{
Serial.println("恢复出厂设置完毕!");
ESP.restart();
}
// 如果没有文件则跳出循环
break;
}
String data = entry.name();
if ( delFile )
{
Serial.print("删除文件:");
Serial.println(data);
SPIFFS.remove(data);
continue;
}
fileName.add(data.substring(9));
Serial.print(data.substring(9));
entry.close();
if ( i >= 60 )
{
state = false;
}
}
doc["state"] = state;
serializeJson(doc , outData);
return outData;
}
uint8_t getEnergy()
{
//max4.2 min2.7
int adNum = analogRead(DUMP_ENERGY_PIN);
//映射出电压
uint8_t voltage = map(adNum , 0 , 4095 , 0 , 33);
if ( !( 27 <= voltage * 2 && voltage * 2 <= 42 ) )
{
Serial.print("电压实际读数:");
Serial.print(voltage);
Serial.println("V");
return 101;//返回错误值
}
return map(voltage * 2 , 27 , 42 , 0 , 100);
}
//向客户端发送老鼠跑动的数据
String sendData_mouseRun(String date)
{
//if ( date.length()!=11 )//hhhhmmdd@hh
//{
// Serial.println("请求日期有误");
// return "err:Incorrect request date";
//}
if ( SPIFFS.exists(date) )
{
File dataFile = SPIFFS.open(date , "r");
long dataSize = dataFile.size();
String fsData;
for ( int i = 0; i < dataSize; i++ )
{
fsData += (char) dataFile.read();
}
dataFile.close();
return fsData;
} else
{
showData("err:The file was not found" , true);
return "err:The file was not found";
}
}
//向客户端发送温湿度数据
String sendData_humiture(String date)
{
//if ( date.length() != 8 )//hhhhmmdd
//{
// Serial.println("请求日期有误");
// return "err:Incorrect request date";
//}
if ( SPIFFS.exists(date) )
{
File dataFile = SPIFFS.open(date , "r");
long dataSize = dataFile.size();
String fsData;
for ( int i = 0; i < dataSize; i++ )
{
fsData += (char) dataFile.read();
}
dataFile.close();
return fsData;
} else
{
return "err:The file was not found";
}
}
//保存老鼠跑动的数据
void savaRunData()
{
//必须结束中断,否则容易被狗复位
Serial.print("闪存剩余空间: ");
int freeSpace = SPIFFS.totalBytes() - SPIFFS.usedBytes();
Serial.print(freeSpace);
Serial.println(" Bytes");
if ( freeSpace < 3000 )
{
Serial.println("存储空间告急!!!");
} else if ( freeSpace < 1500 )
{
Serial.println("存储空间已经用尽,请手动清理!!!");
return;
} else
{
//Serial.println("存储空间充足!!!");
showData("存储空间充足!!!" , true);
}
String runData;
DynamicJsonDocument doc(4096);
DateTime now = rtc.now();
String fileName = "/";
fileName += now.year();
if ( now.month() < 10 )
{
fileName += "0";
}
fileName += now.month();
if ( now.day() < 10 )
{
fileName += "0";
}
fileName += now.day();
fileName += "@";
fileName += now.hour();
#ifdef test
if ( mm_test > 12 )
{
mm_test = 1;
}
if ( dd_test > 28 )
{
dd_test = 1;
}
if ( hh_test > 24 )
{
hh_test = 1;
}
fileName = "/2022";
if ( mm_test < 10 )
{
fileName += "0";
}
fileName += mm_test;
if ( dd_test < 10 )
{
fileName += "0";
}
fileName += dd_test;
fileName += "@";
fileName += hh_test;
#endif // test
/* Serial.print("当前老鼠跑动的数据存储文件名:");
Serial.println(fileName);*/
showData("当前老鼠跑动的数据存储文件名:" , true , false);
showData(fileName , true);
doc["dataType"] = "runData";
doc["date"] = fileName;//hhhhyydd@hh
JsonArray data1 = doc.createNestedArray("data1");
//data1.add(10);
for ( size_t j = 0; j < 60; j++ )
{
data1.add(sendRunData_R[0][j]);
}
JsonArray data2 = doc.createNestedArray("data2");
for ( size_t j = 0; j < 60; j++ )
{
data2.add(sendRunData_R[1][j]);
}
JsonArray data3 = doc.createNestedArray("data3");
for ( size_t j = 0; j < 60; j++ )
{
data3.add(sendRunData_R[2][j]);
}
JsonArray data4 = doc.createNestedArray("data4");
for ( size_t j = 0; j < 60; j++ )
{
data4.add(sendRunData_R[3][j]);
}
serializeJson(doc , runData);
//Serial.println(fileName);
//Serial.println(runData);
showData(runData , true);
File dataFile = SPIFFS.open(fileName , "w");// 建立File对象用于向SPIFFS中的file对象(即/notes.txt)写入信息
dataFile.println(runData); // 向dataFile写入字符串信息
dataFile.close();
saveLog("savaRunData");
}
//保存温湿度
void saveHumitureData()
{
//必须结束中断,否则容易被狗复位
Serial.print("闪存剩余空间: ");
int freeSpace = SPIFFS.totalBytes() - SPIFFS.usedBytes();
Serial.print(freeSpace);
Serial.println(" Bytes");
if ( freeSpace < 3000 )
{
Serial.println("存储空间告急!!!");
} else if ( freeSpace < 1500 )
{
Serial.println("存储空间已经用尽,请手动清理!!!");
return;
} else
{
Serial.println("存储空间充足!!!");
}
String runData;
StaticJsonDocument<1024> doc;
DateTime now = rtc.now();
String fileName = "/";
fileName += now.year();
if ( now.month() < 10 )
{
fileName += "0";
}
fileName += now.month();
if ( now.day() < 10 )
{
fileName += "0";
}
fileName += now.day();
#ifdef test
if ( mm_test > 12 )
{
mm_test = 1;
}
if ( dd_test > 28 )
{
dd_test = 1;
}
fileName = "/2022";
if ( mm_test < 10 )
{
fileName += "0";
}
fileName += mm_test;
if ( dd_test < 10 )
{
fileName += "0";
}
fileName += dd_test;
#endif // test
Serial.print("当前温湿度的数据存储文件名:");
Serial.println(fileName);
doc["dataType"] = "humitureData";
doc["date"] = fileName;//hhhhmmdd
JsonArray temperature = doc.createNestedArray("temperature");
for ( size_t i = 0; i < 24; i++ )
{
temperature.add(sendHumiture_R[0][i]);
}
JsonArray humidity = doc.createNestedArray("humidity");
for ( size_t i = 0; i < 24; i++ )
{
humidity.add(sendHumiture_R[1][i]);
}
serializeJson(doc , runData);
Serial.println(fileName);
Serial.println(runData);
File dataFile = SPIFFS.open(fileName , "w");// 建立File对象用于向SPIFFS中的file对象(即/notes.txt)写入信息
dataFile.println(runData); // 向dataFile写入字符串信息
dataFile.close();