-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigFile.ino
77 lines (70 loc) · 2.23 KB
/
ConfigFile.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
#include "config.h"
void saveConfigCallback () {
Serial.println("Should save config");
shouldSaveConfig = true;
}
void ReadConfigFile(){
Serial.println("mounting FS...");
if (SPIFFS.begin()){
Serial.println("mounted file system");
if (SPIFFS.exists("/config.json")){
Serial.println("reading config file");
File configFile = SPIFFS.open("/config.json", "r");
if (configFile){
Serial.println("opened config file");
size_t size = configFile.size();
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success()){
Serial.println("\nparsed json");
strcpy(mqtt_server, json["mqtt_server"]);
String port = json["mqtt_port"];
mqtt_port = port.toInt();
strcpy(mqtt_user, json["mqtt_user"]);
strcpy(mqtt_password, json["mqtt_password"]);
strcpy(mqtt_keywords1, json["mqtt_keywords1"]);
strcpy(mqtt_keywords2, json["mqtt_keywords2"]);
} else {
Serial.println("failed to load json config");
}
}
}
} else {
Serial.println("failed to mount FS");
}
}
String IpAddress2String(const IPAddress& ipAddress){
return String(ipAddress[0]) + String(".") +\
String(ipAddress[1]) + String(".") +\
String(ipAddress[2]) + String(".") +\
String(ipAddress[3]);
}
void SaveConfigFile(){
if (shouldSaveConfig){
Serial.println("saving config");
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["mqtt_server"] = mqtt_server;
json["mqtt_port"] = String(mqtt_port);
json["mqtt_user"] = mqtt_user;
json["mqtt_password"] = mqtt_password;
json["mqtt_keywords1"] = mqtt_keywords1;
json["mqtt_keywords2"] = mqtt_keywords2;
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("failed to open config file for writing");
}
json.printTo(Serial);
json.printTo(configFile);
configFile.close();
}
}
void ResetSettings(){
WiFiManager wifiManager;
wifiManager.resetSettings();
delay(2000);
ESP.restart();
}