-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWiFiConnection.cpp
168 lines (142 loc) · 4.43 KB
/
WiFiConnection.cpp
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
/***************
* NTP Clock
*
* Brad Hines
* Feb 2020
*/
#include "WiFiConnection.h"
/***************************************
* tWiFiConnection constructor
*
* INPUTS:
* sSsid - a string containing the SSID of your router, e.g. "MyNetGearWiFi"
* sPassword - The password for your wifi as a string, e.g. "LotsOfGerbils"
* iLedPin - which ESP8266 GPIO pin to use for connection status. Recommend using
* 2, which is the LED on the ESP8266 module itself. If you specify 0,
* no LED will be used, and you can use the LED pins for other things.
*/
tWiFiConnection::tWiFiConnection(const char *sSsid, const char *sPassword, int iLedPin) :
_sSsid(sSsid),
_sPassword(sPassword),
_iLedPin(iLedPin)
{
// Init the display pin as an output
// The ESP8266 LED is active low, so write a 1 to turn it off at init
if (iLedPin > 0) {
pinMode(iLedPin, OUTPUT);
digitalWrite(_iLedPin, 1);
}
_status = WL_IDLE_STATUS;
}
/***************************************
* tWiFiConnection::ConnectToRouter
*
* Forge a connection to the WiFi router
*
* Will try connecting at 10-second intervals.
*
* INPUTS:
* iNumTimesToTry - will try this many times before returning. Set to 0 to
* try forever.
* RETURNS:
* true if connected to the router
* false if failed after iNumTimesToTry
*
*/
bool tWiFiConnection::ConnectToRouter(int iNumTimesToTry)
{
bool bKeepTrying = true;
_status = GetStatus();
if (_status == WL_NO_SHIELD) {
Serial.println(F("WiFi-capable hardware not present"));
return false;
}
while (bKeepTrying) {
while (_status != WL_CONNECTED) {
Serial.print(F("Attempting to connect to SSID: "));
Serial.print(_sSsid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
WiFi.begin(_sSsid, _sPassword);
// wait 10 seconds for connection:
for (int iWait = 0; iWait < WIFI_SECONDS_BETWEEN_CONNECTION_TRIES; iWait++) {
Serial.print(F("."));
delay(1000);
if (GetStatus() == WL_CONNECTED) {
bKeepTrying = false;
break;
}
}
PrintStatus();
}
// If iNumTimesToTry is non-zero, decrement it. If it reaches 0, stop trying
if (iNumTimesToTry > 0 && --iNumTimesToTry < 1) bKeepTrying = false;
}
Serial.println(F("\n*** Connected to wifi ***\n"));
PrintInfo();
}
/***************************************
* tWiFiConnection::GetStatus
*
* Retrieves the status of the connection and sets the LED to match
*
* Note that the LED on the ESP module is active low, so this follows that
* paradigm.
*
* INPUTS:
* None
* OUTPUTS:
* The current connection status
* SIDE_EFFECTS:
* Updates _status to match the current status.
* Turns on the LED if status is WL_CONNECTED, else turns it off. Outputs a 0 to turn the
* LED on, 1 to turn it off.
*/
int tWiFiConnection::GetStatus()
{
_status = WiFi.status();
if (_iLedPin > 0) {
digitalWrite(_iLedPin, !(_status == WL_CONNECTED));
}
return _status;
}
/***************************************
* tWiFiConnection::PrintStatus
*
* Prints the current status of the connection to Serial.
*
*/
void tWiFiConnection::PrintStatus()
{
switch (_status) {
case WL_CONNECTED: Serial.println(F("Connected")); break;
case WL_IDLE_STATUS: Serial.println(F("Idle")); break;
case WL_NO_SHIELD: Serial.println(F("No Shield")); break;
case WL_NO_SSID_AVAIL: Serial.println(F("No SSID Available")); break;
case WL_SCAN_COMPLETED: Serial.println(F("Scan completed")); break;
case WL_CONNECT_FAILED: Serial.println(F("Scan completed")); break;
case WL_CONNECTION_LOST: Serial.println(F("Connection lost")); break;
case WL_DISCONNECTED: Serial.println(F("Disconnected")); break;
default: Serial.println(F("Unknown")); break;
}
}
/***************************************
* tWiFiConnection::PrintInfo
*
* Prints the details of the connection to Serial.
*
*/
void tWiFiConnection::PrintInfo()
{
// print the SSID of the network you're attached to:
Serial.print(F("SSID: "));
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print(F("IP Address: "));
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print(F("Signal strength (RSSI): "));
Serial.print(rssi);
Serial.println(F(" dBm"));
}