-
Notifications
You must be signed in to change notification settings - Fork 10
/
WiFiConnect.h
66 lines (54 loc) · 1.52 KB
/
WiFiConnect.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
#ifndef WiFiConnect_h
#define WiFiConnect_h
#include <WiFiManager.h>
#include "ezTime.h"
WiFiManager wifiManager;
Timezone myTZ;
struct WiFiConnect
{
std::vector<const char *> _menu = {"wifi","exit"};
char timezone[40]="0";
bool shouldSaveConfig = false;
void saveTimezone(const char* value) {
Serial.print("Save: ");
Serial.println(value);
myTZ.setCache(0);
myTZ.setLocation(value);
shouldSaveConfig = false;
}
String loadTimezone() {
myTZ.setCache(0);
String tz = myTZ.getTimezoneName();
Serial.print("Load: ");
Serial.println(tz);
return tz;
}
void connect()
{
bool resp;
//wifiManager.resetSettings();
wifiManager.setSaveConfigCallback([&](){ shouldSaveConfig = true; });
sprintf(timezone, "%s", loadTimezone().c_str());
WiFiManagerParameter timezoneParam("tz", "Inform your timezone (e.g. America/Lima)", timezone, 36);
wifiManager.setTitle("Clockwise Wifi Setup");
wifiManager.setMenu(_menu);
wifiManager.addParameter(&timezoneParam);
resp = wifiManager.autoConnect("Clockwise-Wifi", "12345678");
if (!resp) {
Serial.println("Failed to connect");
delay(3000);
ESP.restart();
}
else {
Serial.println("connected!");
if (shouldSaveConfig) {
saveTimezone(timezoneParam.getValue());
}
}
}
boolean isConnected()
{
return WiFi.status() == WL_CONNECTED;
}
};
#endif