-
Notifications
You must be signed in to change notification settings - Fork 7
/
HSDWifi.cpp
115 lines (94 loc) · 2.35 KB
/
HSDWifi.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
#include "HSDWifi.hpp"
#include <ESP8266WiFi.h>
#define SOFT_AP_SSID (F("StatusDisplay"))
#define SOFT_AP_PSK (F("statusdisplay"))
HSDWifi::HSDWifi(const HSDConfig& config)
:
m_config(config),
m_connectFailure(false),
m_maxConnectRetries(100),
m_numConnectRetriesDone(0),
m_retryDelay(500),
m_millisLastConnectTry(0),
m_accessPointActive(false),
m_lastConnectStatus(false)
{
}
void HSDWifi::begin()
{
// nothing to do right now
WiFi.persistent(false);
}
void HSDWifi::handleConnection()
{
bool isConnected = connected();
if(isConnected != m_lastConnectStatus)
{
if(isConnected)
{
Serial.print(F("WiFi connected with IP "));
Serial.print(WiFi.localIP());
Serial.println(F("."));
m_numConnectRetriesDone = 0;
}
else
{
Serial.println(F("WiFi connection lost."));
}
m_lastConnectStatus = isConnected;
}
if(!isConnected && !m_accessPointActive)
{
if(m_connectFailure)
{
startAccessPoint();
}
else
{
unsigned long currentMillis = millis();
if( (currentMillis - m_millisLastConnectTry) >= m_retryDelay)
{
m_millisLastConnectTry = currentMillis;
if(m_numConnectRetriesDone == 0)
{
Serial.print(F("Starting Wifi connection to "));
Serial.print(m_config.getWifiSSID());
Serial.println(F("..."));
WiFi.mode(WIFI_STA);
WiFi.begin(m_config.getWifiSSID(), m_config.getWifiPSK());
m_numConnectRetriesDone++;
}
else if(m_numConnectRetriesDone < m_maxConnectRetries)
{
m_numConnectRetriesDone++;
}
else
{
Serial.println(F("Failed to connect WiFi."));
m_connectFailure = true;
}
}
}
}
}
bool HSDWifi::connected()
{
return (WiFi.status() == WL_CONNECTED);
}
void HSDWifi::startAccessPoint()
{
Serial.println(F(""));
Serial.println(F("Starting access point."));
WiFi.mode(WIFI_AP);
if(WiFi.softAP(String(SOFT_AP_SSID).c_str(), String(SOFT_AP_PSK).c_str()))
{
IPAddress ip = WiFi.softAPIP();
Serial.print(F("AccessPoint SSID is ")); Serial.println(SOFT_AP_SSID);
Serial.print(F("IP: ")); Serial.println(ip);
m_accessPointActive = true;
}
else
{
Serial.println(F("Error starting access point."));
}
}