forked from arduino-libraries/Arduino_ConnectionHandler
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWiFiConnectionHandler.cpp
More file actions
219 lines (189 loc) · 6.46 KB
/
WiFiConnectionHandler.cpp
File metadata and controls
219 lines (189 loc) · 6.46 KB
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
217
218
219
/*
This file is part of the Arduino_ConnectionHandler library.
Copyright (c) 2019 Arduino SA
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/******************************************************************************
INCLUDE
******************************************************************************/
#include "ConnectionHandlerDefinitions.h"
#ifdef BOARD_HAS_WIFI /* Only compile if the board has WiFi */
#include "WiFiConnectionHandler.h"
#include "connectionHandlerUtils/wifiSupport.h"
/******************************************************************************
CONSTANTS
******************************************************************************/
#if defined(ARDUINO_ARCH_ESP8266)
static int const ESP_WIFI_CONNECTION_TIMEOUT = 3000;
#endif
/******************************************************************************
CTOR/DTOR
******************************************************************************/
WiFiConnectionHandler::WiFiConnectionHandler()
: ConnectionHandler(true, NetworkAdapter::WIFI) {
}
WiFiConnectionHandler::WiFiConnectionHandler(char const * ssid, char const * pass, bool const keep_alive)
: ConnectionHandler{keep_alive, NetworkAdapter::WIFI}
{
_settings.type = NetworkAdapter::WIFI;
strncpy(_settings.wifi.ssid, ssid, sizeof(_settings.wifi.ssid)-1);
strncpy(_settings.wifi.pwd, pass, sizeof(_settings.wifi.pwd)-1);
}
/******************************************************************************
PUBLIC MEMBER FUNCTIONS
******************************************************************************/
unsigned long WiFiConnectionHandler::getTime()
{
#if defined(ARDUINO_ARCH_ZEPHYR)
return time(NULL);
#elif !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32)
return WiFi.getTime();
#else
return 0;
#endif
}
int WiFiConnectionHandler::ping(IPAddress ip, uint8_t ttl, uint8_t count) {
#if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32) && !defined(ARDUINO_ARCH_ZEPHYR)
return WiFi.ping(ip);
#else
return 0;
#endif
}
int WiFiConnectionHandler::ping(const String &hostname, uint8_t ttl, uint8_t count) {
#if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32) && !defined(ARDUINO_ARCH_ZEPHYR)
return WiFi.ping(hostname);
#else
return 0;
#endif
}
int WiFiConnectionHandler::ping(const char* host, uint8_t ttl, uint8_t count) {
#if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32) && !defined(ARDUINO_ARCH_ZEPHYR)
return WiFi.ping(host);
#else
return 0;
#endif
}
/******************************************************************************
PROTECTED MEMBER FUNCTIONS
******************************************************************************/
NetworkConnectionState WiFiConnectionHandler::update_handleInit()
{
#if !defined(__AVR__)
DEBUG_INFO(F("WiFi.status(): %d"), WiFi.status());
#endif
#if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32)
if (!isWifiModulePresent() || WiFi.status() == NETWORK_HARDWARE_ERROR)
{
#if !defined(__AVR__)
DEBUG_ERROR(F("WiFi Hardware failure.\nMake sure you are using a WiFi enabled board/shield."));
DEBUG_ERROR(F("Then reset and retry."));
#endif
return NetworkConnectionState::ERROR;
}
#if !defined(__AVR__)
DEBUG_INFO(F("Current WiFi Firmware: %s"), WiFi.firmwareVersion());
#endif
#if defined(WIFI_FIRMWARE_VERSION_REQUIRED)
if (String(WiFi.firmwareVersion()) < String(WIFI_FIRMWARE_VERSION_REQUIRED))
{
#if !defined(__AVR__)
DEBUG_ERROR(F("Latest WiFi Firmware: %s"), WIFI_FIRMWARE_VERSION_REQUIRED);
DEBUG_ERROR(F("Please update to the latest version for best performance."));
#endif
delay(5000);
}
#endif
#else
WiFi.mode(WIFI_STA);
#endif /* #if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32) */
if (WiFi.status() != WL_CONNECTED)
{
WiFi.begin(_settings.wifi.ssid, _settings.wifi.pwd);
#if defined(ARDUINO_ARCH_ESP8266)
/* Wait connection otherwise board won't connect */
unsigned long start = millis();
while((WiFi.status() != WL_CONNECTED) && (millis() - start) < ESP_WIFI_CONNECTION_TIMEOUT) {
delay(100);
}
#endif
}
if (WiFi.status() != NETWORK_CONNECTED)
{
#if !defined(__AVR__)
DEBUG_ERROR(F("Connection to \"%s\" failed"), _settings.wifi.ssid);
DEBUG_INFO(F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.init);
#endif
return NetworkConnectionState::INIT;
}
else
{
#if !defined(__AVR__)
DEBUG_INFO(F("Connected to \"%s\""), _settings.wifi.ssid);
#endif
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
configTime(0, 0, "time.arduino.cc", "pool.ntp.org", "time.nist.gov");
#endif
return NetworkConnectionState::CONNECTING;
}
}
NetworkConnectionState WiFiConnectionHandler::update_handleConnecting()
{
if (WiFi.status() != WL_CONNECTED){
return NetworkConnectionState::INIT;
}
if(!_check_internet_availability){
return NetworkConnectionState::CONNECTED;
}
#if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32)
int ping_result = ping("time.arduino.cc");
DEBUG_INFO(F("WiFi.ping(): %d"), ping_result);
if (ping_result < 0)
{
DEBUG_ERROR(F("Internet check failed"));
DEBUG_INFO(F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.connecting);
return NetworkConnectionState::CONNECTING;
}
#endif
DEBUG_INFO(F("Connected to Internet"));
return NetworkConnectionState::CONNECTED;
}
NetworkConnectionState WiFiConnectionHandler::update_handleConnected()
{
if (WiFi.status() != WL_CONNECTED)
{
#if !defined(__AVR__)
DEBUG_VERBOSE(F("WiFi.status(): %d"), WiFi.status());
DEBUG_ERROR(F("Connection to \"%s\" lost."), _settings.wifi.ssid);
#endif
if (_keep_alive)
{
#if !defined(__AVR__)
DEBUG_INFO(F("Attempting reconnection"));
#endif
}
return NetworkConnectionState::DISCONNECTED;
}
return NetworkConnectionState::CONNECTED;
}
NetworkConnectionState WiFiConnectionHandler::update_handleDisconnecting()
{
WiFi.disconnect();
return NetworkConnectionState::DISCONNECTED;
}
NetworkConnectionState WiFiConnectionHandler::update_handleDisconnected()
{
#if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32)
WiFi.end();
#endif /* #if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32) */
if (_keep_alive)
{
return NetworkConnectionState::INIT;
}
else
{
return NetworkConnectionState::CLOSED;
}
}
#endif /* #ifdef BOARD_HAS_WIFI */