-
Notifications
You must be signed in to change notification settings - Fork 0
/
WifiManager.cpp
53 lines (41 loc) · 989 Bytes
/
WifiManager.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
#include "WifiManager.h"
WifiManager::WifiManager(Config* config) {
this->config = config;
this->lastConnectionAttempt = 0;
}
wifi_result WifiManager::connect() {
WiFi.disconnect();
WiFi.mode(WIFI_STA);
WiFi.hostname(config->get_hostname());
WiFi.begin(config->get_ssid(), config->get_passkey());
int WiFiCounter = 0;
return waitForConnection();
}
bool WifiManager::connected() {
return WiFi.status() == WL_CONNECTED;
}
wifi_result WifiManager::loop() {
if(!connected()) {
return connect();
}
return E_WIFI_OK;
}
wifi_result WifiManager::waitForConnection() {
uint8_t status;
long now = millis();
lastConnectionAttempt = now;
while(true) {
now = millis();
if(now - lastConnectionAttempt > WIFI_TIMEOUT) {
return WIFI_TIMEOUT;
}
status = WiFi.status();
switch(status) {
case WL_CONNECTED:
return E_WIFI_OK;
case WL_CONNECT_FAILED:
return E_WIFI_CONNECT_FAILED;
}
delay(100);
}
}