Domotique/WifiControlSensor/EEPROM.ino

148 lines
4.0 KiB
Arduino
Raw Normal View History

/* EEPROM LAYOUT
"BOOTMODE;SSID;PASSWORD;HOSTNAME;MQTT_SERVER;MQTT_USERNAME;MQTT_PASSWD;MQTT_PORT;IP_CONFIG;IP;GATEWAY;NETMASK;DNS1;DNS2;WIFI_CHANNEL;WIFI_BSSID;"
BOOTMODE could be 0 for Setup, 1 for normal use, 2 for OTA
2016-04-06 19:34:29 +02:00
IP_CONFIG could be 0 for DHCP, 1 for static
Setup mode is trigger by setting GPIO3 to ground or at first boot
*/
2016-06-01 15:21:32 +02:00
#include "EEPROM.h"
2020-07-28 19:37:19 +02:00
#define BME680_BSEC_EEPROM_ORIG (CONFIG_EEPROM_SIZE)
2020-07-28 19:45:23 +02:00
#if (CONFIG_EEPROM_SIZE + BSEC_MAX_STATE_BLOB_SIZE) >= SPI_FLASH_SEC_SIZE
#error "CONFIG_EEPROM_SIZE too big"
#endif
2016-06-01 15:21:32 +02:00
char eeprom[CONFIG_EEPROM_SIZE];
2016-11-20 00:06:31 +01:00
int EepromSaveConfig(productConfig &config) {
String eeprom;
2016-11-20 00:06:31 +01:00
eeprom = String(config.bootMode) + ";" + config.ssid + ";" + config.password + ";"
+ config.host + ";" + config.mqttServer + ";"
+ config.mqttUser + ";" + config.mqttPasswd + ";"
+ String(config.mqttPort) + ";"
+ String(config.ip_mode) + ";"
+ config.ip + ";" + config.gw + ";" + config.mask + ";"
+ config.dns + ";" + config.dns2 + ";" + config.channel + ";"
+ config.bssid + ";";
2016-03-30 00:49:57 +02:00
if (eeprom.length() > CONFIG_EEPROM_SIZE )
return -EMSGSIZE;
SKETCH_DEBUG_PRINTLN("Saving " + eeprom);
2016-04-07 16:14:19 +02:00
for (uint i = 0; i < eeprom.length() && i < CONFIG_EEPROM_SIZE; i++) {
EEPROM.write(i, eeprom.charAt(i));
}
EEPROM.commit();
delay(100);
return 0;
}
int EepromSaveBootMode(uint8_t bootMode) {
2016-03-14 01:47:43 +01:00
EEPROM.write(0, String(bootMode).charAt(0));
EEPROM.commit();
delay(100);
return 0;
}
2016-03-14 01:47:43 +01:00
void readConfElement(char** element, int &i) {
*element = &eeprom[i];
do {
eeprom[i] = EEPROM.read(i);
i++;
2016-03-30 00:49:57 +02:00
} while (i < CONFIG_EEPROM_SIZE && eeprom[i - 1] != ';');
2016-03-14 01:47:43 +01:00
eeprom[i - 1] = '\0';
2016-06-01 15:21:32 +02:00
if (i >= CONFIG_EEPROM_SIZE){
SKETCH_DEBUG_PRINTLN("Looks like there is a configuration issue (too long)");
2016-03-14 01:47:43 +01:00
**element = '\0';
2016-06-01 15:21:32 +02:00
}
2016-03-14 01:47:43 +01:00
}
2016-11-20 00:06:31 +01:00
void EepromReadConfig(productConfig &config) {
int i = 2;
2016-03-14 17:18:36 +01:00
uint8_t boot = EEPROM.read(0);
2016-04-07 16:14:19 +02:00
char *tmpString;
if (boot == '1') {
2016-12-14 22:57:01 +01:00
config.bootMode = BOOTMODE_SETUP;
} else if (boot == '2') {
2016-12-14 22:57:01 +01:00
config.bootMode = BOOTMODE_NORMAL;
} else if (boot == '3') {
2016-11-20 00:06:31 +01:00
config.bootMode = BOOTMODE_OTA;
} else {
//Do not need to parse EEPROM when not configured
2016-11-20 00:06:31 +01:00
config.bootMode = BOOTMODE_SETUP;
return;
}
2016-11-20 00:06:31 +01:00
readConfElement(&config.ssid, i);
readConfElement(&config.password, i);
readConfElement(&config.host, i);
readConfElement(&config.mqttServer, i);
readConfElement(&config.mqttUser, i);
readConfElement(&config.mqttPasswd, i);
2016-04-06 19:34:29 +02:00
readConfElement(&tmpString, i);
2016-11-20 00:06:31 +01:00
config.mqttPort = atoi(tmpString);
2016-04-06 19:34:29 +02:00
readConfElement(&tmpString, i);
2016-11-20 00:06:31 +01:00
config.ip_mode = atoi(tmpString);
2016-04-07 02:02:51 +02:00
readConfElement(&tmpString, i);
2019-06-03 00:05:21 +02:00
config.ip = atoll(tmpString);
2016-04-07 02:02:51 +02:00
readConfElement(&tmpString, i);
2019-06-03 00:05:21 +02:00
config.gw = atoll(tmpString);
2016-04-07 02:02:51 +02:00
readConfElement(&tmpString, i);
2019-06-03 00:05:21 +02:00
config.mask = atoll(tmpString);
2016-04-07 02:02:51 +02:00
readConfElement(&tmpString, i);
2019-06-03 00:05:21 +02:00
config.dns = atoll(tmpString);
2016-04-07 02:02:51 +02:00
readConfElement(&tmpString, i);
2019-06-03 00:05:21 +02:00
config.dns2 = atoll(tmpString);
readConfElement(&tmpString, i);
config.channel = atoi(tmpString);
readConfElement(&config.bssid, i);
2016-04-07 02:02:51 +02:00
}
2020-07-28 19:37:19 +02:00
int EepromSaveBME680State(uint8_t *bsecState)
{
for (uint16_t i = BME680_BSEC_EEPROM_ORIG;
i < BME680_BSEC_EEPROM_ORIG + BSEC_MAX_STATE_BLOB_SIZE; i++) {
EEPROM.write(i + 1, bsecState[i]);
}
EEPROM.write(BME680_BSEC_EEPROM_ORIG, BSEC_MAX_STATE_BLOB_SIZE);
EEPROM.commit();
return 0;
}
int EepromLoadBME680State(uint8_t *bsecState)
{
if (EEPROM.read(BME680_BSEC_EEPROM_ORIG) == BSEC_MAX_STATE_BLOB_SIZE) {
// Existing state in EEPROM
SKETCH_DEBUG_PRINTLN("Reading BME680 state from EEPROM");
for (uint16_t i = BME680_BSEC_EEPROM_ORIG;
i < BME680_BSEC_EEPROM_ORIG + BSEC_MAX_STATE_BLOB_SIZE; i++) {
bsecState[i] = EEPROM.read(i + 1);
}
return 0;
} else {
// Erase the EEPROM with zeroes
SKETCH_DEBUG_PRINTLN("Erasing EEPROM for BME680 state");
for (uint16_t i = BME680_BSEC_EEPROM_ORIG;
i < BME680_BSEC_EEPROM_ORIG + BSEC_MAX_STATE_BLOB_SIZE + 1; i++)
EEPROM.write(i, 0);
EEPROM.commit();
return -1;
}
}