-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path8_CONFIGFILE.ino
364 lines (312 loc) · 12.5 KB
/
8_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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
bool loadConfig()
{
DEBUG_MSG("%s\n", "------ loadConfig started ------");
File configFile = LittleFS.open(CONFIG, "r");
if (!configFile)
{
DEBUG_MSG("%s\n", "Failed to open config file\n");
DEBUG_MSG("%s\n", "------ loadConfig aborted ------\n");
return false;
}
size_t size = configFile.size();
if (size > 2048)
{
DEBUG_MSG("%s\n", "Config file size is too large");
DEBUG_MSG("%s\n", "------ loadConfig aborted ------");
return false;
}
DynamicJsonDocument doc(2500);
DeserializationError error = deserializeJson(doc, configFile);
if (error)
{
DEBUG_MSG("Conf: Error Json %s\n", error.c_str());
return false;
}
// Misc Settings
JsonArray miscArray = doc["misc"];
JsonObject miscObj = miscArray[0];
wait_on_Sensor_error_actor = miscObj["del_sen_act"] | 120000;
wait_on_Sensor_error_induction = miscObj["del_sen_ind"] | 120000;
DEBUG_MSG("Wait on sensor error actors: %d sec\n", wait_on_Sensor_error_actor / 1000);
DEBUG_MSG("Wait on sensor error induction: %d sec\n", wait_on_Sensor_error_induction / 1000);
StopOnMQTTError = miscObj["enable_mqtt"] | 0;
wait_on_error_mqtt = miscObj["delay_mqtt"] | 120000;
DEBUG_MSG("Switch off actors on MQTT error: %d after %d sec\n", StopOnMQTTError, (wait_on_error_mqtt / 1000));
startBuzzer = miscObj["buzzer"] | 0;
// DEBUG_MSG("Buzzer: %d\n", startBuzzer);
if (startBuzzer)
mqttBuzzer = miscObj["mqbuz"] | 0;
else
mqttBuzzer = false;
DEBUG_MSG("Buzzer: %d mqttBuzzer: %d\n", startBuzzer, mqttBuzzer);
senRes = miscObj["res"] | 0;
useDisplay = miscObj["display"] | 0;
startPage = miscObj["page"] | 0;
devBranch = miscObj["devbranch"] | 0;
DEBUG_MSG("Display: %d startPage: %d\n", useDisplay, startPage);
strlcpy(nameMDNS, miscObj["mdns_name"] | "", maxHostSign);
startMDNS = miscObj["mdns"] | 0;
useI2C = miscObj["i2c"] | 0;
DEBUG_MSG("I2C: %d mDNS: %d name: %s\n", useI2C, startMDNS, nameMDNS);
strlcpy(mqtthost, miscObj["MQTTHOST"] | "", maxHostSign);
strlcpy(mqttuser, miscObj["MQTTUSER"] | "", maxUserSign);
strlcpy(mqttpass, miscObj["MQTTPASS"] | "", maxPassSign);
mqttport = miscObj["MQTTPORT"] | 1883;
DEBUG_MSG("MQTT server IP: %s Port: %d User: %s Pass: %s\n", mqtthost, mqttport, mqttuser, mqttpass);
if (useI2C)
numberOfPins = ALLPINS;
else
numberOfPins = GPIOPINS;
DEBUG_MSG("%s\n", "--------------------");
// read actors
JsonArray actorsArray = doc["actors"];
numberOfActors = actorsArray.size();
if (numberOfActors > numberOfActorsMax)
numberOfActors = numberOfActorsMax;
int i = 0;
for (JsonObject actorObj : actorsArray)
{
if (i < numberOfActors)
{
actors[i].change(actorObj["PIN"] | "", actorObj["SCRIPT"] | "", actorObj["NAME"] | "", actorObj["INV"] | 0, actorObj["SW"] | 0);
DEBUG_MSG("Actor #: %d Name: %s MQTT: %s PIN: %s INV: %d SW: %d\n", (i + 1), actorObj["NAME"].as<const char *>(), actorObj["SCRIPT"].as<const char *>(), actorObj["PIN"].as<const char *>(), actorObj["INV"].as<int>(), actorObj["SW"].as<int>());
i++;
}
}
if (numberOfActors == 0)
{
DEBUG_MSG("Actors: %d\n", numberOfActors);
}
DEBUG_MSG("%s\n", "--------------------");
// read sensors
JsonArray sensorsArray = doc["sensors"];
numberOfSensors = sensorsArray.size();
if (numberOfSensors > numberOfSensorsMax)
numberOfSensors = numberOfSensorsMax;
i = 0;
for (JsonObject sensorsObj : sensorsArray)
{
if (i < numberOfSensors)
{
sensors[i].change(sensorsObj["ADDRESS"] | "", sensorsObj["SCRIPT"] | "", sensorsObj["NAME"] | "", sensorsObj["CBPIID"] | "", sensorsObj["OFFSET1"] | 0.0, sensorsObj["OFFSET2"] | 0.0, sensorsObj["SW"] | 0);
DEBUG_MSG("Sensor #: %d Name: %s Address: %s MQTT: %s CBPi-ID: %s Offset1: %.02f Offset2: %.02f SW: %d\n", (i + 1), sensorsObj["NAME"].as<const char *>(), sensorsObj["ADDRESS"].as<const char *>(), sensorsObj["SCRIPT"].as<const char *>(), sensorsObj["CBPIID"].as<const char *>(), sensorsObj["OFFSET1"].as<float>(), sensorsObj["OFFSET2"].as<float>(), sensorsObj["SW"].as<int>());
i++;
}
else
sensors[i].change("", "", "", "", 0.0, 0.0, false);
}
DEBUG_MSG("%s\n", "--------------------");
// read induction
JsonArray indArray = doc["induction"];
JsonObject indObj = indArray[0];
inductionCooker.isEnabled = indObj["ENABLED"] | 0;
inductionStatus = inductionCooker.isEnabled;
if (inductionStatus)
{
inductionCooker.change(StringToPin(indObj["PINWHITE"]), StringToPin(indObj["PINYELLOW"]), StringToPin(indObj["PINBLUE"]), indObj["TOPIC"] | "", true, indObj["PL"] | 100);
DEBUG_MSG("Induction: %d MQTT: %s Relais (WHITE): %s, Command channel (YELLOW): %s, Backchannel (BLUE): %s, PlOnErr: %d\n", inductionStatus, indObj["TOPIC"].as<const char *>(), indObj["PINWHITE"].as<const char *>(), indObj["PINYELLOW"].as<const char *>(), indObj["PINBLUE"].as<const char *>(), indObj["PL"].as<int>());
}
else
{
inductionStatus = 0;
DEBUG_MSG("Induction: %d\n", inductionStatus);
}
DEBUG_MSG("%s\n", "--------------------");
DEBUG_MSG("%s\n", "------ loadConfig finished ------");
configFile.close();
if (numberOfSensors > 0) // Ticker Sensors
TickerSen.start();
if (numberOfActors > 0) // Ticker Sensors
TickerAct.start();
if (inductionStatus > 0) // Induktion
TickerInd.start();
DEBUG_MSG("Config file size %d\n", size);
size_t len = measureJson(doc);
DEBUG_MSG("JSON config length: %d\n", len);
int memoryUsed = doc.memoryUsage();
DEBUG_MSG("JSON memory usage: %d\n", memoryUsed);
DEBUG_MSG("%s\n", "--------------------");
if (useI2C)
numberOfPins = ALLPINS;
else
numberOfPins = GPIOPINS;
if (startBuzzer)
{
pins_used[PIN_BUZZER] = true;
pinMode(PIN_BUZZER, OUTPUT);
digitalWrite(PIN_BUZZER, LOW);
sendAlarm(ALARM_ON);
}
return true;
}
void saveConfigCallback()
{
shouldSaveConfig = true;
if (LittleFS.begin())
{
// saveConfig();
shouldSaveConfig = true;
}
}
bool saveConfig()
{
DEBUG_MSG("%s\n", "------ saveConfig started ------");
DynamicJsonDocument doc(2500);
// Write Actors
JsonArray actorsArray = doc.createNestedArray("actors");
for (int i = 0; i < numberOfActors; i++)
{
JsonObject actorsObj = actorsArray.createNestedObject();
actorsObj["PIN"] = PinToString(actors[i].pin_actor);
actorsObj["NAME"] = actors[i].name_actor;
actorsObj["SCRIPT"] = actors[i].argument_actor;
actorsObj["INV"] = (int)actors[i].isInverted;
actorsObj["SW"] = (int)actors[i].switchable;
DEBUG_MSG("Actor #: %d Name: %s MQTT: %s PIN: %s INV: %d SW: %d\n", (i + 1), actors[i].name_actor.c_str(), actors[i].argument_actor.c_str(), PinToString(actors[i].pin_actor).c_str(), actors[i].isInverted, actors[i].switchable);
}
if (numberOfActors == 0)
{
DEBUG_MSG("Actors: %d\n", numberOfActors);
}
DEBUG_MSG("%s\n", "--------------------");
// Write Sensors
JsonArray sensorsArray = doc.createNestedArray("sensors");
for (int i = 0; i < numberOfSensors; i++)
{
JsonObject sensorsObj = sensorsArray.createNestedObject();
sensorsObj["ADDRESS"] = sensors[i].getSens_adress_string();
sensorsObj["NAME"] = sensors[i].getName();
sensorsObj["OFFSET1"] = (int(sensors[i].getOffset1() * 100)) / 100.0;
sensorsObj["OFFSET2"] = (int(sensors[i].getOffset2() * 100)) / 100.0;
sensorsObj["SCRIPT"] = sensors[i].getTopic();
sensorsObj["CBPIID"] = sensors[i].getId();
sensorsObj["SW"] = (int)sensors[i].getSw();
DEBUG_MSG("Sensor #: %d Name: %s Address: %s MQTT: %s CBPi-ID: %s Offset1: %f Offset2: %f SW: %d\n", (i + 1), sensors[i].getName().c_str(), sensors[i].getSens_adress_string().c_str(), sensors[i].getTopic().c_str(), sensors[i].getId().c_str(), sensors[i].getOffset1(), sensors[i].getOffset2(), sensors[i].getSw());
}
DEBUG_MSG("%s\n", "--------------------");
// Write Induction
JsonArray indArray = doc.createNestedArray("induction");
if (inductionCooker.isEnabled)
{
inductionStatus = 1;
JsonObject indObj = indArray.createNestedObject();
indObj["PINWHITE"] = PinToString(inductionCooker.PIN_WHITE);
indObj["PINYELLOW"] = PinToString(inductionCooker.PIN_YELLOW);
indObj["PINBLUE"] = PinToString(inductionCooker.PIN_INTERRUPT);
indObj["TOPIC"] = inductionCooker.mqtttopic;
indObj["ENABLED"] = (int)inductionCooker.isEnabled;
indObj["PL"] = inductionCooker.powerLevelOnError;
DEBUG_MSG("Induction: %d MQTT: %s Relais (WHITE): %s, Command channel (YELLOW): %s, Backchannel (BLUE): %s, PlOnErr: %d\n", inductionCooker.isEnabled, inductionCooker.mqtttopic.c_str(), PinToString(inductionCooker.PIN_WHITE).c_str(), PinToString(inductionCooker.PIN_YELLOW).c_str(), PinToString(inductionCooker.PIN_INTERRUPT).c_str(), inductionCooker.powerLevelOnError);
}
else
{
inductionStatus = 0;
DEBUG_MSG("Induction: %d\n", inductionCooker.isEnabled);
}
DEBUG_MSG("%s\n", "--------------------");
// Write Misc Stuff
JsonArray miscArray = doc.createNestedArray("misc");
JsonObject miscObj = miscArray.createNestedObject();
miscObj["del_sen_act"] = wait_on_Sensor_error_actor;
miscObj["del_sen_ind"] = wait_on_Sensor_error_induction;
DEBUG_MSG("Wait on sensor error actors: %d sec\n", wait_on_Sensor_error_actor / 1000);
DEBUG_MSG("Wait on sensor error induction: %d sec\n", wait_on_Sensor_error_induction / 1000);
miscObj["delay_mqtt"] = wait_on_error_mqtt;
miscObj["enable_mqtt"] = (int)StopOnMQTTError;
DEBUG_MSG("Switch off actors on error enabled after %d sec\n", (wait_on_error_mqtt / 1000));
miscObj["buzzer"] = (int)startBuzzer;
if (startBuzzer)
miscObj["mqbuz"] = (int)mqttBuzzer;
else
miscObj["mqbuz"] = 0;
miscObj["res"] = (int)senRes;
miscObj["display"] = (int)useDisplay;
miscObj["page"] = startPage;
miscObj["devbranch"] = (int)devBranch;
DEBUG_MSG("Display: %d startPage: %d\n", useDisplay, startPage);
DEBUG_MSG("DevBranch: %d\n", devBranch);
miscObj["mdns_name"] = nameMDNS;
miscObj["mdns"] = (int)startMDNS;
miscObj["i2c"] = (int)useI2C;
miscObj["MQTTHOST"] = mqtthost;
miscObj["MQTTPORT"] = mqttport;
miscObj["MQTTUSER"] = mqttuser;
miscObj["MQTTPASS"] = mqttpass;
miscObj["VER"] = Version;
DEBUG_MSG("MQTT broker IP: %s Port: %d User: %s Pass: %s\n", mqtthost, mqttport, mqttuser, mqttpass);
DEBUG_MSG("%s\n", "--------------------");
// size_t len = measureJson(doc);
// int memoryUsed = doc.memoryUsage();
if (measureJson(doc) > 2048 || doc.memoryUsage() > 2500)
{
DEBUG_MSG("JSON config length: %d\n", measureJson(doc));
DEBUG_MSG("JSON memory usage: %d\n", doc.memoryUsage());
DEBUG_MSG("%s\n", "Failed to write config file - config too large");
DEBUG_MSG("%s\n", "------ saveConfig aborted ------");
if (startBuzzer)
sendAlarm(ALARM_ERROR);
return false;
}
File configFile = LittleFS.open(CONFIG, "w");
if (!configFile)
{
DEBUG_MSG("%s\n", "Failed to open config file for writing");
DEBUG_MSG("%s\n", "------ saveConfig aborted ------");
if (startBuzzer)
sendAlarm(ALARM_ERROR);
return false;
}
serializeJson(doc, configFile);
configFile.close();
DEBUG_MSG("Config file size %d\n", configFile.size());
DEBUG_MSG("JSON config length: %d\n", measureJson(doc));
DEBUG_MSG("JSON memory usage: %d\n", doc.memoryUsage());
DEBUG_MSG("%s\n", "------ saveConfig finished ------");
if (useI2C)
numberOfPins = ALLPINS;
else
numberOfPins = GPIOPINS;
DEBUG_MSG("Maximum number of pins: %d\n", numberOfPins);
DEBUG_MSG("Free heap memory: %d\n", ESP.getFreeHeap());
if (numberOfSensors > 0) // Ticker Sensors
TickerSen.start();
else
TickerSen.stop();
if (numberOfActors > 0) // Ticker Sensors
TickerAct.start();
else
TickerAct.stop();
if (inductionStatus > 0) // Ticker Induktion
TickerInd.start();
else
TickerInd.stop();
if (!useDisplay) // Ticker Display
TickerDisp.stop();
else
{
if (TickerDisp.state() != RUNNING)
{
if (!softSerial)
softSerial.begin(9600, SWSERIAL_8N1, D1, D2, false);
DEBUG_MSG("SoftwareSerial init %d\n", int(softSerial));
pins_used[D1] = true;
pins_used[D2] = true;
nextion.begin(softSerial);
initDisplay();
TickerDisp.start();
}
}
TickerPUBSUB.start();
String Network = WiFi.SSID();
DEBUG_MSG("ESP8266 device IP Address: %s\n", WiFi.localIP().toString().c_str());
DEBUG_MSG("Configured WLAN SSID: %s\n", Network.c_str());
if (startBuzzer)
{
pins_used[PIN_BUZZER] = true;
pinMode(PIN_BUZZER, OUTPUT);
digitalWrite(PIN_BUZZER, LOW);
sendAlarm(ALARM_ON);
}
DEBUG_MSG("%s\n", "---------------------------------");
return true;
}