-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConfiguration.cpp
459 lines (392 loc) · 13.6 KB
/
Configuration.cpp
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#include "Configuration.h"
#include <avr/wdt.h>
#include <EEPROM.h>
namespace Configuration {
States state = undefinedState;
char PLC_Topic[CONFIG_MAX_LENGTH] = "controllino";
char root_Topic[CONFIG_MAX_LENGTH] = "iot";
char command_Topic[CONFIG_MAX_LENGTH] = "command";
char state_Topic[CONFIG_MAX_LENGTH] = "state";
char log_Topic[CONFIG_MAX_LENGTH]= "log";
char username[CONFIG_MAX_LENGTH]= "mqttuser";
char password[CONFIG_MAX_LENGTH]= "mqttpass";
byte mac[6] = {0x1A, 0x6B, 0xE7, 0x45, 0xB9, 0x26};
byte ip[4] = {192, 168, 1, 20};
byte server[4] = {192, 168, 1, 10};
uint16_t port = 1883;
bool isValid = false;
bool isConfiguring = false;
char signature[CONFIG_SIGNATURE_LENGTH+1] = CONFIG_SIGNATURE;
byte modbus_address = 1;
byte modbus_count = 0;
void save() {
int currentAddress = 0;
saveCharArray(currentAddress, signature);
saveCharArray(currentAddress, PLC_Topic);
saveCharArray(currentAddress, root_Topic);
saveCharArray(currentAddress, command_Topic);
saveCharArray(currentAddress, log_Topic);
saveCharArray(currentAddress, username);
saveCharArray(currentAddress, password);
saveByteArray(currentAddress, mac, 6);
saveByteArray(currentAddress, ip, 4);
saveByteArray(currentAddress, server, 4);
EEPROM.update(currentAddress++, modbus_address);
EEPROM.update(currentAddress++, modbus_count);
EEPROM.update(currentAddress++, port & 0xff);
EEPROM.update(currentAddress++, port >> 8);
}
void saveCharArray(int &offset, char* charArray){
int i = 0;
bool finish = false;
while(!finish) {
EEPROM.update(offset+i,charArray[i]);
if(!charArray[i]) {
finish = true;
}
i++;
}
offset +=i;
}
void saveByteArray(int &offset,byte* byteArray, int length){
int i = 0;
for(i=0;i<length;i++){
EEPROM.update(offset+i,byteArray[i]);
}
offset +=i;
}
void readCharArray(int &offset, char* charArray, int maxLength){
String aux("");
int i = 0;
bool finish = false;
while(!finish) {
char signatureChar = 0;
EEPROM.get(offset+i, signatureChar);
if(!signatureChar || i >= (maxLength-1)) {
aux.toCharArray(charArray,i+1);
finish = true;
} else {
aux += signatureChar;
}
i++;
}
offset +=i;
}
void readByteArray(int &offset,byte* byteArray, int length){
int i = 0;
for(i=0;i<length;i++){
EEPROM.get(offset+i,byteArray[i]);
}
offset +=i;
}
void load() {
Serial.print(F("Loading configuration..."));
int currentAddress = 0;
char aux[CONFIG_SIGNATURE_LENGTH+1];
isValid = true;
// signature
readCharArray(currentAddress, aux, CONFIG_SIGNATURE_LENGTH+1);
if(strcmp(aux,signature)) {
Serial.print(F("signature "));
Serial.print(aux);
Serial.print(F(" invalid!!!"));
isValid = false;
} else {
Serial.print(F("found..."));
readCharArray(currentAddress, PLC_Topic, CONFIG_MAX_LENGTH);
readCharArray(currentAddress, root_Topic, CONFIG_MAX_LENGTH);
readCharArray(currentAddress, command_Topic, CONFIG_MAX_LENGTH);
readCharArray(currentAddress, log_Topic, CONFIG_MAX_LENGTH);
readCharArray(currentAddress, username, CONFIG_MAX_LENGTH);
readCharArray(currentAddress, password, CONFIG_MAX_LENGTH);
readByteArray(currentAddress, mac, 6);
readByteArray(currentAddress, ip, 4);
readByteArray(currentAddress, server, 4);
EEPROM.get(currentAddress++, modbus_address);
EEPROM.get(currentAddress++, modbus_count);
EEPROM.get(currentAddress++, port);
Serial.println("OK");
}
}
void setInitialState() {
isConfiguring = false;
Serial.println(F("To enter configuration mode press C"));
state = initialState;
}
void initial(String readString) {
if(readString.equals(String("C"))) {
setMenuState();
} else {
setInitialState();
}
}
void setMenuState() {
isConfiguring = true;
Serial.println(F("*** MAIN CONFIGURATION MENU ***"));
Serial.println();
Serial.print(F("1: Enter MAC => ("));
char formattedMAC[17];
sprintf(formattedMAC, "%x:%x:%x:%x:%x:%x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Serial.print(formattedMAC); Serial.println(")");
Serial.print(F("2: Enter IP => ("));
char formattedIP[16];
sprintf(formattedIP, "%03d.%03d.%03d.%03d", ip[0], ip[1], ip[2], ip[3]);
Serial.print(formattedIP); Serial.println(")");
Serial.print(F("3: MQTT Server IP => ("));
sprintf(formattedIP, "%03d.%03d.%03d.%03d", server[0], server[1], server[2], server[3]);
Serial.print(formattedIP); Serial.println(F(")"));
Serial.print(F("4: MQTT Port => ("));
Serial.print(port);
Serial.println(F(")"));
Serial.print(F("5: Root Topic => ("));
Serial.print(root_Topic);
Serial.println(F(")"));
Serial.print(F("6: PLC Topic => ("));
Serial.print(PLC_Topic);
Serial.println(F(")"));
Serial.print(F("7: Command Topic => ("));
Serial.print(command_Topic);
Serial.println(F(")"));
Serial.print(F("8: State Topic => ("));
Serial.print(state_Topic);
Serial.println(F(")"));
Serial.print(F("9: Log Topic => ("));
Serial.print(log_Topic);
Serial.println(F(")"));
Serial.print(F("10: Modbus address => ("));
Serial.print(modbus_address);
Serial.println(F(")"));
Serial.print(F("11: Modbus unit count => ("));
Serial.print(modbus_count);
Serial.println(F(")"));
Serial.print(F("12: MQTT username => ("));
Serial.print(username);
Serial.println(F(")"));
Serial.print(F("13: MQTT password => ("));
Serial.print(password);
Serial.println(F(")"));
Serial.println();
Serial.println(F("X: Exit"));
Serial.println();
Serial.println(F("R: Reboot"));
Serial.println();
Serial.println (F("Enter option: "));
state = menuState;
}
void menu(String readString){
if(readString == "X") {
setInitialState();
} else if(readString == "1") {
setMACState();
} else if(readString == "2") {
setIPState();
} else if(readString == "3") {
setServerState();
} else if(readString == "4") {
setPortState();
} else if(readString == "5") {
setRootTopicState();
} else if(readString == "6") {
setPLCTopicState();
} else if(readString == "7") {
setCommandTopicState();
} else if(readString == "8") {
setStateTopicState();
} else if(readString == "9") {
setLogTopicState();
} else if(readString == "10") {
setModbusAddressState();
} else if(readString == "11") {
setModbusCountState();
} else if(readString == "12") {
setUsernameState();
} else if(readString == "13") {
setPasswordState();
} else if(readString == "R") {
save();
Serial.println(F("Saved"));
wdt_enable(WDTO_60MS);
while(1) {}
} else {
}
}
void setPLCTopicState() {
Serial.print (F("Enter PLC Topic: "));
state = PLCTopicState;
}
void PLCTopic(String readString){
readString.toCharArray(PLC_Topic,readString.length()+1);
setMenuState();
}
void setRootTopicState() {
Serial.print (F("Enter root topic: "));
state = rootTopicState;
}
void rootTopic(String readedString){
readedString.toCharArray(root_Topic,readedString.length()+1);
setMenuState();
}
void setCommandTopicState() {
Serial.print (F("Enter command topic: "));
state = commandTopicState;
}
void commandTopic(String readString){
readString.toCharArray(command_Topic,readString.length()+1);
setMenuState();
}
void setStateTopicState() {
Serial.print (F("Enter state topic: "));
state = stateTopicState;
}
void stateTopic(String readedString){
readedString.toCharArray(state_Topic,readedString.length()+1);
setMenuState();
}
void setLogTopicState() {
Serial.print (F("Enter log topic: "));
state = logTopicState;
}
void logTopic(String readString){
readString.toCharArray(log_Topic,readString.length()+1);
setMenuState();
}
void setModbusAddressState() {
Serial.print (F("Enter starting modbus address: "));
state = modbusAddressState;
}
void modbusAddress(String readString){
modbus_address = readString.toInt();
setMenuState();
}
void setModbusCountState() {
Serial.print (F("Enter modbus unit count: "));
state = modbusCountState;
}
void modbusCount(String readString){
modbus_count = readString.toInt();
setMenuState();
}
void setUsernameState() {
Serial.print (F("Enter mqtt username (empty for no auth): "));
state = usernameState;
}
void user(String readString){
readString.toCharArray(username,readString.length()+1);
setMenuState();
}
void setPasswordState() {
Serial.print (F("Enter mqtt password: "));
state = passwordState;
}
void pass(String readString){
readString.toCharArray(password,readString.length()+1);
setMenuState();
}
void setIPState() {
Serial.print (F("Enter IP: (nnn.nnn.nnn.nnn)\n(or 000.000.000.000 for DHCP)"));
state = IPState;
}
void IP(String readString) {
int i;
for(i=0;i<4;i++){
ip[i] = readString.substring(i*4,i*4+3).toInt();
}
setMenuState();
}
void setServerState() {
Serial.print (F("Enter server IP: (nnn.nnn.nnn.nnn)"));
state = serverState;
}
void server_State(String readString) {
int i;
for(i=0;i<4;i++){
server[i] = readString.substring(i*4,i*4+3).toInt();
}
setMenuState();
}
void setPortState() {
Serial.print (F("Enter server port: "));
state = portState;
}
void port_State(String readString){
port = readString.toInt();
setMenuState();
}
void setMACState() {
Serial.print (F("Enter MAC: (HH:HH:HH:HH:HH:HH)"));
state = MACState;
}
void MAC(String readString) {
char aux[3];
int i;
for(i=0;i<6;i++) {
readString.substring(i*3,i*3+2).toCharArray(aux,3);
mac[i] = strtol(aux, 0, 16);
}
setMenuState();
}
void loop(){
static String readString("");
if (Serial.available() > 0) {
char read = Serial.read();
Serial.print(read);
if(read == 13){
Serial.println();
switch (state) {
case menuState:
menu(readString);
break;
case initialState:
initial(readString);
break;
case PLCTopicState:
PLCTopic(readString);
break;
case MACState:
MAC(readString);
break;
case IPState:
IP(readString);
break;
case serverState:
server_State(readString);
break;
case portState:
port_State(readString);
break;
case rootTopicState:
rootTopic(readString);
break;
case commandTopicState:
commandTopic(readString);
break;
case stateTopicState:
stateTopic(readString);
break;
case logTopicState:
logTopic(readString);
break;
case modbusAddressState:
modbusAddress(readString);
break;
case modbusCountState:
modbusCount(readString);
break;
case usernameState:
user(readString);
break;
case passwordState:
pass(readString);
break;
default:
setInitialState();
break;
}
readString="";
Serial.print(">");
} else {
readString += read;
}
}
}
}