|
19 | 19 | INCLUDE
|
20 | 20 | ******************************************************************************/
|
21 | 21 |
|
22 |
| - |
23 |
| -/* |
24 |
| - static int const DBG_NONE = -1; |
25 |
| - static int const DBG_ERROR = 0; |
26 |
| - static int const DBG_WARNING = 1; |
27 |
| - static int const DBG_INFO = 2; |
28 |
| - static int const DBG_DEBUG = 3; |
29 |
| - static int const DBG_VERBOSE = 4; |
30 |
| -*/ |
31 |
| - |
32 |
| -#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_NANO_33_IOT) |
33 |
| - |
34 | 22 | #include "Arduino_WiFiConnectionHandler.h"
|
35 | 23 |
|
36 |
| -/****************************************************************************** |
37 |
| - CONSTANTS |
38 |
| - ******************************************************************************/ |
39 |
| - |
40 |
| -static const unsigned long NETWORK_CONNECTION_INTERVAL = 30000; /* NOT USED */ |
| 24 | +#ifdef BOARD_HAS_WIFI /* Only compile if the board has WiFi */ |
41 | 25 |
|
42 | 26 | /******************************************************************************
|
43 | 27 | CTOR/DTOR
|
@@ -101,197 +85,142 @@ unsigned long WiFiConnectionHandler::getTime() {
|
101 | 85 | void WiFiConnectionHandler::update() {
|
102 | 86 |
|
103 | 87 | unsigned long const now = millis();
|
104 |
| - int networkStatus = 0; |
105 |
| - if (now - lastConnectionTickTime > connectionTickTimeInterval) { /* time bracket */ |
106 |
| - |
| 88 | + if((now - lastConnectionTickTime) > connectionTickTimeInterval) |
| 89 | + { |
107 | 90 | lastConnectionTickTime = now;
|
108 | 91 |
|
109 | 92 | switch (netConnectionState) {
|
110 |
| - case NetworkConnectionState::INIT: { |
111 |
| - Debug.print(DBG_VERBOSE, "::INIT"); |
112 |
| - #if !defined(BOARD_ESP8266) |
113 |
| - networkStatus = WiFi.status(); |
114 |
| - |
115 |
| - Debug.print(DBG_INFO, "WiFi.status(): %d", networkStatus); |
116 |
| - if (networkStatus == NETWORK_HARDWARE_ERROR) { |
117 |
| - // NO FURTHER ACTION WILL FOLLOW THIS |
118 |
| - changeConnectionState(NetworkConnectionState::ERROR); |
119 |
| - return; |
120 |
| - } |
121 |
| - Debug.print(DBG_ERROR, "Current WiFi Firmware: %s", WiFi.firmwareVersion()); |
122 |
| - if (WiFi.firmwareVersion() < WIFI_FIRMWARE_VERSION_REQUIRED) { |
123 |
| - Debug.print(DBG_ERROR, "Latest WiFi Firmware: %s", WIFI_FIRMWARE_VERSION_REQUIRED); |
124 |
| - Debug.print(DBG_ERROR, "Please update to the latest version for best performance."); |
125 |
| - delay(5000); |
126 |
| - } |
127 |
| - #else |
128 |
| - Debug.print(DBG_ERROR, "WiFi status ESP: %d", WiFi.status()); |
129 |
| - WiFi.disconnect(); |
130 |
| - delay(300); |
131 |
| - networkStatus = WiFi.begin(ssid, pass); |
132 |
| - delay(1000); |
133 |
| - #endif |
134 |
| - |
135 |
| - changeConnectionState(NetworkConnectionState::CONNECTING); |
136 |
| - } |
137 |
| - break; |
138 |
| - case NetworkConnectionState::CONNECTING: { |
139 |
| - Debug.print(DBG_VERBOSE, "::CONNECTING"); |
140 |
| - networkStatus = WiFi.status(); |
141 |
| - |
142 |
| - #if !defined(BOARD_ESP8266) |
143 |
| - |
144 |
| - if (networkStatus != WL_CONNECTED) { |
145 |
| - networkStatus = WiFi.begin(ssid, pass); |
146 |
| - } |
147 |
| - |
148 |
| - #else |
149 |
| - |
150 |
| - networkStatus = WiFi.status(); |
151 |
| - |
152 |
| - #endif |
153 |
| - |
154 |
| - Debug.print(DBG_VERBOSE, "WiFi.status(): %d", networkStatus); |
155 |
| - if (networkStatus != NETWORK_CONNECTED) { |
156 |
| - Debug.print(DBG_ERROR, "Connection to \"%s\" failed", ssid); |
157 |
| - Debug.print(DBG_INFO, "Retrying in \"%d\" milliseconds", connectionTickTimeInterval); |
158 |
| - |
159 |
| - return; |
160 |
| - } else { |
161 |
| - Debug.print(DBG_INFO, "Connected to \"%s\"", ssid); |
162 |
| - changeConnectionState(NetworkConnectionState::GETTIME); |
163 |
| - return; |
164 |
| - } |
165 |
| - } |
166 |
| - break; |
167 |
| - case NetworkConnectionState::CONNECTED: { |
168 |
| - |
169 |
| - networkStatus = WiFi.status(); |
170 |
| - Debug.print(DBG_VERBOSE, "WiFi.status(): %d", networkStatus); |
171 |
| - if (networkStatus != WL_CONNECTED) { |
172 |
| - changeConnectionState(NetworkConnectionState::DISCONNECTED); |
173 |
| - return; |
174 |
| - } |
175 |
| - Debug.print(DBG_VERBOSE, "Connected to \"%s\"", ssid); |
176 |
| - } |
177 |
| - break; |
178 |
| - case NetworkConnectionState::GETTIME: { |
179 |
| - Debug.print(DBG_VERBOSE, "NetworkConnectionState::GETTIME"); |
180 |
| -#if defined(BOARD_ESP8266) |
181 |
| - configTime(0, 0, "time.arduino.cc", "pool.ntp.org", "time.nist.gov"); |
182 |
| -#endif |
183 |
| - changeConnectionState(NetworkConnectionState::CONNECTED); |
184 |
| - } |
185 |
| - break; |
186 |
| - case NetworkConnectionState::DISCONNECTING: { |
187 |
| - if (networkStatus != WL_CONNECTED) { |
188 |
| - changeConnectionState(NetworkConnectionState::DISCONNECTED); |
189 |
| - } |
190 |
| - } |
191 |
| - break; |
192 |
| - case NetworkConnectionState::DISCONNECTED: { |
193 |
| - #if !defined(BOARD_ESP8266) |
194 |
| - WiFi.end(); |
195 |
| - #endif |
196 |
| - if (keepAlive) { |
197 |
| - changeConnectionState(NetworkConnectionState::INIT); |
198 |
| - } else { |
199 |
| - changeConnectionState(NetworkConnectionState::CLOSED); |
200 |
| - } |
201 |
| - |
202 |
| - } |
203 |
| - break; |
204 |
| - case NetworkConnectionState::ERROR: { |
205 |
| - |
206 |
| - } |
207 |
| - break; |
208 |
| - case NetworkConnectionState::CLOSED: { |
209 |
| - |
210 |
| - } |
211 |
| - break; |
| 93 | + case NetworkConnectionState::INIT: netConnectionState = update_handleInit (); break; |
| 94 | + case NetworkConnectionState::CONNECTING: netConnectionState = update_handleConnecting (); break; |
| 95 | + case NetworkConnectionState::CONNECTED: netConnectionState = update_handleConnected (); break; |
| 96 | + case NetworkConnectionState::GETTIME: netConnectionState = update_handleGetTime (); break; |
| 97 | + case NetworkConnectionState::DISCONNECTING: netConnectionState = update_handleDisconnecting(); break; |
| 98 | + case NetworkConnectionState::DISCONNECTED: netConnectionState = update_handleDisconnected (); break; |
| 99 | + case NetworkConnectionState::ERROR: break; |
| 100 | + case NetworkConnectionState::CLOSED: break; |
212 | 101 | }
|
213 |
| - } /* time bracket */ |
| 102 | + } |
214 | 103 | }
|
215 | 104 |
|
216 | 105 | /******************************************************************************
|
217 | 106 | PRIVATE MEMBER FUNCTIONS
|
218 | 107 | ******************************************************************************/
|
219 | 108 |
|
220 |
| -void WiFiConnectionHandler::changeConnectionState(NetworkConnectionState _newState) { |
221 |
| - if (_newState == netConnectionState) { |
| 109 | +void WiFiConnectionHandler::connect() { |
| 110 | + if (netConnectionState == NetworkConnectionState::INIT || netConnectionState == NetworkConnectionState::CONNECTING) { |
222 | 111 | return;
|
223 | 112 | }
|
224 |
| - int newInterval = CHECK_INTERVAL_INIT; |
225 |
| - switch (_newState) { |
226 |
| - case NetworkConnectionState::INIT: { |
227 |
| - Debug.print(DBG_VERBOSE, "CHANGING STATE TO ::INIT"); |
228 |
| - newInterval = CHECK_INTERVAL_INIT; |
229 |
| - } |
230 |
| - break; |
231 |
| - case NetworkConnectionState::CONNECTING: { |
232 |
| - Debug.print(DBG_INFO, "Connecting to \"%s\"", ssid); |
233 |
| - newInterval = CHECK_INTERVAL_CONNECTING; |
234 |
| - } |
235 |
| - break; |
236 |
| - case NetworkConnectionState::CONNECTED: { |
237 |
| - execNetworkEventCallback(_on_connect_event_callback, 0); |
238 |
| - newInterval = CHECK_INTERVAL_CONNECTED; |
239 |
| - } |
240 |
| - break; |
241 |
| - case NetworkConnectionState::GETTIME: { |
242 |
| - } |
243 |
| - break; |
244 |
| - case NetworkConnectionState::DISCONNECTING: { |
245 |
| - Debug.print(DBG_VERBOSE, "Disconnecting from \"%s\"", ssid); |
246 |
| - WiFi.disconnect(); |
247 |
| - } |
248 |
| - break; |
249 |
| - case NetworkConnectionState::DISCONNECTED: { |
250 |
| - execNetworkEventCallback(_on_disconnect_event_callback, 0); |
251 |
| - Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status()); |
252 |
| - |
253 |
| - Debug.print(DBG_ERROR, "Connection to \"%s\" lost.", ssid); |
254 |
| - if (keepAlive) { |
255 |
| - Debug.print(DBG_ERROR, "Attempting reconnection"); |
256 |
| - } |
257 |
| - |
258 |
| - newInterval = CHECK_INTERVAL_DISCONNECTED; |
259 |
| - } |
260 |
| - break; |
261 |
| - case NetworkConnectionState::CLOSED: { |
262 |
| - |
263 |
| - #if !defined(BOARD_ESP8266) |
264 |
| - WiFi.end(); |
265 |
| - #endif |
266 |
| - |
267 |
| - Debug.print(DBG_VERBOSE, "Connection to \"%s\" closed", ssid); |
268 |
| - } |
269 |
| - break; |
270 |
| - case NetworkConnectionState::ERROR: { |
271 |
| - execNetworkEventCallback(_on_error_event_callback, 0); |
272 |
| - Debug.print(DBG_ERROR, "WiFi Hardware failure.\nMake sure you are using a WiFi enabled board/shield."); |
273 |
| - Debug.print(DBG_ERROR, "Then reset and retry."); |
274 |
| - } |
275 |
| - break; |
| 113 | + keepAlive = true; |
| 114 | + connectionTickTimeInterval = CHECK_INTERVAL_INIT; |
| 115 | + netConnectionState = NetworkConnectionState::INIT; |
| 116 | +} |
| 117 | + |
| 118 | +void WiFiConnectionHandler::disconnect() { |
| 119 | + keepAlive = false; |
| 120 | + netConnectionState = NetworkConnectionState::DISCONNECTING; |
| 121 | +} |
| 122 | + |
| 123 | +NetworkConnectionState WiFiConnectionHandler::update_handleInit() { |
| 124 | + Debug.print(DBG_VERBOSE, "::INIT"); |
| 125 | + |
| 126 | +#ifndef BOARD_ESP8266 |
| 127 | + Debug.print(DBG_INFO, "WiFi.status(): %d", WiFi.status()); |
| 128 | + if (WiFi.status() == NETWORK_HARDWARE_ERROR) { |
| 129 | + execNetworkEventCallback(_on_error_event_callback, 0); |
| 130 | + Debug.print(DBG_ERROR, "WiFi Hardware failure.\nMake sure you are using a WiFi enabled board/shield."); |
| 131 | + Debug.print(DBG_ERROR, "Then reset and retry."); |
| 132 | + return NetworkConnectionState::ERROR; |
276 | 133 | }
|
277 |
| - connectionTickTimeInterval = newInterval; |
278 |
| - lastConnectionTickTime = millis(); |
279 |
| - netConnectionState = _newState; |
280 |
| - //connectionStateChanged(netConnectionState); |
| 134 | + |
| 135 | + Debug.print(DBG_ERROR, "Current WiFi Firmware: %s", WiFi.firmwareVersion()); |
| 136 | + |
| 137 | + if (WiFi.firmwareVersion() < WIFI_FIRMWARE_VERSION_REQUIRED) { |
| 138 | + Debug.print(DBG_ERROR, "Latest WiFi Firmware: %s", WIFI_FIRMWARE_VERSION_REQUIRED); |
| 139 | + Debug.print(DBG_ERROR, "Please update to the latest version for best performance."); |
| 140 | + delay(5000); |
| 141 | + } |
| 142 | +#else |
| 143 | + Debug.print(DBG_ERROR, "WiFi status ESP: %d", WiFi.status()); |
| 144 | + WiFi.disconnect(); |
| 145 | + delay(300); |
| 146 | + WiFi.begin(ssid, pass); |
| 147 | + delay(1000); |
| 148 | +#endif /* ifndef BOARD_ESP8266 */ |
| 149 | + |
| 150 | + connectionTickTimeInterval = CHECK_INTERVAL_CONNECTING; |
| 151 | + return NetworkConnectionState::CONNECTING; |
281 | 152 | }
|
282 | 153 |
|
283 |
| -void WiFiConnectionHandler::connect() { |
284 |
| - if (netConnectionState == NetworkConnectionState::INIT || netConnectionState == NetworkConnectionState::CONNECTING) { |
285 |
| - return; |
| 154 | +NetworkConnectionState WiFiConnectionHandler::update_handleConnecting() { |
| 155 | + Debug.print(DBG_VERBOSE, "::CONNECTING"); |
| 156 | + |
| 157 | +#ifndef BOARD_ESP8266 |
| 158 | + if (WiFi.status() != WL_CONNECTED) { |
| 159 | + WiFi.begin(ssid, pass); |
286 | 160 | }
|
287 |
| - keepAlive = true; |
288 |
| - changeConnectionState(NetworkConnectionState::INIT); |
| 161 | +#endif /* ifndef BOARD_ESP8266 */ |
289 | 162 |
|
| 163 | + Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status()); |
| 164 | + if (WiFi.status() != NETWORK_CONNECTED) { |
| 165 | + Debug.print(DBG_ERROR, "Connection to \"%s\" failed", ssid); |
| 166 | + Debug.print(DBG_INFO, "Retrying in \"%d\" milliseconds", connectionTickTimeInterval); |
| 167 | + return NetworkConnectionState::CONNECTING; |
| 168 | + } |
| 169 | + else { |
| 170 | + Debug.print(DBG_INFO, "Connected to \"%s\"", ssid); |
| 171 | + execNetworkEventCallback(_on_connect_event_callback, 0); |
| 172 | + connectionTickTimeInterval = CHECK_INTERVAL_CONNECTED; |
| 173 | + return NetworkConnectionState::GETTIME; |
| 174 | + } |
290 | 175 | }
|
291 |
| -void WiFiConnectionHandler::disconnect() { |
292 |
| - //WiFi.end(); |
293 | 176 |
|
294 |
| - changeConnectionState(NetworkConnectionState::DISCONNECTING); |
295 |
| - keepAlive = false; |
| 177 | +NetworkConnectionState WiFiConnectionHandler::update_handleConnected() { |
| 178 | + |
| 179 | + Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status()); |
| 180 | + if (WiFi.status() != WL_CONNECTED) |
| 181 | + { |
| 182 | + execNetworkEventCallback(_on_disconnect_event_callback, 0); |
| 183 | + |
| 184 | + Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status()); |
| 185 | + Debug.print(DBG_ERROR, "Connection to \"%s\" lost.", ssid); |
| 186 | + |
| 187 | + if (keepAlive) { |
| 188 | + Debug.print(DBG_ERROR, "Attempting reconnection"); |
| 189 | + } |
| 190 | + |
| 191 | + connectionTickTimeInterval = CHECK_INTERVAL_DISCONNECTED; |
| 192 | + return NetworkConnectionState::DISCONNECTED; |
| 193 | + } |
| 194 | + Debug.print(DBG_VERBOSE, "Connected to \"%s\"", ssid); |
| 195 | + return NetworkConnectionState::CONNECTED; |
| 196 | +} |
| 197 | + |
| 198 | +NetworkConnectionState WiFiConnectionHandler::update_handleGetTime() { |
| 199 | + Debug.print(DBG_VERBOSE, "NetworkConnectionState::GETTIME"); |
| 200 | +#ifdef BOARD_ESP8266 |
| 201 | + configTime(0, 0, "time.arduino.cc", "pool.ntp.org", "time.nist.gov"); |
| 202 | +#endif |
| 203 | + return NetworkConnectionState::CONNECTED; |
| 204 | +} |
| 205 | + |
| 206 | +NetworkConnectionState WiFiConnectionHandler::update_handleDisconnecting() { |
| 207 | + Debug.print(DBG_VERBOSE, "Disconnecting from \"%s\"", ssid); |
| 208 | + WiFi.disconnect(); |
| 209 | + return NetworkConnectionState::DISCONNECTED; |
296 | 210 | }
|
| 211 | + |
| 212 | +NetworkConnectionState WiFiConnectionHandler::update_handleDisconnected() { |
| 213 | +#ifndef BOARD_ESP8266 |
| 214 | + WiFi.end(); |
| 215 | +#endif /* ifndef BOARD_ESP8266 */ |
| 216 | + if (keepAlive) { |
| 217 | + connectionTickTimeInterval = CHECK_INTERVAL_INIT; |
| 218 | + return NetworkConnectionState::INIT; |
| 219 | + } |
| 220 | + else { |
| 221 | + Debug.print(DBG_VERBOSE, "Connection to \"%s\" closed", ssid); |
| 222 | + return NetworkConnectionState::CLOSED; |
| 223 | + } |
| 224 | +} |
| 225 | + |
297 | 226 | #endif /* #ifdef BOARD_HAS_WIFI */
|
0 commit comments