-
Notifications
You must be signed in to change notification settings - Fork 1
/
wifi_Utilities.h
216 lines (132 loc) · 4.85 KB
/
wifi_Utilities.h
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
// ~~~~~~~~ Wifi Functions ~~~~~~~~
#include <WiFi.h>
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Access Point/Station Variables ~~~~~
// Variables for SOFT AP (Access Point)
const char* APssid = "countdownClock"; //Wifi Name - SSID
const char* APpassword = "12345678"; //Router Password - PASSWORD // Minimum 8 Characters
#define USE_AP_PASSWORD false
#define USE_DEFAULT_IP false
// Variables for Local network (Station)
//const char* STssid = "HOME_SSID";
//const char* STpassword = "HOME_PASS";
const char* STssid = "WORK_SSID";
const char* STpassword = "WORK_PASS";
#define USE_STATIC_IP true // Only effects parameters when in station mode
#define USE_ACCESS_POINT true // Not implemented yet - No effect
#define USE_STATION false
//~~~~~~ IP Info ~~~~
//wiFi.softAPIP - This defaults to 192.168.4.1 // Can be overwritten
// Set web server port number to 80
WiFiServer server(80);
// Set your Static IP address
IPAddress local_IP(192, 168, 1, 4);
// Set your Gateway IP address
IPAddress gateway(192, 168, 1, 254);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8); //optional
IPAddress secondaryDNS(8, 8, 4, 4); //optional
unsigned int connectionAttempts = 0; // Number of attempts taken to connect to wifi
unsigned int maxAttempts = 10; // Maximum number of attempts before reset or alternative action
unsigned long rolloverDelay = 500; // time between attempts with different wifi credentials.
unsigned long resetDelay = 10000; // disconnected time that would trigger a reset
// Disconnect all wifi (can be used for stability before restarting wifi)
void disconnectWIFI() {
Serial.println(F("Wifi Disconnecting.."));
//////////////////////////////////////////////////////////////////////////////////////BUG FIX POWER UP WIFI ISSUE??????????????????????????????
WiFi.disconnect(true); // disconnects STA Mode
delay(1000);
WiFi.softAPdisconnect(true); // disconnects AP Mode
delay(1000);
/////////////////////////////////////////////////////////////////////////////////////////
Serial.println(F("Wifi Successfully Disconnected"));
}
// Set up Server as an Access Point (AP)
void setup_accessPoint() {
disconnectWIFI();
// Connect to Wi-Fi network with SSID and password
Serial.print("Setting AP (Access Point)…");
// Remove the password parameter, if you want the AP (Access Point) to be open
// WiFi.hostname("relay");
if (USE_AP_PASSWORD) {
WiFi.softAP(APssid, APpassword);
} else {
WiFi.softAP(APssid);
}
IPAddress IP;
if (USE_DEFAULT_IP) {
IP = WiFi.softAPIP(); //wiFi.softAPIP - This defaults to 192.168.4.1
} else {
IP = local_IP;
}
Serial.print("AP IP address: ");
Serial.println(IP);
server.begin();
Serial.println("Server Ready");
}
// Connect to wifi with a Static IP
void connectWifi_staticIP() {
disconnectWIFI();
delay(2000); // delay for power issue during wifi startup // decoupling cap added to board as well.
// Configures static IP address
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
Serial.println("STA Failed to configure");
delay(1);
}
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(STssid);
WiFi.begin(STssid, STpassword);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
Serial.printf(" Connection Attempt: %i \n", connectionAttempts);
connectionAttempts++;
if (connectionAttempts >= maxAttempts) {
delay(1);
Serial.println("....");
Serial.println("RESETTING!");
Serial.println(" ");
ESP.restart();;
connectionAttempts = 0;
}
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
// ~~~~~~~~ Wifi Utilities ~~~~~~~~
/*
* Off the Shelf Functions for setting up ESP32 wifi modules
*
*
*
*
*/
// Connect to a Wifi Router as a Station - uses DHCP
void connectWifi_as_station() {
disconnectWIFI();
Serial.println(F("Trying to connect to WiFi as Station"));
WiFi.begin(STssid, STpassword);
delay(1000);
int wificheck = millis();
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
Serial.printf(" Connection Attempt: %i \n", connectionAttempts);
connectionAttempts++;
if (connectionAttempts >= maxAttempts) {
// This does nothing at the moment might do something later.
connectionAttempts = 0;
}
if ((millis() - wificheck) > 10000) {
Serial.println("....");
Serial.println("RESETTING!");
Serial.println(" ");
ESP.restart();;
}
}
Serial.println("WiFi Connected Successfully");
}