-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcWiFi.h
174 lines (159 loc) · 4.35 KB
/
cWiFi.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
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
class cWiFi {
private:
#define pinAP D0 //WiFi AP switch
const byte pinBlue = D4; //D4=02 on-board diod
unsigned long lastReconnect; //wifi connection try
int wifix; //state and tries to connect to wifi
Memory *mem;
bool connectedJob = true; //do jobs once when connected
void device(String deviceName_ = "") {
report("Device: " + deviceName_, true);
uint8_t mac[6];
WiFi.macAddress(mac);
String macStr = macToStr(mac);
macStr.toUpperCase();
report("MAC " + macStr);
if (deviceName_ != "") WiFi.hostname(deviceName_.c_str());
}
void disconnect() {
if (APmode == true) {
WiFi.softAPdisconnect();
report("WiFi AP disconnected, stations: " + String(WiFi.softAPgetStationNum()));
} else {
WiFi.disconnect();
report("WiFi disconnected, status: " + String(WiFi.status()));
}
}
void reconnect() {
reconnectNeeded = false;
disconnect();
if (mem->read(3) == 0) {
beginAP();
} else {
begin(deviceName);
}
}
void sleep() {
if (digitalRead(pinAP) == LOW and sleeping == true) {
sleeping = false;
WiFi.forceSleepWake();
delay(10);
report("modem sleep: OFF", true);
reconnect();
} else if (digitalRead(pinAP) == HIGH and sleeping == false) {
sleeping = true;
disconnect();
WiFi.forceSleepBegin();
delay(10);
report("modem sleep: ON", true);
}
}
void led(bool state) {
if (state == true) {
if (LEDextra == true) {
analogWrite(pinBlue, 177);
} else {
digitalWrite(LED_BUILTIN, LOW);
}
} else {
if (LEDextra == true) {
analogWrite(pinBlue, 0);
} else {
digitalWrite(LED_BUILTIN, HIGH);
}
}
}
void report(String msg, bool offset = false) {
if(offset) {Serial.println("");}
Serial.println(msg);
}
String macToStr(const uint8_t* mac){
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5){
result += ':';
}
}
return result;
}
public:
String ssid = "";
String password = "";
int switchAP; //Wifi switch AP or modem-sleep exists
bool LEDextra; //external-led instead of board-led
bool APmode;
bool sleeping;
bool reconnectNeeded; // force reconnect
String deviceName;
String IP;
cWiFi(Memory *eprom, bool extraLED = false) {
mem = eprom;
LEDextra = extraLED;
}
bool connected() {
if (switchAP == 2) sleep();
if (sleeping == true) return false;
if (reconnectNeeded == true) reconnect();
if ((APmode == false and WiFi.status() == WL_CONNECTED) or (APmode == true and WiFi.softAPgetStationNum() > 0)) {
return true;
} else {
if (APmode == false) {
if (millis() - lastReconnect >= 1000UL or lastReconnect == 0) {
lastReconnect = millis();
Serial.print(".");
if (wifix < 3600) wifix += 1;
if (wifix == 15 and mem->read(3) != 2) {
mem->write(0, 3);
report("WiFi status " + String(WiFi.status() + "."));
if (String(WiFi.status()) == "7") report("IP not provided, router restart needed.");
}
//1:30 trvá restart modemu WIFI //WiFi.printDiag(Serial);
led(true);
}
}
connectedJob = true;
return false;
}
}
bool connectedJobs() { //do jobs once when connected
if (connectedJob == true) {
connectedJob = false;
report("WiFi connected.");
if (APmode == false) {
IP = WiFi.localIP().toString();
report("Connection created in " + String(wifix) + " seconds");
mem->write(2, 3);
} else {
IP = WiFi.softAPIP().toString();
}
report("IP address: " + IP);
led(false);
return true;
} else {
return false;
}
}
void begin(String deviceName_) {
deviceName = deviceName_;
APmode = false;
wifix = 0;
device(deviceName);
report("WIFI connecting to: " + ssid);
WiFi.begin(ssid.c_str(), password.c_str());
led(true);
}
void beginAP() {
APmode = true;
led(false);
device();
report("WIFI AP creating...");
while (!WiFi.softAP("pyramidak", "")) {
report("WIFI AP failed:");
WiFi.printDiag(Serial);
delay(3000);
}
report("WIFI AP ready: pyramidak");
led(true);
}
};