-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wifi_STA.ino
160 lines (132 loc) · 3.51 KB
/
Wifi_STA.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
/*
* @Description: WIFI STA
* @version: V1.0.0
* @Author: LILYGO_L
* @Date: 2023-08-16 15:19:01
* @LastEditors: LILYGO_L
* @LastEditTime: 2024-02-01 14:13:49
* @License: GPL 3.0
*/
#include "Arduino.h"
#include "WiFi.h"
#include <HTTPClient.h>
#include "pin_config.h"
#define WIFI_SSID "xinyuandianzi"
#define WIFI_PASSWORD "AA15994823428"
// #define WIFI_SSID "LilyGo-AABB"
// #define WIFI_PASSWORD "xinyuandianzi"
#define WIFI_CONNECT_WAIT_MAX 5000
#define NTP_SERVER1 "pool.ntp.org"
#define NTP_SERVER2 "time.nist.gov"
#define GMT_OFFSET_SEC 8 * 3600 // 时区设置函数,东八区(UTC/GMT+8:00)写成8*3600
#define DAY_LIGHT_OFFSET_SEC 0 // 夏令时填写3600,否则填0
static bool Wifi_Connection_Flag = false;
static size_t CycleTime = 0;
void Wifi_STA_Test(void)
{
String text;
int wifi_num = 0;
Serial.printf("\nScanning wifi");
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
wifi_num = WiFi.scanNetworks();
if (wifi_num == 0)
{
text = "\nWiFi scan complete !\nNo wifi discovered.\n";
}
else
{
text = "\nWiFi scan complete !\n";
text += wifi_num;
text += " wifi discovered.\n\n";
for (int i = 0; i < wifi_num; i++)
{
text += (i + 1);
text += ": ";
text += WiFi.SSID(i);
text += " (";
text += WiFi.RSSI(i);
text += ")";
text += (WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " \n" : "*\n";
delay(10);
}
}
Serial.println(text);
delay(3000);
text.clear();
text = "Connecting to ";
Serial.print("Connecting to ");
text += WIFI_SSID;
text += "\n";
Serial.print(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
uint32_t last_tick = millis();
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
text += ".";
delay(100);
if (millis() - last_tick > WIFI_CONNECT_WAIT_MAX)
{
Wifi_Connection_Flag = false;
break;
}
else
{
Wifi_Connection_Flag = true;
}
}
if (Wifi_Connection_Flag == true)
{
text += "\nThe connection was successful ! \nTakes ";
Serial.print("\nThe connection was successful ! \nTakes ");
text += millis() - last_tick;
Serial.print(millis() - last_tick);
text += " ms\n";
Serial.println(" ms\n");
}
else
{
Serial.printf("\nWifi test error!\n");
}
}
void WIFI_STA_Test_Loop(void)
{
if (Wifi_Connection_Flag == false)
{
Serial.printf("Not connected to the networ\n");
Wifi_STA_Test();
delay(2000);
}
else
{
// Obtain and set the time from the network time server
// After successful acquisition, the chip will use the RTC clock to update the holding time
configTime(GMT_OFFSET_SEC, DAY_LIGHT_OFFSET_SEC, NTP_SERVER1, NTP_SERVER2);
delay(3000);
struct tm timeinfo;
if (!getLocalTime(&timeinfo, 10000))
{
Serial.println("Failed to obtain time");
Wifi_Connection_Flag = false;
return;
}
Serial.println("Get time success");
Serial.println(&timeinfo, "%A,%B %d %Y %H:%M:%S"); // Format Output
}
}
void setup()
{
Serial.begin(115200);
Serial.println("Ciallo");
Wifi_STA_Test();
}
void loop()
{
if (millis() > CycleTime)
{
WIFI_STA_Test_Loop();
CycleTime = millis() + 3000; // 3000ms
}
}