forked from chickey/RS485-WiFi-EPEver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.cpp
More file actions
93 lines (74 loc) · 2.52 KB
/
settings.cpp
File metadata and controls
93 lines (74 loc) · 2.52 KB
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
#include <EEPROM.h>
#include "settings.h"
eeprom_settings myConfig;
uint32_t calculateCRC32(const uint8_t *data, size_t length)
{
//This calculates a CRC32 the same as used in MPEG2 streams
uint32_t crc = 0xffffffff;
while (length--) {
uint8_t c = *data++;
for (uint32_t i = 0x80; i > 0; i >>= 1) {
bool bit = crc & 0x80000000;
if (c & i) {
bit = !bit;
}
crc <<= 1;
if (bit) {
crc ^= 0x04c11db7;
}
}
}
return crc;
}
void WriteConfigToEEPROM() {
uint32_t checksum = calculateCRC32((uint8_t*)&myConfig, sizeof(eeprom_settings));
EEPROM.begin(EEPROM_storageSize);
EEPROM.put(EEPROM_CONFIG_ADDRESS, myConfig);
EEPROM.put(EEPROM_CHECKSUM_ADDRESS, checksum);
EEPROM.end();
}
bool LoadConfigFromEEPROM() {
eeprom_settings restoredConfig;
uint32_t existingChecksum;
EEPROM.begin(EEPROM_storageSize);
EEPROM.get(EEPROM_CONFIG_ADDRESS, restoredConfig);
EEPROM.get(EEPROM_CHECKSUM_ADDRESS, existingChecksum);
EEPROM.end();
// Calculate the checksum of an entire buffer at once.
uint32_t checksum = calculateCRC32((uint8_t*)&restoredConfig, sizeof(eeprom_settings));
Serial.println(checksum, HEX);
Serial.println(existingChecksum, HEX);
if (checksum == existingChecksum) {
//Clone the config into our global variable and return all OK
memcpy(&myConfig, &restoredConfig, sizeof(eeprom_settings));
return true;
}
//Config is not configured or gone bad, return FALSE
Serial.println("Config is not configured or gone bad");
return false;
}
void FactoryResetSettings() {
const char mqtt_server[] = "192.168.0.254";
const char mqtt_username[]="username";
const char mqtt_password[]="password";
const char mqtt_topic[]="topic";
const char influxdb_host[] = "192.168.0.254";
const char influxdb_database[] = "powerwall";
const char influxdb_user[] = "username";
const char influxdb_password[] = "password";
strcpy(myConfig.influxdb_host, influxdb_host );
strcpy(myConfig.influxdb_database, influxdb_database );
strcpy(myConfig.influxdb_user, influxdb_user );
strcpy(myConfig.influxdb_password, influxdb_password );
myConfig.influxdb_enabled=false;
myConfig.influxdb_httpPort=8086;
strcpy(myConfig.mqtt_server, mqtt_server );
strcpy(myConfig.mqtt_username, mqtt_username);
strcpy(myConfig.mqtt_password, mqtt_password);
strcpy(myConfig.mqtt_topic, mqtt_topic);
myConfig.MQTT_Enable=false;
myConfig.mqtt_port = 1883;
myConfig.Device_ID=1;
myConfig.Device_BAUD=115200;
WriteConfigToEEPROM();
}