-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzong.ino
More file actions
1394 lines (1132 loc) · 37.3 KB
/
Copy pathzong.ino
File metadata and controls
1394 lines (1132 loc) · 37.3 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
#include <Arduino.h>
#include <U8g2lib.h>
#include <TM1637.h>
#include <WiFi.h>
#include <Wire.h>
#include <RTClib.h>
#include <WebServer.h>
#include <EEPROM.h>
// 定义PCF8574T的I2C地址
#define PCF8574T_ADDRESS 0x20
// 定义PCF8574T的引脚
#define PIN_LED 4
#define NOTE_B4 4110 // BB机的频率
#define NTP1 "ntp1.aliyun.com" // 定义 NTP 服务器地址
#define NTP2 "ntp2.aliyun.com"
#define NTP3 "ntp3.aliyun.com"
const char* assid = "esp32c3时钟"; //热点名称
const char* apassword = "123456789"; //热点密码
String ssid;
String password;
String username;
String passwords;
int x = 0;
int y = 48;
const int SDA_PIN = 5; //引脚连接
const int SDC_PIN = 4;
const uint8_t TM1637_CLK = 2;
const uint8_t TM1637_DIO = 3;
const int speakerPin = 18; // 喇叭连接到引脚18
// 定义闹钟声音的音调和持续时间
const int alarmMelody[] = {
NOTE_B4, NOTE_B4
};
const int noteDurations[] = {
110, 110
};
bool lb = false;
bool ll = true;
DateTime dateTime;
int Connection_timing = 0; //wifi连接后关闭WiFi连接的计时器
int Connection_timings = 0; //闹钟被关闭后的计时器
int chun;
bool pw = true; //配网第一次运行
int LiangDu = 0; //时钟亮度
int ShouDianTong = 0; //手电筒
char ymbx; //页面位置保存
bool JiShi = false; //确认时间是否被第一次获取
char xh; //二级菜单序号
char ymr = 0;
int page = 3; //控制初始化完成后的默认页面
int m = 128 * page; //动画
char yml = 0;
int Third_level_page = 0; //三级菜单序号
int Fourth_level_page = 0; //四级菜单序号
bool hj = false;
bool Enter_page = false;
int firstRun = false; //控制Enter_page第一次不运行
int i = 0; //123
int h = 0;
int networkCount; //wifi列表数量
int gpioy = 1;
int gpior = 0;
int gpios = 12; //定义io口
int gpiof = 13;
int pin0;
int pin1; //io口的存储
int pin18;
int pin13;
//闹钟变量
bool alarm_clock_switch = true; //闹钟开关
int clock1[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; //闹钟1
int clock2[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; //闹钟2
int clock3[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; //闹钟3
int clock4[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; //闹钟4
int clock5[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; //闹钟5
int sec = 0; // 定义一个 int 变量来存储秒钟
int hour = 0; // 定义一个 int 变量来存储小时
int minute = 0; // 定义一个 int 变量来存储分钟
int year = 0; // 定义一个 int 变量来存储年份
int month = 0; // 定义一个 int 变量来存储月份
int day = 0; // 定义一个 int 变量来存储日期
int weekt = 0;
int secl = 0; // 定义一个 int 变量来存储秒钟
int hourl = 0; // 定义一个 int 变量来存储小时
int minutel = 0; // 定义一个 int 变量来存储分钟
int yearl = 0; // 定义一个 int 变量来存储年份
int monthl = 0; // 定义一个 int 变量来存储月份
int dayl = 0; // 定义一个 int 变量来存储日期
/******************************************************************************/ //定义网页显示内容
String HTML = R"=====(
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>esp32c3配网界面</title>
<style>
* {
margin: 0;
padding: 0;
}
h1{color: #C4C4C4;
text-align: center;}
.dx {
width: auto;
height: 1200px;
background: #2994E1
}
.bx {
width: 600px;
height: 1200px;
background: #128FE9;
margin: 0 auto;
}
.srk {
height: 220px;
width: 450px;
border-radius: 12px;
margin: 0 auto;
background: #FFB600;
color: #FFFFFF;
font-size: 30px;
}
.srk label {
margin-left: 10px;
}
.srk input {
margin: 30px 10px 30px 0px;
}
.tj {
height: 50px;
width: 200px;
border-radius: 5px;
margin: 40px auto;
}
.tj input {
background: #FF7E00;
height: 50px;
width: 200px;
color: #FFFFFF;
border: none;
border-radius: 5px;
font-size: 25px;
}
.tjhz {
height: 50px;
width: 200px;
}
.sr {
border-radius: 5px;
height: 40px;
width: 250px;
border: none;
}
</style>
</head>
<body>
<div class="dx">
<div class="bx">
<h1>输入网络名称和密码</h1>
<form action= "/submit">
<div class="srk">
<label for="username"> wifi名称: </label>
<input class="sr" type="text" id="username" name="username">
<label for= "password"> wifi密码: </label>
<input class="sr" type="password" id="password" name="password">
</div>
<div class="tj">
<input type="submit" value="提交 ">
</div>
</form>
</div>
</div>
</body>
</html>
)=====";
String HTMLR = R"=====(
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>esp32c3配网界面</title>
<style>
h1{color: #C4C4C4;
text-align: center;}
.dx{width: auto;height: 1200px;background: #2994E1}
.bx{width: 600px;height: 1200px;background: #128FE9;margin: 0 auto;}
.bx p{color: #FFFFFF;line-height: 50px;margin-left: 40px;font-size: 30px;}
</style>
</head>
<body>
<div class="dx">
<div class="bx">
<h1>提交完成</h1>
</div>
</div>
</body>
</html>
)=====";
/***********************************************************************************************************************/ // 初始化
IPAddress local_ip(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
WebServer server(80);
//char* sj[] ={0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,-,}
//U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R2, U8X8_PIN_NONE, SDC_PIN, SDA_PIN);//定义单屏幕
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R2); //屏幕时钟芯片
RTC_DS1307 RTC;
//#define OLED_I2C_ADDRESS 0x3C
TM1637 tm1637(TM1637_CLK, TM1637_DIO);
/********************************************************************************************************************/ // 显示的图片
// 设置图标
const unsigned char settingsIcon[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xC0, 0x03, 0x00, 0x00, 0xE0, 0x07, 0x00, 0x00, 0xE3, 0xC7, 0x00, 0x80, 0xE7, 0xE7, 0x01,
0xC0, 0xFF, 0xFF, 0x03, 0xC0, 0xFF, 0xFF, 0x03, 0x80, 0xFF, 0xFF, 0x01, 0x00, 0xFF, 0xFF, 0x00,
0x00, 0x3F, 0xFC, 0x00, 0xE0, 0x1F, 0xF8, 0x07, 0xF0, 0x0F, 0xF0, 0x0F, 0xF0, 0x0F, 0xF0, 0x0F,
0xF0, 0x0F, 0xF0, 0x0F, 0xF0, 0x0F, 0xF0, 0x0F, 0xE0, 0x1F, 0xF8, 0x07, 0x00, 0x3F, 0xFC, 0x00,
0x00, 0xFF, 0xFF, 0x00, 0x80, 0xFF, 0xFF, 0x01, 0xC0, 0xFF, 0xFF, 0x03, 0xC0, 0xFF, 0xFF, 0x03,
0x80, 0xE7, 0xE7, 0x01, 0x00, 0xE3, 0xC7, 0x00, 0x00, 0xE0, 0x07, 0x00, 0x00, 0xC0, 0x03, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /*"d:\Users\hejia\Desktop\123.bmp",0*/
};
const unsigned char settingsIconl[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xF8, 0x1F, 0x00, 0x00, 0xF0, 0x0F, 0x00, 0x00, 0xF0, 0x0F, 0x00, 0x00, 0xE0, 0x07, 0x00,
0x00, 0xE0, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x02, 0x00,
0x00, 0x40, 0x02, 0x00, 0x00, 0xC0, 0x03, 0x00, 0x00, 0x40, 0x02, 0x00, 0x00, 0x40, 0x02, 0x00,
0x00, 0x40, 0x02, 0x00, 0x00, 0xC0, 0x03, 0x00, 0x00, 0xC0, 0x03, 0x00, 0x00, 0xC0, 0x03, 0x00,
0x00, 0xC0, 0x03, 0x00, 0x00, 0xC0, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /*"d:\Users\hejia\Desktop\112.bmp",0*/
};
const unsigned char settingsIcone[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x3F, 0x00, 0x80, 0xFF, 0xFF, 0x01,
0xC0, 0xFF, 0xFF, 0x07, 0xF0, 0x07, 0xE0, 0x0F, 0xF8, 0x01, 0x80, 0x1F, 0x7C, 0xF0, 0x0F, 0x3E,
0x1C, 0xFE, 0x7F, 0x38, 0x08, 0xFF, 0xFF, 0x10, 0xC0, 0x1F, 0xF8, 0x03, 0xC0, 0x07, 0xE0, 0x03,
0xC0, 0xE1, 0x87, 0x03, 0x80, 0xF8, 0x1F, 0x01, 0x00, 0xFC, 0x3F, 0x00, 0x00, 0x7E, 0x7E, 0x00,
0x00, 0x1C, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x03, 0x00, 0x00, 0xE0, 0x07, 0x00,
0x00, 0xE0, 0x07, 0x00, 0x00, 0xE0, 0x07, 0x00, 0x00, 0xC0, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /*"d:\Users\hejia\Desktop\544454.bmp",0*/
};
/****************************************************************/
// 函数:向PCF8574T芯片写入数据
void writePCF8574T(uint8_t pin, uint8_t value) {
Wire.beginTransmission(PCF8574T_ADDRESS); // 开始 I2C 传输,设置通信设备地址
Wire.write(value ? (1 << pin) : ~(1 << pin)); // 通过位操作符设置引脚状态
Wire.endTransmission(); // 结束 I2C 传输,发送数据
}
/***********************************************************************************************************************/ //喇叭
void playAlarm() {
for (int i = 0; i < sizeof(alarmMelody) / sizeof(alarmMelody[0]); i++) {
int toneDuration = noteDurations[i];
if (alarmMelody[i] == 0) {
noTone(speakerPin); // 停止发声
} else {
tone(speakerPin, alarmMelody[i]); // 播放音调
}
delay(toneDuration); // 持续时间
noTone(speakerPin); // 停止发声
delay(50); // 短暂的间隔
}
}
void laba() {
for (int i; i < 8; i++) {
writePCF8574T(PIN_LED, LOW);
playAlarm();
writePCF8574T(PIN_LED, HIGH);
delay(60);
writePCF8574T(PIN_LED, LOW);
playAlarm();
writePCF8574T(PIN_LED, HIGH);
delay(500);
}
delay(1000);
}
/**********************************************************************************************************************************************/ //网页
void handle_root() {
server.send(200, "text/html", HTML);
}
void handle_submit() {
username = server.arg("username"); //读取网页信息
passwords = server.arg("password");
u8g2.firstPage();
do {
u8g2.setCursor(0, 10);
u8g2.print("用户名: " + username);
u8g2.setCursor(0, 30);
u8g2.print("密码: " + passwords);
} while (u8g2.nextPage());
// server.send(200, "text/plain", "数据已接收");
int address2 = sizeof(username) + 4; // 下一个变量的地址是上一个变量所占空间的大小
// 将变量写入EEPROM
EEPROM.put(4, username);
EEPROM.put(address2, passwords);
// 将修改保存到EEPROM
EEPROM.commit();
wifilj(); //连接网络
server.send(200, "text/html", HTMLR);
}
void web() {
WiFi.softAP(assid, apassword); // 创建一个软件接入点(SoftAP)
WiFi.softAPConfig(local_ip, gateway, subnet); // 配置接入点的 IP 地址、网关和子网掩码
server.on("/", handle_root); // 处理根URL请求的回调函数为 handle_root
server.on("/submit", handle_submit); // 处理表单提交请求的回调函数为 handle_submit
server.begin(); // 启动HTTP服务器
delay(100);
}
/*********************************************************************************************************************************************/
//WiFi获取时间
void setClock() {
struct tm timeinfo; // 定义一个 tm 结构体来存储时间信息
if (WiFi.status() == WL_CONNECTED) {
configTime(8 * 3600, 0, NTP1, NTP2, NTP3); // 配置时间服务器并获取时间
}
if (getLocalTime(&timeinfo)) {
// 打印当前时间
//u8g2.print(timeinfo.tm_sec);
} else {
//u8g2.print("xxx");
}
year = timeinfo.tm_year + 1900; // 获取年份并存储到 year 变量中
month = timeinfo.tm_mon + 1; // 获取月份并存储到 month 变量中
day = timeinfo.tm_mday; // 获取日期并存储到 day 变量中
sec = timeinfo.tm_sec; // 获取秒时并存储到 sec 变量中
hour = timeinfo.tm_hour; // 获取小时并存储到 hour 变量中
minute = timeinfo.tm_min; // 获取分钟并存储到 minute 变量中
weekt = timeinfo.tm_wday;
secl = dateTime.second(), DEC;
hourl = dateTime.hour(), DEC;
minutel = dateTime.minute(), DEC;
yearl = dateTime.year(), DEC;
monthl = dateTime.month(), DEC;
dayl = dateTime.day(), DEC;
if (WiFi.status() == WL_CONNECTED) {
if (year != yearl || monthl != month || day != dayl || hour != hourl || minute != minutel || JiShi != true) {
RTC.adjust(DateTime(year, month, day, hour, minute, sec)); //设置初始时间
}
}
JiShi = true; //获取到时间后记录已获取
}
/******************************************
//获取附近2.4GWiFi列表
void wifihuoqv() {
WiFi.mode(WIFI_MODE_NULL);
networkCount = WiFi.scanNetworks();
for (int ib = 0; ib < networkCount; ++ib) {
//u8g2.setCursor(0, 5);
// 打印当前网络的编号
u8g2.setCursor(0, (ib + 1) * 16);
u8g2.print(ib + 1);
u8g2.print(WiFi.SSID(ib)); // 打印当前网络的名称
delay(10); // 延迟10ms,防止单次串口数据量过大导致丢数据
}
}
***********************************************/
//连接WiFi
void wifilj() {
int js = 5;
EEPROM.get(4, ssid);
EEPROM.get(sizeof(ssid) + 4, password);
u8g2.firstPage();
u8g2.sendBuffer();
u8g2.setCursor(0, 16);
u8g2.print("wifi: ");
u8g2.setCursor(0, 33);
u8g2.print(ssid);
u8g2.setCursor(0, 50);
u8g2.print("密码: ");
u8g2.setCursor(0, 64);
u8g2.print(password);
u8g2.sendBuffer();
delay(1500);
u8g2.firstPage();
u8g2.sendBuffer();
u8g2.setCursor(0, 20);
u8g2.print("连接中...");
u8g2.sendBuffer(); // 在串口上打印连接WiFi的信息
delay(500);
u8g2.firstPage();
u8g2.sendBuffer();
WiFi.begin(ssid, password); // 连接WiFi网络
while (WiFi.status() != WL_CONNECTED && js > 0) { // 等待WiFi连接状态
u8g2.setCursor(0, 18);
u8g2.print("连接中...");
u8g2.setCursor(0, 32);
u8g2.print(js);
u8g2.sendBuffer();
delay(400);
u8g2.firstPage();
js--;
}
u8g2.setCursor(3, 16);
u8g2.print("WIFI名称:");
u8g2.print(ssid); // 在串口上打印成功连接WiFi的信息
u8g2.setCursor(3, 32);
u8g2.print("密码:");
u8g2.print(password); // 在串口上打印IP地址信息
u8g2.setCursor(3, 48);
u8g2.print("IP地址:");
if (WiFi.status() == WL_CONNECTED) {
u8g2.setCursor(3, 64);
u8g2.print(WiFi.localIP()); // 获取并输出ESP32-C3的IP地址信息
WiFi.mode(WIFI_STA);
server.stop(); // 停止接受新请求
server.close(); // 断开与客户端的连接
} else {
u8g2.print("连接失败");
}
u8g2.sendBuffer();
u8g2.firstPage();
delay(500);
}
/**************************************************************************************************************************************/ //按键检测
void anjian() {
pin0 = digitalRead(gpioy);
pin1 = digitalRead(gpior);
pin18 = digitalRead(gpios);
pin13 = digitalRead(gpiof);
if (yml >= 0) {
if (pin0 == LOW) {
page++;
delay(200); //上
}
if (pin1 == LOW) {
page--;
delay(200); //下
}
if (pin18 == LOW) {
yml++;
delay(200); //中
}
}
if (pin13 == LOW) {
if (yml > 0 && !Enter_page) {
yml--;
} else if (yml <= -1) { //返回键
yml = 0;
}
delay(200);
}
if (page < 0 || page > 3) {
if (page < 0) {
page = 3;
} else if (page > 3) {
page = 0;
}
}
}
/**************************************************************************************************************************************/ //一级页面
void xs() {
u8g2.firstPage();
u8g2.setFont(u8g2_font_7_Seg_33x19_mn);
u8g2.setFont(u8g2_font_shizhong_32);
u8g2.setCursor(-128 + 10 - m, 30);
u8g2.print(year);
u8g2.print("年");
u8g2.setCursor(-128 + 5 - m, 60);
u8g2.print(month);
u8g2.print("月");
u8g2.print(day); //界面3
u8g2.print("日");
u8g2.setFont(u8g2_font_wqy16_t_gb2312);
u8g2.setCursor(48 - m, 60);
u8g2.print("设置");
u8g2.drawXBMP(48 - m, 8, 32, 32, settingsIcon);
u8g2.setCursor(168 - m, 60);
u8g2.print("手电筒"); //界面1
u8g2.drawXBMP(176 - m, 8, 32, 32, settingsIconl);
u8g2.setCursor(304 - m, 60);
u8g2.print("WIFI"); //界面2
u8g2.drawXBMP(304 - m, 8, 32, 32, settingsIcone);
u8g2.setFont(u8g2_font_7_Seg_33x19_mn);
u8g2.setFont(u8g2_font_shizhong_32);
u8g2.setCursor(394 - m, 30);
u8g2.print(year);
u8g2.print("年");
u8g2.setCursor(389 - m, 60);
u8g2.print(month);
u8g2.print("月");
u8g2.print(day); //界面3
u8g2.print("日");
u8g2.setFont(u8g2_font_wqy16_t_gb2312);
u8g2.setCursor(512 + 48 - m, 60);
u8g2.print("设置");
u8g2.drawXBMP(512 + 48 - m, 8, 32, 32, settingsIcon);
u8g2.sendBuffer();
}
void First_level_page() {
if (page == 0) {
x = 0;
} else if (page == 1) {
x = 128;
} else if (page == 2) {
x = 256;
} else if (page == 3) {
x = 384;
}
for (; m != x;) {
if (m < x) {
if (x - m > 128) {
for (; m != -128;) {
m = m - 16;
xs();
}
m = 384;
break;
} else {
m = m + 16;
}
} else if (m > x) {
if (m - x > 128) {
for (; m != 512;) {
m = m + 16;
xs();
}
m = 0;
break;
} else {
m = m - 16;
}
}
xs();
}
if (m == x) {
if (page == 0) {
u8g2.setCursor(48, 60);
u8g2.print("设置");
u8g2.drawXBMP(48, 8, 32, 32, settingsIcon);
} else if (page == 1) {
u8g2.setCursor(40, 60);
u8g2.print("手电筒"); //界面1
u8g2.drawXBMP(48, 8, 32, 32, settingsIconl);
} else if (page == 2) {
u8g2.setCursor(48, 60);
u8g2.print("WIFI"); //界面2
u8g2.drawXBMP(48, 8, 32, 32, settingsIcone);
} else if (page == 3) {
u8g2.setFont(u8g2_font_7_Seg_33x19_mn);
u8g2.setFont(u8g2_font_shizhong_32);
u8g2.setCursor(10, 30);
u8g2.print(year);
u8g2.print("年");
u8g2.setCursor(5, 60);
u8g2.print(month);
u8g2.print("月");
u8g2.print(day); //界面3
u8g2.print("日");
}
}
}
/********************************************************************************************************************************************/ //读取写入闹钟数据
void alarm_clock_put() {
EEPROM.put(512, clock1);
EEPROM.put(552, clock2);
EEPROM.put(592, clock3);
EEPROM.put(632, clock4);
EEPROM.put(672, clock5);
// 将修改保存到EEPROM
EEPROM.commit();
}
void alarm_clock_get() {
EEPROM.get(512, clock1);
EEPROM.get(552, clock2);
EEPROM.get(592, clock3);
EEPROM.get(632, clock4);
EEPROM.get(672, clock5);
}
/*****************************************/
//闹钟列表显示
void Alarm_display() {
int* currentClock;
switch (Third_level_page) {
case 0:
currentClock = clock1;
break;
case 1:
currentClock = clock2;
break;
case 2:
currentClock = clock3;
break;
case 3:
currentClock = clock4;
break;
case 4:
currentClock = clock5;
break;
default:
// 处理默认情况,如果需要的话
return;
}
u8g2.setCursor(45, 14);
u8g2.print("闹钟");
u8g2.print(Third_level_page + 1);
u8g2.setCursor(20, 14);
if (currentClock[0] == 1) {
u8g2.print("off");
} else {
u8g2.print("no");
}
// 闹钟界面
u8g2.setCursor(5, 32);
if (currentClock[1] / 10 < 1) {
u8g2.print("0");
}
u8g2.print(currentClock[1]);
u8g2.setCursor(22, 32);
u8g2.print("时");
u8g2.setCursor(52, 32);
if (currentClock[2] / 10 < 1) {
u8g2.print("0");
}
u8g2.print(currentClock[2]);
u8g2.setCursor(70, 32);
u8g2.print("分");
u8g2.setCursor(90, 37);
u8g2.print("星期:");
for (int i = 3; i <= 9; i++) {
u8g2.setCursor((i - 3) * 15 + 7, 60);
if (currentClock[i] == 1) {
u8g2.print(i - 3);
} else {
u8g2.drawFrame((i - 3) * 15 + 6, 47, 11, 15);
u8g2.print(i - 3);
}
}
}
/*******************************************************/
//闹钟列表调整
void Alarm_list_adjustment() {
if (Fourth_level_page == 0) {
u8g2.drawFrame(15, 0, 30, 18);
}
if (Fourth_level_page == 1) {
u8g2.drawFrame(1, 16, 40, 20);
}
if (Fourth_level_page == 2) {
u8g2.drawFrame(48, 16, 40, 20);
}
if (Fourth_level_page == 3) {
u8g2.drawFrame(4, 45, 15, 19);
}
if (Fourth_level_page == 4) {
u8g2.drawFrame(19, 45, 15, 19);
}
if (Fourth_level_page == 5) {
u8g2.drawFrame(34, 45, 15, 19);
}
if (Fourth_level_page == 6) {
u8g2.drawFrame(49, 45, 15, 19);
}
if (Fourth_level_page == 7) {
u8g2.drawFrame(64, 45, 15, 19);
}
if (Fourth_level_page == 8) {
u8g2.drawFrame(79, 45, 15, 19);
}
if (Fourth_level_page == 9) {
u8g2.drawFrame(94, 45, 15, 19);
}
}
/*******************************************************************************************************************************************/
void Alarm_reminder() {
if (!alarm_clock_switch) {
if (Connection_timings >= 1550) {
alarm_clock_switch = true;
Connection_timings = 0;
} else {
Connection_timings++;
}
}
int* clocks[] = { clock1, clock2, clock3, clock4, clock5 }; // 将数组声明修改为包含正确的初始化项
int numClocks = sizeof(clocks) / sizeof(clocks[0]);
ll = true;
for (int i = 0; i < numClocks; i++) {
if (clocks[i][1] == hour && clocks[i][2] == minute && clocks[i][0] == 0 && clocks[i][weekt + 3] == 0) {
ll = false;
if (pin0 == LOW || pin1 == LOW || pin18 == LOW || pin13 == LOW) {
alarm_clock_switch = false;
lb = false;
// 在条件满足时跳出循环
}
if (alarm_clock_switch) {
tm1637.display(1, 8); //第一位
tm1637.display(2, 8); //第二位
tm1637.display(0, 8); //第三位
tm1637.display(3, 8); //第四位
tm1637.point(1); //分号
// 执行相应操作
lb = true;
break;
}
}
}
if (ll) {
lb = false;
alarm_clock_switch = true;
Connection_timings = 0;
}
// 将alarm_clock_switch的值设置为true移至for循环之外
// 遍历完所有时钟后,如果没有满足条件的,则将其设置为true
}
/*******************************************************************************************************************************************/
void setup() {
Wire.begin(SDA_PIN, SDC_PIN); //初始化iic
RTC.begin(); // 初始化时钟
u8g2.begin(); // 初始化显示屏
Serial.begin(115200); //初始化串口
WiFi.mode(WIFI_STA); // 设置 ESP32 工作模式为无线终端模式
pinMode(speakerPin, OUTPUT); // 将引脚设为输出模式
pinMode(gpioy, INPUT_PULLUP);
pinMode(gpior, INPUT_PULLUP); // 配置输入引脚并设置为上拉
pinMode(gpios, INPUT_PULLUP);
writePCF8574T(PIN_LED, HIGH); //控制扩展io芯片4脚上拉以保证喇叭处于关闭状态
tm1637.init();
tm1637.set(0.2); //BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;亮度设置
// 设定字体
u8g2.enableUTF8Print(); // 启用UTF-8编码
//configTime(8 * 3600, 0, NTP1, NTP2, NTP3); // 配置时间服务器并获取时间
//setClock(); // 调用 setClock 函数来设置时间
EEPROM.begin(1024); //长久存储初始化
EEPROM.get(0, chun);
if (chun != 1) {
chun = 1;
username = "--------";
passwords = "--------";
EEPROM.put(0, chun);
EEPROM.put(4, username);
EEPROM.put(sizeof(username) + 4, passwords);
EEPROM.put(512, clock1);
EEPROM.put(552, clock2);
EEPROM.put(592, clock3);
EEPROM.put(632, clock4);
EEPROM.put(672, clock5);
// 将修改保存到EEPROM
EEPROM.commit();
}
// 设置中文字体
u8g2.setFont(u8g2_font_wqy16_t_gb2312);
//u8g2.setFont(u8g2_font_shizhong);
// 清空显示屏并准备显示下一帧内容
wifilj(); //连接网络
alarm_clock_get();
}
/**************************************************************************************************************************************/
void loop() {
dateTime = RTC.now();
// 读取GPIO引脚状态
// 设置中文字体
u8g2.setFont(u8g2_font_wqy16_t_gb2312);
//u8g2.setFont(u8g2_font_shizhong);
u8g2.firstPage(); // 清空显示屏并准备显示下一帧内容
/***************************************************/ //按键
anjian();
/**************************************************/
// 绘制图形、文字等
ShouDianTong = 0; //判断手电筒是否开启
do {
// 绘制图片
//u8g2.setDisplayRotation(U8G2_R2);
if (yml == 0) {
/*******************************************************************************/
if (i == 1) {
page = ymbx;
i = 0;
}
First_level_page(); //显示一级页面
u8g2.setFont(u8g2_font_wqy16_t_gb2312);
if (sec == 59) {
sec = 0;
} else { //降低误差
sec++;
}
/*
u8g2.setCursor(95, 16);
u8g2.print(Connection_timings);
u8g2.setCursor(110, 32);*/
u8g2.setCursor(110, 16);
u8g2.print(sec); //显示秒数
ymr = page;
/*****************************************************************************************/
} else if (yml >= 1) {
if (i == 0) {
page = 0;
i = 1;
}
ymbx = ymr;
/********************************************************************************/
if (ymr == 0) {
if (yml == 2 && xh == 0) {
if (pin1 == LOW) {
LiangDu++;
//上
} else if (pin0 == LOW) {
LiangDu--;
//下
}
if (LiangDu < 0 || LiangDu > 7) {
if (LiangDu < 0) {
LiangDu = 7;
} else {
LiangDu = 0;
}
}
u8g2.drawFrame(0, 0, 128, 18);
u8g2.drawBox(0, 0, 18.3 * LiangDu, 18); //亮度设置百分比进度条
u8g2.setCursor(60, 55);
u8g2.print(LiangDu);
tm1637.set(LiangDu);
page = 0;
} else if (yml == 2 && xh == 1) {
u8g2.setCursor(0, 16);
u8g2.print("请进入网页配置网络");
u8g2.sendBuffer();
WiFi.disconnect(true); //重新开启配网模式(断开wifi连接)
web();
delay(1000);
yml = 1;
} else if (yml == 2 && xh == 2) { //闹钟页面
if (pin13 == LOW) {
if (firstRun) {
firstRun = false;