-
Notifications
You must be signed in to change notification settings - Fork 0
/
WiFi.ino
68 lines (64 loc) · 1.71 KB
/
WiFi.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
void startWireless() {
startWireless(120);
} // end startWireless()
void startWireless(int howLong) {
WiFi.mode(WIFI_OFF);
if (deviceMode == 1) {
if (!startStation(howLong))
switchMode(9);
}
if (deviceMode != 1)
startAP();
} // end startWireless()
void startAP() {
handleDelay(200);
if (deviceMode == 2 || deviceMode == 9)
WiFi.mode(WIFI_AP_STA);
else
WiFi.mode(WIFI_AP);
if (strlen(apPass) >= 8)
WiFi.softAP(deviceName, apPass);
else {
if (deviceMode == 0)
WiFi.softAP(deviceName);
else
ESP.restart();
}
handleDelay(10);
Serial.print("Setting up AP: ");
Serial.print(deviceName);
Serial.print(" with IP Address: ");
Serial.println(WiFi.softAPIP());
} // end startAP()
boolean startStation(int howLong) {
if (deviceMode == 1)
WiFi.mode(WIFI_STA);
else
WiFi.mode(WIFI_AP_STA);
WiFi.hostname(deviceName);
Serial.print(F("Attempting connection to SSID: "));
Serial.print(stationSSID);
Serial.print(F(" with hostname: "));
Serial.println(deviceName);
WiFi.begin(stationSSID, stationPass);
// Try to connect for specified time, then give up.
for (int i = 0; i < howLong; i++) {
if (WiFi.status() != WL_CONNECTED) {
handleDelay(1000);
Serial.print(F("."));
}
// If connection achieved, write to serial and return true.
else {
Serial.print(F("\nConnected to SSID: "));
Serial.print(stationSSID);
Serial.print(F(" with IP address: "));
Serial.println(WiFi.localIP());
return true;
}
}
// Return false if connection not achieved.
Serial.print(F("\nCould not connect to SSID: "));
Serial.println(stationSSID);
WiFi.mode(WIFI_OFF);
return false;
} // end startStationMode()