-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.h
278 lines (251 loc) · 8.07 KB
/
functions.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/***********************************************/
/* FILE SYSTEM */
/***********************************************/
void formatFS() {
Serial.println("Formatting SPIFFS");
SPIFFS.format();
Serial.println("DONE, U're clean kiddo!");
}
void rebootESP() {
Serial.println("reboot in 2 sec");
delay(2000);
ESP.reset();
delay(2000);
}
/***********************************************/
/* WiFI */
/***********************************************/
bool APStart = false;
bool STAStart = false;
bool scanWiFi() {
String findWiFi[30]; //set max WiFi scanned to 30
bool networkScanResult = false;
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.println("Setup done");
delay(500);
Serial.println("scan start");
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0) {
Serial.println("no networks found. Start AP mode");
}
else
{
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
findWiFi[i] = String( WiFi.SSID(i));
Serial.print("SSID [");
Serial.print(i + 1);
Serial.print("]");
Serial.println(findWiFi[i]);
if (String(findWiFi[i]) == String (wifiValues[0])) {
networkScanResult = Found_WiFi;
Serial.println("FOUND WIFI!!!");
break;
}
}
}
return networkScanResult;
}
void startAP() {
Serial.println("Configuring access point...");
WiFi.softAP(deviceName);
IPAddress APIP = WiFi.softAPIP();
myIP = String(APIP[0]) + String(".") + \
String(APIP[1]) + String(".") + \
String(APIP[2]) + String(".") + \
String(APIP[3]);
Serial.println(myIP);
Serial.print("AP IP address: ");
Serial.println(APIP);
myMAC = WiFi.macAddress();
APStart = true;
}
void setup_wifi(char ssid[], char pswd[]) {
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.hostname(deviceName);
WiFi.begin(ssid, pswd);
uint64_t millisAtConnection = millis();
uint8_t wifiStatus = 0;
while (wifiStatus != WL_CONNECTED ) {
if (wifiStatus == 4) {
Serial.printf("password is incorrect \nStart AP mode");
startAP();
break;
}
if ((unsigned long)(millis() - millisAtConnection) >= WiFiTimeoutInSeconds * 1000) {
Serial.println();
Serial.print("couldn't connect: ");
switch (wifiStatus) {
case 0: Serial.println("Wi-Fi is in process of changing between statuses");
break;
case 1: Serial.println("configured SSID cannot be reached");
break;
//case WL_CONNECTED: Serial.println("actually is't connected just after hitting timeout (this is happening once every 10^1000000 times...)");
// break;
//case 4: Serial.println("password is incorrect");
// break;
case 6: Serial.println("module is not configured in station mode (more likley too short timeout time)");
break;
default: Serial.println("REASON UNKNOWN");
break;
}
Serial.println("hit time out... starting AP");
startAP();
break;
}
wifiStatus = WiFi.status();
delay(500);
Serial.print(".");
}
if (wifiStatus == WL_CONNECTED) {
STAStart = true;
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
IPAddress ipAddress = WiFi.localIP();
myIP = String(ipAddress[0]) + String(".") + \
String(ipAddress[1]) + String(".") + \
String(ipAddress[2]) + String(".") + \
String(ipAddress[3]) ;
Serial.println(myIP);
client.publish("dht/1/ip", String(myIP).c_str());
myMAC = WiFi.macAddress();
}
}
void StartWiFi() {
if (scanWiFi()) {
setup_wifi(wifiValues[0], wifiValues[1]);
} else {
Serial.println();
startAP();
}
}
/***********************************************/
/* MQTT */
/***********************************************/
boolean reconnect();
void callback(char* p_topic, byte* p_payload, unsigned int p_length);
uint64_t lastReconnectAttempt = 0;
void nonBlockingMQTTConnection() {
randomSeed(micros());
if (!client.connected()) {
mqttConnectionStatus = false;
unsigned long now = millis();
if (now - lastReconnectAttempt > 5000) {
lastReconnectAttempt = now;
if (reconnect()) {
lastReconnectAttempt = 0;
mqttConnectionStatus = true;
}
}
}
else {
client.loop();
}
}//nonBlockingMQTTConnection
bool startMQTT() {
if (String(mqttValues[0]) == "active") {
client.setServer(mqttValues[1], atoi(mqttValues[2]));
client.setCallback(callback);
Serial.println("MQTT client is now ready for use");
mqttInit = true;
} else {
Serial.print("MQTT is disabled");
mqttInit = false;
}
return mqttInit;
}//startMQTT
bool saveConfig(char names[][NAME_BUFFER_SIZE], char values[][VALUE_BUFFER_SIZE], const char path[], uint8_t nrParameters) {
Serial.println("saving config");
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
for (int i = 0; i < nrParameters; i++) json[names[i]] = values[i];
File configFile = SPIFFS.open(path, "w");
if (!configFile) {
Serial.println("Failed to open config file for writing");
return false;
}
json.printTo(Serial);
Serial.println();
json.printTo(configFile);
return true;
}//saveConfig
bool loadConfig(char names[][NAME_BUFFER_SIZE], char values[][VALUE_BUFFER_SIZE], const char path[], uint8_t nrParameters) {
Serial.printf("mounting %s\n", path);
if (SPIFFS.exists(path)) {
File configFile = SPIFFS.open(path, "r");
if (!configFile) {
Serial.println("Failed to open config file");
return false;
}
size_t size = configFile.size();
if (size > 1024) {
Serial.println("Config file size is too large");
return false;
}
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
Serial.println();
if (!json.success()) {
Serial.println("Failed to parse config file");
return false;
}
for (int i = 0; i < nrParameters; i++)
strcpy(values[i], json[names[i]]);
return true;
}
else {
Serial.printf("sorry, but the file %s does not exist..\n", path);
return false;
}
}//loadConfig
/***********************************************/
/* HANDLERS */
/***********************************************/
void handleWiFiArgs() {
Serial.println("handling WiFi");
server.send(200, "text/html", "saving parameter");
for (int i = 0; i < server.args(); i++) {
server.arg(i).toCharArray(wifiValues[i], VALUE_BUFFER_SIZE); //load parameters in the array
}
saveConfig(wifiNames, wifiValues, wifi_path, NR_WIFI_PARAM);
rebootESP();
}//handleWiFiArgs
void handleMqttArgs() {
Serial.println("handling MQTT");
server.send(200, "text/html", "saving parameter");
if (server.args() == NR_MQTT_PARAM) { //MQTT enabled
for (int i = 0; i < server.args(); i++) {
server.arg(i).toCharArray(mqttValues[i], VALUE_BUFFER_SIZE); //load parameters in the array
}
} else memset(mqttValues[0], 0, VALUE_BUFFER_SIZE);
saveConfig(mqttNames, mqttValues, mqtt_path, NR_MQTT_PARAM);
rebootESP();
}//handleMqttArgs
/***********************************************/
/* SERVER */
/***********************************************/
void startServer() {
server.on("/", handleRootPage);
server.on("/wifi.html", handleWiFiPage);
server.on("/availWiFi", availWiFiContent);
server.on("/mqtt.html", handleMqttPage);
server.on("/about.html", handleAboutPage);
server.on("/netParam", netParamAjax);
server.on("/mystyle.css", handleStyles);
server.on("/submitWiFi", handleWiFiArgs);
server.on("/submitMqtt", handleMqttArgs);
server.begin();
Serial.println("Server Started");
}