This repository has been archived by the owner on Nov 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
WifiManager.ino
186 lines (161 loc) · 7.68 KB
/
WifiManager.ino
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
bool doWifiConnect() {
String _ssid = WiFi.SSID();
String _psk = WiFi.psk();
String _pskMask = "";
for (int i = 0; i < _psk.length(); i++) {
_pskMask += "*";
}
DEBUG("ssid = " + _ssid + ", psk = " + _pskMask);
const char* ipStr = NetConfig.ip; byte ipBytes[4]; parseBytes(ipStr, '.', ipBytes, 4, 10);
const char* netmaskStr = NetConfig.netmask; byte netmaskBytes[4]; parseBytes(netmaskStr, '.', netmaskBytes, 4, 10);
const char* gwStr = NetConfig.gw; byte gwBytes[4]; parseBytes(gwStr, '.', gwBytes, 4, 10);
if (!startWifiManager && _ssid != "" && _psk != "" ) {
DEBUG(F("Connecting WLAN the classic way..."));
WiFi.mode(WIFI_STA);
WiFi.hostname(GlobalConfig.Hostname);
WiFi.setSleepMode(WIFI_NONE_SLEEP);
WiFi.setAutoReconnect(true);
if (String(NetConfig.ip) != "0.0.0.0") {
WiFi.config(IPAddress(ipBytes[0], ipBytes[1], ipBytes[2], ipBytes[3]), IPAddress(gwBytes[0], gwBytes[1], gwBytes[2], gwBytes[3]), IPAddress(netmaskBytes[0], netmaskBytes[1], netmaskBytes[2], netmaskBytes[3]));
ETS_UART_INTR_DISABLE();
wifi_station_disconnect();
ETS_UART_INTR_ENABLE();
}
WiFi.begin(_ssid.c_str(), _psk.c_str());
int waitCounter = 0;
while (WiFi.status() != WL_CONNECTED) {
waitCounter++;
Serial.print(".");
digitalWrite(LEDPinHVIO, (!(digitalRead(LEDPinHVIO))));
digitalWrite(LEDPinDual, (!(digitalRead(LEDPinDual))));
if (waitCounter == 30) {
return false;
}
delay(500);
}
DEBUG("Wifi Connected");
WiFiConnected = true;
return true;
} else {
WiFiManager wifiManager;
wifiManager.setDebugOutput(wifiManagerDebugOutput);
pinMode(LEDPinDual, OUTPUT);
digitalWrite(LEDPinDual, LOW);
digitalWrite(LEDPinHVIO, LOW);
wifiManager.setAPCallback(configModeCallback);
wifiManager.setSaveConfigCallback(saveConfigCallback);
WiFiManagerParameter custom_ccuip("ccu", "IP der CCU2", GlobalConfig.ccuIP, IPSIZE, 0, "pattern='((^|\\.)((25[0-5])|(2[0-4]\\d)|(1\\d\\d)|([1-9]?\\d))){4}$'");
//WiFiManagerParameter custom_loxusername("loxusername", "Loxone Username", "", VARIABLESIZE);
//WiFiManagerParameter custom_loxpassword("loxpassword", "Loxone Password", "", VARIABLESIZE,4);
WiFiManagerParameter custom_loxudpport("loxudpport", "Loxone UDP Port", LoxoneConfig.UDPPort, 10, 0, "pattern='[0-9]{1,5}'");
WiFiManagerParameter custom_devicename("devicename", "Gerätename", GlobalConfig.DeviceName, VARIABLESIZE, 0, "pattern='[A-Za-z0-9_ -]+'");
String backend = "";
switch (GlobalConfig.BackendType) {
case BackendType_HomeMatic:
backend = F("<option selected value='0'>HomeMatic</option><option value='1'>Loxone</option>");
break;
case BackendType_Loxone:
backend = F("<option value='0'>HomeMatic</option><option selected value='1'>Loxone</option>");
break;
default:
backend = F("<option value='0'>HomeMatic</option><option value='1'>Loxone</option>");
break;
}
WiFiManagerParameter custom_backendtype("backendtype", "Backend", "", 8, 2, backend.c_str());
String model = "";
switch (GlobalConfig.Model) {
case Model_Dual:
model = F("<option selected value='0'>Sonoff Dual</option><option value='1'>HVIO</option><option value='2'>Sonoff Dual R2</option>");
break;
case Model_HVIO:
model = F("<option value='0'>Sonoff Dual</option><option selected value='1'>HVIO</option><option value='2'>Sonoff Dual R2</option>");
break;
case Model_DualR2:
model = F("<option value='0'>Sonoff Dual</option><option value='1'>HVIO</option><option selected value='2'>Sonoff Dual R2</option>");
break;
default:
model = F("<option selected value='0'>Sonoff Dual</option><option value='1'>HVIO</option><option value='2'>Sonoff Dual R2</option>");
break;
}
WiFiManagerParameter custom_model("model", "Modell", "", 8, 2, model.c_str());
WiFiManagerParameter custom_ip("custom_ip", "IP-Adresse", (String(NetConfig.ip) != "0.0.0.0") ? NetConfig.ip : "", IPSIZE, 0, "pattern='((^|\\.)((25[0-5])|(2[0-4]\\d)|(1\\d\\d)|([1-9]?\\d))){4}$'");
WiFiManagerParameter custom_netmask("custom_netmask", "Netzmaske", (String(NetConfig.netmask) != "0.0.0.0") ? NetConfig.netmask : "", IPSIZE, 0, "pattern='((^|\\.)((25[0-5])|(2[0-4]\\d)|(1\\d\\d)|([1-9]?\\d))){4}$'");
WiFiManagerParameter custom_gw("custom_gw", "Gateway", (String(NetConfig.gw) != "0.0.0.0") ? NetConfig.gw : "", IPSIZE, 0, "pattern='((^|\\.)((25[0-5])|(2[0-4]\\d)|(1\\d\\d)|([1-9]?\\d))){4}$'");
WiFiManagerParameter custom_text("<br/><br><div>Statische IP (wenn leer, dann DHCP):</div>");
wifiManager.addParameter(&custom_model);
wifiManager.addParameter(&custom_ccuip);
//wifiManager.addParameter(&custom_loxusername);
//wifiManager.addParameter(&custom_loxpassword);
wifiManager.addParameter(&custom_loxudpport);
wifiManager.addParameter(&custom_devicename);
wifiManager.addParameter(&custom_backendtype);
wifiManager.addParameter(&custom_text);
wifiManager.addParameter(&custom_ip);
wifiManager.addParameter(&custom_netmask);
wifiManager.addParameter(&custom_gw);
String Hostname = "DualSwitch-" + WiFi.macAddress();
wifiManager.setConfigPortalTimeout(ConfigPortalTimeout);
if (startWifiManager == true) {
if (_ssid == "" || _psk == "" ) {
wifiManager.resetSettings();
}
else {
if (!wifiManager.startConfigPortal(Hostname.c_str())) {
DEBUG(F("WM: failed to connect and hit timeout"));
delay(500);
ESP.restart();
}
}
}
wifiManager.setSTAStaticIPConfig(IPAddress(ipBytes[0], ipBytes[1], ipBytes[2], ipBytes[3]), IPAddress(gwBytes[0], gwBytes[1], gwBytes[2], gwBytes[3]), IPAddress(netmaskBytes[0], netmaskBytes[1], netmaskBytes[2], netmaskBytes[3]));
wifiManager.autoConnect(Hostname.c_str());
DEBUG(F("Wifi Connected"));
DEBUG("CUSTOM STATIC IP: " + String(NetConfig.ip) + " Netmask: " + String(NetConfig.netmask) + " GW: " + String(NetConfig.gw));
if (wm_shouldSaveConfig) {
if (String(custom_ip.getValue()).length() > 5) {
DEBUG("Custom IP Address is set!");
strcpy(NetConfig.ip, custom_ip.getValue());
strcpy(NetConfig.netmask, custom_netmask.getValue());
strcpy(NetConfig.gw, custom_gw.getValue());
} else {
strcpy(NetConfig.ip, "0.0.0.0");
strcpy(NetConfig.netmask, "0.0.0.0");
strcpy(NetConfig.gw, "0.0.0.0");
}
GlobalConfig.BackendType = (atoi(custom_backendtype.getValue()));
GlobalConfig.Model = (atoi(custom_model.getValue()));
strcpy(GlobalConfig.ccuIP, custom_ccuip.getValue());
strcpy(GlobalConfig.DeviceName, custom_devicename.getValue());
//strcpy(LoxoneConfig.Username, custom_loxusername.getValue());
//strcpy(LoxoneConfig.Password, custom_loxpassword.getValue());
strcpy(LoxoneConfig.UDPPort, custom_loxudpport.getValue());
saveSystemConfig();
delay(100);
ESP.restart();
}
return true;
}
}
void configModeCallback (WiFiManager *myWiFiManager) {
DEBUG("AP-Modus ist aktiv!");
}
void saveConfigCallback () {
DEBUG("Should save config");
wm_shouldSaveConfig = true;
}
void parseBytes(const char* str, char sep, byte* bytes, int maxBytes, int base) {
for (int i = 0; i < maxBytes; i++) {
bytes[i] = strtoul(str, NULL, base);
str = strchr(str, sep);
if (str == NULL || *str == '\0') {
break;
}
str++;
}
}
void printWifiStatus() {
DEBUG("SSID: " + WiFi.SSID());
DEBUG("IP Address: " + IpAddress2String(WiFi.localIP()));
DEBUG("Gateway Address: " + IpAddress2String(WiFi.gatewayIP()));
DEBUG("signal strength (RSSI):" + String(WiFi.RSSI()) + " dBm");
}