Domotique/WifiControlSensor/EEPROM.ino
2016-03-30 00:49:57 +02:00

74 lines
1.9 KiB
C++

/* EEPROM LAYOUT
"BOOTMODE;SSID;PASSWORD;HOSTNAME;MQTT_SERVER;MQTT_USERNAME;MQTT_PASSWD;MQTT_PORT;"
BOOTMODE could be 0 for Setup, 1 for normal use, 2 for OTA
Setup mode is trigger by setting GPIO3 to ground or at first boot
*/
int EepromSaveConfig(uint8_t bootMode, String ssid, String password, String host, String mqttServer, String mqttUser, String mqttPasswd, int mqttPort) {
String eeprom;
eeprom = String(bootMode) + ";" + ssid + ";" + password + ";" + host + ";" + mqttServer + ";" + mqttUser + ";" + mqttPasswd + ";" + String(mqttPort) + ";";
if (eeprom.length() > CONFIG_EEPROM_SIZE )
return -EMSGSIZE;
SKETCH_DEBUG_PRINTLN("Saving " + eeprom);
for (int 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) {
EEPROM.write(0, String(bootMode).charAt(0));
EEPROM.commit();
delay(100);
return 0;
}
void readConfElement(char** element, int &i) {
*element = &eeprom[i];
do {
eeprom[i] = EEPROM.read(i);
i++;
} while (i < CONFIG_EEPROM_SIZE && eeprom[i - 1] != ';');
eeprom[i - 1] = '\0';
if (i >= CONFIG_EEPROM_SIZE)
**element = '\0';
}
void EepromReadConfig(uint8_t &bootMode, char **ssid, char **password, char **host, char **mqttServer, char **mqttUser, char **mqttPasswd, int &mqttPort) {
int i = 2;
uint8_t boot = EEPROM.read(0);
char *mqttPortString = "";
if (boot == '1') {
bootMode = BOOTMODE_NORMAL;
} else if (boot == '2') {
bootMode = BOOTMODE_OTA;
} else {
//Do not need to parse EEPROM when not configured
bootMode = BOOTMODE_SETUP;
return;
}
readConfElement(ssid, i);
readConfElement(password, i);
readConfElement(host, i);
readConfElement(mqttServer, i);
readConfElement(mqttUser, i);
readConfElement(mqttPasswd, i);
readConfElement(&mqttPortString, i);
mqttPort = atoi(mqttPortString);
}