-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEEPROM_utils.cpp
103 lines (94 loc) · 3.2 KB
/
EEPROM_utils.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
/*
Vladimir Kirievskiy (C) 2019
********************************************************************************************
* @brief MQTT client software for esp8266 platform (tested on Wemos D1 mini)
* @license MIT
* SDK: Arduino IDE 1.8.5 with plugin for esp8266
*
********************************************************************************************
* @author V. Kirievskiy aka vlakir
* vladimir@kirievskiy.ru
* https://github.com/vlakir
* This software is furnished "as is", without technical support, and with no
* warranty, express or implied, as to its usefulness for any purpose.
*/
#include "EEPROM_utils.h"
/*
* @brief
* Restore xGlobalSettings from defines in settings.h
*
*/
void vRestoreDefaultSettings(void) {
strcpy(xGlobalSettings.acDeviceID, DEFAULT_DEVICE_UNIQ_ID);
strcpy(xGlobalSettings.acWiFiSSID, DEFAULT_WIFI_SSID);
strcpy(xGlobalSettings.acWiFiPassword, DEFAULT_WIFI_PASSWORD);
strcpy(xGlobalSettings.acMQTTserver, DEFAULT_MQTT_SERVER);
strcpy(xGlobalSettings.acMQTTclientID, DEFAULT_MQTT_CLIENT_ID);
strcpy(xGlobalSettings.acMQTTclientPassword, DEFAULT_MQTT_CLIENT_PASSWORD);
xGlobalSettings.uiMQTTport = DEFAULT_MQTT_PORT;
xGlobalSettings.ulCheckSum = DEFAULT_CRC;
}
/*
* @brief
* Convert JSON string (see: /doc/settings_example.txt) to xGlobalSettings.
*
* @param acInput - JSON string
* @return 0 if conversion ok, -1 in case of some error
*/
int iGetSettingsFromJson(char* acInput) {
const int capacity = JSON_OBJECT_SIZE(7);
StaticJsonBuffer<capacity> xJsonBuffer;
JsonObject& xJsonObject = xJsonBuffer.parseObject(acInput);
if (xJsonObject.success()) {
strcpy(xGlobalSettings.acDeviceID, xJsonObject["device_id"]);
strcpy(xGlobalSettings.acWiFiSSID, xJsonObject["wifi_ssid"]);
strcpy(xGlobalSettings.acWiFiPassword, xJsonObject["wifi_password"]);
strcpy(xGlobalSettings.acMQTTserver, xJsonObject["mqtt_server"]);
strcpy(xGlobalSettings.acMQTTclientID, xJsonObject["mqtt_client_id"]);
strcpy(xGlobalSettings.acMQTTclientPassword, xJsonObject["mqtt_client_password"]);
xGlobalSettings.uiMQTTport = atoi(xJsonObject["mqtt_port"]);
xGlobalSettings.ulCheckSum = DEFAULT_CRC;
return 0;
}
else { //JSON parse fault
return -1;
}
}
/*
* @brief
* Save xGlobalSettings to EEPROM
*
*/
void vSaveCurrentSettingsToEEPROM(void) {
EEPROM.begin(sizeof(SettingsStruct));
EEPROM.put(0, xGlobalSettings);
boolean ok1 = EEPROM.commitReset();
Serial.println((ok1) ? "Commit EEPROM OK" : "Commit EEPROM failed");
}
/*
* @brief
* Restore xGlobalSettings from EEPROM
*
*/
void vGetSettingsFromEEPROM(void) {
EEPROM.begin(sizeof(SettingsStruct));
EEPROM.get(0, xGlobalSettings);
}
/*
* @brief
* Restore xGlobalSettings from EEPROM if they are stored there or from defines in settings.h
*
*/
void vGetGlobalSettings(void) {
Serial.println("\n\n\nTry to get settings from EEPROM...");
vGetSettingsFromEEPROM();
if (xGlobalSettings.ulCheckSum != DEFAULT_CRC) { //no settings in EEPROM
Serial.println("No saved settings, use default.");
vRestoreDefaultSettings();
Serial.println(xGlobalSettings.ulCheckSum);
vSaveCurrentSettingsToEEPROM();
}
else {
Serial.println("Success!");
}
}