-
Notifications
You must be signed in to change notification settings - Fork 1
/
wifi.cpp
71 lines (58 loc) · 1.91 KB
/
wifi.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
#include "wifi.h"
#include <ESP8266WiFi.h>
#include <string.h>
#define TIMEOUT 240
#ifdef ESP8266
extern "C" {
#include "user_interface.h"
}
#endif
WifiConnect::WifiConnect(const char* hostname) {
WiFi.hostname(hostname);
Serial.println("Try to connect");
}
bool WifiConnect::startWPSPBC() {
Serial.printf("\nWPS config start\n");
// WPS works in STA (Station mode) only -> not working in WIFI_AP_STA !!!
Serial.printf("Trying to connect to %s with saved config ...", WiFi.SSID().c_str());
WiFi.mode(WIFI_STA);
WiFi.begin(WiFi.SSID().c_str(), WiFi.psk().c_str());
int timeout = 0;
while ((WiFi.status() == WL_DISCONNECTED) && (timeout++ <TIMEOUT)) {
delay(500);
Serial.print(".");
}
Serial.print("|");
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Fail");
Serial.println("Press WPS button on your router");
Serial.print("Trying to connect with WPS ...");
bool wpsSuccess = WiFi.beginWPSConfig();
if(wpsSuccess) {
// Well this means not always success :-/ in case of a timeout we have an empty ssid
String newSSID = WiFi.SSID();
if(newSSID.length() > 0) {
// WPSConfig has already connected in STA mode successfully to the new station.
Serial.printf("WPS finished. Connected successfull to SSID '%s'\n", newSSID.c_str());
// save to config and use next time or just use - WiFi.begin(WiFi.SSID().c_str(),WiFi.psk().c_str());
} else {
wpsSuccess = false;
}
}
if (wpsSuccess) {
Serial.println("SUCCESS");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
return wpsSuccess;
}
if (timeout >= TIMEOUT) {
Serial.printf("Wifi error\n");
return false;
} else {
Serial.printf("Done\n");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
return true;
}
}