-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigFunctions.ino
executable file
·98 lines (83 loc) · 2.65 KB
/
ConfigFunctions.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
bool loadConfig() {
File configFile = LittleFS.open("/config.json", "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;
}
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
// We don't use String here because ArduinoJson library requires the input
// buffer to be mutable. If you don't use ArduinoJson, you may as well
// use configFile.readString instead.
configFile.readBytes(buf.get(), size);
StaticJsonDocument<400> json;
DeserializationError err = deserializeJson(json, buf.get());
if (err) {
Serial.print(F("Failed to parse config file: "));
Serial.println(err.c_str());
return false;
}
String ssidC = json["ssid"];
String passC = json["pass"];
String mqtt_ipC = json["mqttServer"];
String mqtt_portC = json["mqttPort"];
String mqtt_authC = json["mqttAuth"];
String mqtt_passC = json["mqttPass"];
String mqtt_TempC = json["mqttTemp"];
String mqtt_HumC = json["mqttHum"];
String mqtt_PressC= json["mqttPress"];
String mqtt_CO2C = json["mqttCO2"];
otaFlag = int(json["otaFlag"]);
TIMEZONE = int(json["TIMEZONE"]);
ssid = ssidC;
pass = passC;
mqtt_ip = mqtt_ipC;
mqtt_port = mqtt_portC;
mqtt_auth = mqtt_authC;
mqtt_pass = mqtt_passC;
mqtt_Temp = mqtt_TempC;
mqtt_Hum = mqtt_HumC;
mqtt_Press= mqtt_PressC;
mqtt_CO2 = mqtt_CO2C;
TempCorrection = int(json["TempCorrection"]);
HumCorrection = int(json["HumCorrection"]);
return true;
}
bool saveConfig() {
StaticJsonDocument<400> json;
json["otaFlag"] = otaFlag;
json["TIMEZONE"] = TIMEZONE;
json["ssid"] = ssid;
json["pass"] = pass;
json["mqttServer"] = mqtt_ip;
json["mqttPort"] = mqtt_port;
json["mqttAuth"] = mqtt_auth;
json["mqttPass"] = mqtt_pass;
json["mqttTemp"] = mqtt_Temp;
json["mqttHum"] = mqtt_Hum;
json["mqttPress"] = mqtt_Press;
json["mqttCO2"] = mqtt_CO2;
json["TempCorrection"] = TempCorrection;
json["HumCorrection"] = HumCorrection;
File configFile = LittleFS.open("/config.json", "w");
if (!configFile) {
Serial.println("Failed to open config file for writing");
return false;
}
serializeJson(json, configFile);
return true;
}
void setOtaFlag(int intOta){
otaFlag=intOta;
saveConfig();
yield();
}
bool clearConfig(){
Serial.println("DEBUG: In config clear!");
return LittleFS.format();
}