-
Notifications
You must be signed in to change notification settings - Fork 4
/
WiFiMgr.cpp
140 lines (106 loc) · 2.73 KB
/
WiFiMgr.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
//
//
//
#include "WiFiMgr.h"
WiFiMgrClass::WiFiMgrClass()
: _STAssid("")
, _STAkey("")
, _devicename("")
, _STAlastTry(0)
, _STAtryTimeout(10 * 1000)
, _APssid("")
, _APkey("")
, _APlastTry(0)
, _APtryTimeout(10 * 60 * 1000)
, _STAconnected(false)
, _STAIPip(192,168,1,1)
, _STAIPgw(192,168,1,1)
, _STAIPmask(255,255,255,0)
, _STAIPdns(192,168,1,1)
, _STADHCP(true)
{
_APssid = "ESP-" + String(ESP.getChipId(), HEX);
}
void WiFiMgrClass::setAPssid(String ssid, String key)
{
_APssid = ssid;
_APkey = key;
}
void WiFiMgrClass::setSTAIPdhcp(bool dhcp)
{
_STADHCP = dhcp;
}
void WiFiMgrClass::setSTAIPip(IPAddress ip, IPAddress gw, IPAddress mask, IPAddress dns)
{
_STAIPip = ip;
_STAIPgw = gw;
_STAIPmask = mask;
_STAIPdns = dns;
setSTAIPdhcp(false);
}
void WiFiMgrClass::tryToConnect(String ssid, String key, String devicename)
{
_STAssid = ssid;
_STAkey = key;
_devicename = devicename;
tryToReconnect();
}
void WiFiMgrClass::tryToReconnect()
{
// If STA mode is not configured, directly switch in AP mode
if (_STAssid.length() == 0) {
setAPMode();
return;
}
_STAconnected = false;
WiFi.mode(WIFI_STA);
wifi_station_set_hostname(_devicename.c_str()); //See more at : http ://www.esp8266.com/viewtopic.php?f=29&t=11124#sthash.458xtq2U.dpuf
Serial.print("Trying to connect to ");
Serial.print(_STAssid.c_str());
Serial.print(" with key : ");
Serial.println(_STAkey.c_str());
WiFi.begin(_STAssid, _STAkey);
WiFi.setSleepMode(WIFI_NONE_SLEEP);
_STAlastTry = millis();
}
void WiFiMgrClass::setAPMode()
{
IPAddress apIP(192, 168, 1, 1);
IPAddress apNetMsk(255, 255, 255, 0);
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, apNetMsk);
Serial.println("Setting AP mode");
if (_APkey.length() == 0)
WiFi.softAP(_APssid);
else
WiFi.softAP(_APssid, _APkey);
WiFi.setSleepMode(WIFI_NONE_SLEEP);
_APlastTry = millis();
_STAconnected = false;
}
bool WiFiMgrClass::handle()
{
// STA mode and connected. Do Nothing
if (WiFi.getMode() == WIFI_STA && WiFi.status() == WL_CONNECTED) {
if (_STAconnected)
return false;
Serial.println("WiFi Connected");
if (!_STADHCP)
WiFi.config(_STAIPip, _STAIPgw, _STAIPmask, _STAIPdns);
Serial.print("WiFi IP : ");
Serial.println(WiFi.localIP());
WiFi.setAutoReconnect(true);
_STAconnected = true;
return true;
}
// STA mode and not connected after timeout. Switch on AP mode
if (WiFi.getMode() == WIFI_STA && millis() - _STAlastTry > _STAtryTimeout) {
setAPMode();
}
// AP mode and timeout. Try to reconnect in STA mode
if (WiFi.getMode() == WIFI_AP && millis() - _APlastTry > _APtryTimeout) {
tryToReconnect();
}
return false;
}
WiFiMgrClass WiFiMgr;