-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspiffsUtils.cpp
More file actions
59 lines (43 loc) · 1.74 KB
/
spiffsUtils.cpp
File metadata and controls
59 lines (43 loc) · 1.74 KB
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
#include "IotKernel.h"
void IotKernel::spiffs_setup() {
if (!LittleFS.begin()) {
Serial.println("[SPIFFS] Failed to mount file system");
}
else {
Serial.println("[SPIFFS] File system mounted");
}
}
void IotKernel::get_config_from_spiffs(){
Serial.println("[SPIFFS] Loading config from config.json...");
JsonDocument doc;
File configFile = LittleFS.open("/config.json", "r");
if (!configFile) {
Serial.println("[SPIFFS] Failed to open config file");
return;
}
size_t size = configFile.size();
if (size > 1024) {
Serial.println("[SPIFFS] Config file size is too large");
return;
}
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
auto error = deserializeJson(doc, buf.get());
if (error) {
Serial.println("[SPIFFS] Failed to parse config file");
return;
}
// load file content into custom config structure
this->config.nickname = doc["nickname"].as<String>();
this->config.hostname = doc["hostname"].as<String>();
this->config.wifi.ssid = doc["wifi"]["ssid"].as<String>();
this->config.wifi.password = doc["wifi"]["password"].as<String>();
this->config.mqtt.broker.host = doc["mqtt"]["broker"]["host"].as<String>();
this->config.mqtt.broker.port = doc["mqtt"]["broker"]["port"].as<int>();
this->config.mqtt.broker.secure = doc["mqtt"]["broker"]["secure"].as<String>();
this->config.mqtt.username = doc["mqtt"]["username"].as<String>();
this->config.mqtt.password = doc["mqtt"]["password"].as<String>();
// Warning, unset parameters become the string "null"
Serial.println("[SPIFFS] Successfully loaded config from config.json");
}