-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig.cpp
362 lines (319 loc) · 9.5 KB
/
config.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
/*
* IoT Device Firmware for Arduino OPTA with Ethernet and MQTT Support
* -------------------------------------------------------------------
* This firmware is designed to run on the Arduino OPTA platform. It provides
* a robust framework for network connectivity, MQTT communication,
* and web server functionality. The code allows for dynamic configuration
* via a built-in web interface and supports telemetry data publishing to
* an MQTT broker. It includes:
* - Ethernet-based networking.
* - WiFi Netowkring.
* - MQTT client for telemetry and control.
* - JSON-based configuration stored in flash memory.
* - Web server for monitoring and configuration.
* - Scheduler for periodic tasks.
*
* Author: Alberto Perro
* Date: 27-12-2024
* License: CERN-OHL-P
*/
#include "config.h"
#include <ArduinoJson.h> // Include ArduinoJson library
#include <Arduino.h>
namespace remoto
{
config::config()
{
loadDefaults();
}
// Getter for deviceId
String config::getDeviceId() const
{
return _deviceId;
}
// Setter for deviceId
void config::setDeviceId(const String &id)
{
_deviceId = id;
}
// Getter for IP address
String config::getDeviceIpAddress() const
{
return _ipaddr;
}
// Setter for IP address
void config::setDeviceIpAddress(const String &ip)
{
_ipaddr = ip;
}
// Getter for dhcp
bool config::getDHCP() const
{
return _dhcp;
}
// Setter for dhcp
void config::setDHCP(const bool val)
{
_dhcp = val;
}
// Getter for dhcp
bool config::getWiFiPref() const
{
return _preferWifi;
}
// Setter for preferwifi
void config::setWiFiPref(const bool val)
{
_preferWifi = val;
}
// Getter for MQTT server
String config::getMqttServer() const
{
return _mqtt.server;
}
// Setter for MQTT server
void config::setMqttServer(const String &server)
{
_mqtt.server = server;
}
// Getter for MQTT Port
int config::getMqttPort() const
{
return _mqtt.port;
}
// Setter for MQTT Port
void config::setMqttPort(const int port)
{
_mqtt.port = port;
}
// Getter for MQTT user
String config::getMqttUser() const
{
return _mqtt.user;
}
// Setter for MQTT user
void config::setMqttUser(const String &user)
{
_mqtt.user = user;
}
// Getter for MQTT password
String config::getMqttPassword() const
{
return _mqtt.password;
}
// Setter for MQTT password
void config::setMqttPassword(const String &password)
{
_mqtt.password = password;
}
// Getter for MQTT update interval
int config::getMqttUpdateInterval() const
{
return _mqtt.updateInterval;
}
// Setter for MQTT update interval
void config::setMqttUpdateInterval(int interval)
{
_mqtt.updateInterval = interval;
}
// Getter for timeserver address
String config::getTimeServer() const
{
return _timeServer;
}
// Setter for timeserver address
void config::setTimeServer(const String ×erver)
{
_timeServer = timeserver;
}
// Getter for ssid name
String config::getSSID() const
{
return _ssid;
}
// Setter for ssid name
void config::setSSID(const String &ssid)
{
_ssid = ssid;
}
// Getter for ssid password
String config::getWiFiPassword() const
{
return _wifiPass;
}
// Setter for ssid password
void config::setWiFiPassword(const String &password)
{
_wifiPass = password;
}
// Getter for input type (DIGITAL or ANALOG)
int config::getInputType(int index) const
{
if (index >= 0 && index < NUM_INPUTS)
{
return _inputs[index][1]; // 0 for ANALOG, 1 for DIGITAL
}
return -1; // Invalid index
}
// Setter for input type (DIGITAL or ANALOG)
int config::setInputType(int index, int type)
{
if (index >= 0 && index < NUM_INPUTS && (type == DIGITAL || type == ANALOG))
{
_inputs[index][1] = type; // 0 for ANALOG, 1 for DIGITAL
return 0;
}
return -1; // Invalid index
}
int config::getInputPin(int index) const
{
if (index >= 0 && index < NUM_INPUTS)
{
return _inputs[index][0];
}
return -1; // Invalid index
}
int config::getOutputPin(int index) const
{
if (index >= 0 && index < NUM_OUTPUTS)
{
return _outputs[index];
}
return -1; // Invalid index
}
int config::getOutputLed(int index) const
{
if (index >= 0 && index < NUM_OUTPUTS)
{
return _outputsLed[index];
}
return -1; // Invalid index
}
// Initialize input pin modes
void config::initializePins()
{
analogReadResolution(ADC_BITS);
for (int i = 0; i < NUM_INPUTS; ++i)
{
if (_inputs[i][1] == DIGITAL)
{
pinMode(_inputs[i][0], INPUT); // Set pin to digital input
}
}
for (int i = 0; i < NUM_OUTPUTS; ++i)
{
pinMode(_outputs[i], OUTPUT);
pinMode(_outputsLed[i], OUTPUT);
digitalWrite(_outputs[i], LOW);
digitalWrite(_outputsLed[i], LOW);
}
}
// Function to load configuration from a JSON string
// Function to load configuration from a JSON buffer
int config::loadFromJson(const char *buffer, size_t length)
{
DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc, buffer, length);
// Check for deserialization errors
if (error)
{
Serial.println("Failed to parse JSON");
return -1;
}
// Check if all necessary keys are present
if (!doc.containsKey("deviceId") ||
!doc.containsKey("deviceIpAddress") ||
!doc.containsKey("dhcp") ||
!doc.containsKey("preferWifi") ||
!doc.containsKey("ssid") ||
!doc.containsKey("wifiPass") ||
!doc.containsKey("timeServer") ||
!doc["mqtt"].containsKey("server") ||
!doc["mqtt"].containsKey("port") ||
!doc["mqtt"].containsKey("user") ||
!doc["mqtt"].containsKey("password") ||
!doc["mqtt"].containsKey("updateInterval") ||
!doc.containsKey("inputs"))
{
Serial.println("Missing required keys in JSON");
return -1;
}
// Set values from JSON if all keys are valid
_deviceId = doc["deviceId"].as<String>();
_ipaddr = doc["deviceIpAddress"].as<String>();
_dhcp = doc["dhcp"].as<bool>();
_preferWifi = doc["preferWifi"].as<bool>();
_ssid = doc["ssid"].as<String>();
_ssid = doc["wifiPass"].as<String>();
_ssid = doc["timeServer"].as<String>();
_mqtt.server = doc["mqtt"]["server"].as<String>();
_mqtt.port = doc["mqtt"]["port"].as<int>();
_mqtt.user = doc["mqtt"]["user"].as<String>();
_mqtt.password = doc["mqtt"]["password"].as<String>();
_mqtt.updateInterval = doc["mqtt"]["updateInterval"].as<int>();
// Load input pins and types
for (int i = 0; i < NUM_INPUTS; ++i)
{
String pinName = "I" + String(i + 1);
_inputs[i][1] = doc["inputs"][pinName].as<int>();
}
return 0; // Successfully loaded configuration
}
// Function to convert configuration to a JSON string
String config::toJson() const
{
DynamicJsonDocument doc(2048);
doc["deviceId"] = _deviceId;
doc["deviceIpAddress"] = _ipaddr;
doc["dhcp"] = _dhcp;
doc["preferWifi"] = _preferWifi;
doc["ssid"] = _ssid;
doc["wifiPass"] = _wifiPass;
doc["timeServer"] = _timeServer;
doc["mqtt"]["server"] = _mqtt.server;
doc["mqtt"]["port"] = _mqtt.port;
doc["mqtt"]["user"] = _mqtt.user;
doc["mqtt"]["password"] = _mqtt.password;
doc["mqtt"]["updateInterval"] = _mqtt.updateInterval;
for (int i = 0; i < NUM_INPUTS; ++i)
{
String pinName = "I" + String(i + 1);
doc["inputs"][pinName] = _inputs[i][1];
}
String jsonString;
serializeJson(doc, jsonString);
return jsonString;
}
void config::loadDefaults()
{
_deviceId = DEFAULT_DEVICE_ID;
_mqtt.server = DEFAULT_MQTT_BROKER;
_mqtt.port = DEFAULT_MQTT_PORT;
_mqtt.user = DEFAULT_MQTT_USER;
_mqtt.password = DEFAULT_MQTT_PASSWORD;
_mqtt.updateInterval = DEFAULT_TELEMETRY_INTERVAL;
_dhcp = DEFAULT_USE_DHCP;
_preferWifi = DEFAULT_PREFER_WIFI;
_ipaddr = DEFAULT_IP_ADDR;
_ssid = DEFAULT_SSID;
_wifiPass = DEFAULT_SSID_PASS;
_timeServer = DEFAULT_TIME_SERVER;
// Default input pin configurations
_inputs[0][0] = A0;
_inputs[0][1] = DIGITAL;
_inputs[1][0] = A1;
_inputs[1][1] = DIGITAL;
_inputs[2][0] = A2;
_inputs[2][1] = DIGITAL;
_inputs[3][0] = A3;
_inputs[3][1] = DIGITAL;
_inputs[4][0] = A4;
_inputs[4][1] = DIGITAL;
_inputs[5][0] = A5;
_inputs[5][1] = DIGITAL;
_inputs[6][0] = A6;
_inputs[6][1] = ANALOG;
_inputs[7][0] = A7;
_inputs[7][1] = ANALOG;
}
} // namespace remoto