-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.ino
138 lines (114 loc) · 3.84 KB
/
Config.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
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
#include <ArduinoJson.h>
#include <FS.h>
#include <LittleFS.h>
#define FORMAT_LITTLEFS_IF_FAILED
#define RELAYS 7
#define CALIBRATION 0.0157521
// Our configuration structure.
//
// Never use a JsonDocument to store the configuration!
// A JsonDocument is *not* a permanent storage; it's only a temporary storage
// used during the serialization phase. See:
// https://arduinojson.org/v6/faq/why-must-i-create-a-separate-config-object/
const char *confFile = "/config.txt";
// Loads the configuration from a file
void loadConfiguration(const char *filename, Config &config) {
// Open file for reading
File file = LittleFS.open(filename,"r");
// Allocate a temporary JsonDocument
// Don't forget to change the capacity to match your requirements.
// Use arduinojson.org/v6/assistant to compute the capacity.
StaticJsonDocument<1024> doc;
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, file);
if (error)
Serial.println(F("Failed to read file, using default configuration"));
// Copy values from the JsonDocument to the Config
config.calConst = doc["calConst"] | CALIBRATION;
config.mqttBroker = doc["mqttBroker"] | "";
config.mqttPort = doc["mqttPort"] | 1883 ;
config.mqttUser = doc["mqttUser"] | "";
config.mqttPasswd = doc["mqttPasswd"] | "";
char tmpstr[50];
sprintf(tmpstr,"%s/data",sapString);
config.mqttTopic = doc["mqttTopic"] | tmpstr;
for(int i = 0; i < RELAYS;i++){
char tmpstr[30];
sprintf(tmpstr,"Label %d",i+1);
config.labelTXT[i] = doc["labelTXT"][i] | tmpstr;
}
// Close the file (Curiously, File's destructor doesn't close the file)
file.close();
}
// Saves the configuration to a file
void saveConfiguration(const char *confFile, const Config &config) {
// Delete existing file, otherwise the configuration is appended to the file
LittleFS.remove(confFile);
// Open file for writing
File file = LittleFS.open(confFile, "w");
if (!file) {
Serial.println(F("Failed to create file"));
return;
}
// Allocate a temporary JsonDocument
// Don't forget to change the capacity to match your requirements.
// Use arduinojson.org/assistant to compute the capacity.
StaticJsonDocument<512> doc;
// Set the values in the document
doc["mqttBroker"] = config.mqttBroker;
doc["mqttPort"] = config.mqttPort;
doc["mqttUser"] = config.mqttUser;
doc["mqttPasswd"] = config.mqttPasswd;
doc["mqttTopic"] = config.mqttTopic;
doc["calConst"] = config.calConst;
for(int i = 0; i < RELAYS;i++){
doc["labelTXT"][i] = config.labelTXT[i];
}
// Serialize JSON to file
if (serializeJson(doc, file) == 0) {
Serial.println(F("Failed to write to file"));
}
// Close the file
file.close();
}
// Prints the content of a file to the Serial
void printFile(const char *confFile) {
// Open file for reading
File file = LittleFS.open(confFile,"r");
if (!file) {
Serial.println(F("Failed to read file"));
return;
}
// Extract each characters by one by one
while (file.available()) {
Serial.print((char)file.read());
}
Serial.println();
// Close the file
file.close();
}
void configSetup() {
// Initialize serial port
Serial.begin(115200);
while (!Serial) continue;
if (!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED)) {
Serial.println(F("LittleFS mount failed"));
return;
}
// Should load default config if run for the first time
Serial.println(F("Loading configuration..."));
loadConfiguration(confFile, config);
// Create configuration file
Serial.println(F("Saving configuration..."));
saveConfiguration(confFile, config);
// Dump config file
Serial.println(F("Print config file..."));
printFile(confFile);
}
void rmfiles(){
if (LittleFS.remove("/config.txt")) {
Serial.println("/config.txt removed");
} else {
Serial.println("/config.txt removal failed");
}
}