-
Notifications
You must be signed in to change notification settings - Fork 0
/
saveConfig.ino
149 lines (129 loc) · 3.64 KB
/
saveConfig.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
139
140
141
142
143
144
145
146
147
148
149
boolean saveDeviceMode() {
char fileName[] = "/deviceMode.json";
SPIFFS.begin();
StaticJsonBuffer<200> jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["deviceMode"] = deviceMode;
File jsonFile = SPIFFS.open(fileName, "w");
if (!jsonFile) {
Serial.print(F("Failed to open "));
Serial.print(fileName);
Serial.println(F(" for writing."));
SPIFFS.end();
return false;
}
json.printTo(jsonFile);
Serial.print(F("Wrote file:"));
Serial.println(fileName);
SPIFFS.end();
return true;
} // end deviceMode()
boolean saveStationConfig() {
char fileName[] = "/station.json";
SPIFFS.begin();
StaticJsonBuffer<200> jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["stationSSID"] = stationSSID;
json["stationPass"] = stationPass;
File jsonFile = SPIFFS.open(fileName, "w");
if (!jsonFile) {
Serial.print(F("Failed to open "));
Serial.print(fileName);
Serial.println(F(" for writing."));
SPIFFS.end();
return false;
}
json.printTo(jsonFile);
Serial.print(F("Wrote file:"));
Serial.println(fileName);
SPIFFS.end();
return true;
} // end saveStationConfig()
boolean saveAPConfig() {
char fileName[] = "/ap.json";
SPIFFS.begin();
StaticJsonBuffer<200> jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["apPass"] = apPass;
json["deviceName"] = deviceName;
File jsonFile = SPIFFS.open(fileName, "w");
if (!jsonFile) {
Serial.print(F("Failed to open "));
Serial.print(fileName);
Serial.println(F(" for writing."));
SPIFFS.end();
return false;
}
json.printTo(jsonFile);
Serial.print(F("Wrote file: "));
Serial.println(fileName);
SPIFFS.end();
if (deviceMode == 0) {
deviceMode = 2;
saveDeviceMode();
}
return true;
} // end saveAPConfig()
boolean saveMQTTConfig() {
char fileName[] = "/mqtt.json";
char temp[6] = "false";
SPIFFS.begin();
StaticJsonBuffer<200> jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
if (useMQTT)
strcpy(temp, "true");
json["useMQTT"] = temp;
json["mqttBroker"] = mqttBroker;
json["mqttPass"] = mqttPass;
json["mqttUser"] = mqttUser;
json["inTopic"] = inTopic;
json["outTopic"] = outTopic;
json["mqttPort"] = mqttPort;
json["mqttTimeout"] = mqttTimeout;
json["onFail"] = onFail;
File jsonFile = SPIFFS.open(fileName, "w");
if (!jsonFile) {
Serial.print(F("Failed to open "));
Serial.print(fileName);
Serial.println(F(" for writing."));
SPIFFS.end();
return false;
}
json.printTo(jsonFile);
Serial.print(F("Wrote file: "));
Serial.println(fileName);
SPIFFS.end();
return true;
} // end saveMQTTConfig()
void wipeSpiffs() {
Serial.println(F("Formatting SPIFFS."));
SPIFFS.format();
Serial.println(F("Format complete."));
} // end wipeSpiffs()
void switchMode(byte choice) {
boolean ok = true;
if (strlen(stationSSID) == 0)
ok = false;
if (choice == 2 && strlen(apPass) == 0)
ok = false;
if (deviceMode == choice)
ok = false;
if (!ok) {
Serial.println("Mode change not permitted.");
page("Permission denied", "Permission denied.\n<p>\n", 0);
sendMQTT("Mode change not permitted.");
return;
}
if (choice == 1)
page("Changing to station mode.", "Changing to station mode.\n<p>\n", 10);
if (choice == 2)
page("Changing to AP mode.", "Changing to AP mode.\n<p>\n", 10);
if (choice == 9)
page("Changing to temporary AP temporary mode.", "Changing to temporary AP mode.\n<p>\n", 10);
deviceMode = choice;
saveDeviceMode();
Serial.print("Booting in mode: ");
Serial.println(deviceMode);
handleDelay(500);
ESP.restart();
} // end switchMode()