-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWLAN.h
145 lines (135 loc) · 4.83 KB
/
WLAN.h
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
void startAP(const char* ssid, const char* password) {
/*
* This starts AP only, probably because we had no stored STA info.
*/
WiFi.mode(WIFI_AP);
debugMsg(F("Starting AP, SSID \""),1);
debugMsg(String(ssid),1);
debugMsg(F("\", pass \""),1);
debugMsg(String(password),1);
debugMsgln(F("\""),1);
if (strlen(password) > 0) {
WiFi.softAP(ssid, password, 6, false, 8);
} else {
WiFi.softAP(ssid);
}
}
boolean connectToWLAN(const char* ssid = "", const char* password = "") {
/*
* Try to connect as a station with the given info.
* If no info provided, read stored info from eeprom. Give up after a while
* if we can't get in. Returns connected status.
*/
int retries = 0;
WiFi.persistent(true);
if (strlen(ssid)>0) { // here we try to connect using the info passed to us
debugMsg(F("clearing wifiMulti"),3);
wifiMulti.~WiFiMulti(); // clear
wlan_count = 1 ;
if (password && strlen(password) > 0 ) {
debugMsg(F("wifiMulti adding SSID: "),3);
debugMsgln(ssid,3);
debugMsg(F("wifiMulti with pass: "),9);
debugMsgln(password,9);
wifiMulti.addAP(ssid, password); //esid.c_str()?
} else {
wifiMulti.addAP(ssid);
}
} else { // if we weren't given info, look it up
getWLANs();
int i;
for (i = 0; i<=3; i++) {
if (esid[i] != "") {
if ( epass[i] != "" ) { // got both ssid and pass
if (!wlanSet) { // skip if already there
debugMsg(F("wifiMulti adding SSID: "),3);
debugMsgln(esid[i],3);
debugMsg(F("wifiMulti with pass: "),3);
debugMsg(epass[i],9);
debugMsgln("",3);
wifiMulti.addAP(esid[i].c_str(), epass[i].c_str());
wlan_count++ ;
}
} else { // only ssid, no pass
if (!wlanSet) { // skip if already there
debugMsg(F("wifiMulti adding SSID: "),3);
debugMsgln(esid[i],3);
wifiMulti.addAP(esid[i].c_str());
wlan_count++ ;
}
}
}
} // done getting WLANs
wlanSet = true;
} // else, didn't get passed WLAN info
#ifdef WIFI_MODE_AP_STA // mode controlled by #define
WiFi.mode(WIFI_AP_STA);
if (strlen(ap_password.c_str()) > 0) {
WiFi.softAP(ap_ssid.c_str(), ap_password.c_str(), 6, false, 8);
} else {
WiFi.softAP(ap_ssid.c_str());
}
#else
WiFi.mode(WIFI_STA);
debugMsgln(F("WLAN changing to station mode."),1);
#endif
WiFi.setHostname(my_hostname.c_str()); // TODO not working
debugMsg(F("Hostname: "),1);
debugMsgln(WiFi.getHostname(),1);
if (wlan_count) { // if we have WLANs configured for station mode, try to connect
tryWLAN();
} else { // no wlan_count
debugMsgln(F("No WLANs configured"),1);
return false;
}
return true;
}
/*
* If we have configured WLANs and are not connected as a station, try to connect every few minutes
* Only if running as both AP and STA (doesn't take AP down, but will be non-responsive to client
* while scanning). If running as STA -or- AP, don't scan if client is connected.
*/
void tryWLAN() {
#ifdef WIFI_AP_STA // if AP_STA mode, ok to try with client connected
if ( wlan_count && !WiFi.isConnected() ) {
#else // if not, would disconnect client, so check
if ( wlan_count && !WiFi.isConnected() && !WiFi.softAPgetStationNum() ) {
#endif
debugMsgln(F("Trying WLAN connection"),2);
int retries=0;
#ifndef WIFI_MODE_AP_STA // we're in AP mode, switch to try
debugMsgln(F("WLAN switching to STA"),2);
WiFi.mode(WIFI_STA);
#endif
debugMsg(F("Trying to connect to WLAN ("),2);
debugMsg(String(wlan_count),2);
debugMsg(")",2);
while ( wifiMulti.run() != WL_CONNECTED ) {
delay(500);
debugMsg(".",2);
retries++;
if (retries > 5) { // try for a while
debugMsgln("",2);
debugMsgln(F("Failed to connect"),2);
#ifndef WIFI_MODE_AP_STA // go back to AP mode so users can connect
debugMsgln(F("WLAN switching back to AP"),2);
startAP(ap_ssid.c_str(), ap_password.c_str());
#endif
return;
}
} // while
debugMsgln("",2);
IPAddress ip = WiFi.localIP();
debugMsg(F("WLAN IP address: "),1);
debugMsgln(formatIPAsString(ip),1);
debugMsgln("Connected to: " + String(WiFi.SSID()),1);
if (!MDNS.begin(my_hostname.c_str())) {
debugMsgln(F("Error setting up MDNS responder!"),1);
} else {
MDNS.addService("_http", "_tcp", 80);
// MDNS.addServiceTxt("_http", "http://"+my_hostname+"/status");
MDNS.addService("_mbap", "_tcp", 502); // modbus application protocol, from IANA
debugMsgln(F("mDNS responder started"),1);
}
}
} // tryWLAN()