2016-03-11 00:43:24 +01:00
|
|
|
/* EEPROM LAYOUT
|
2016-03-14 17:18:36 +01:00
|
|
|
"BOOTMODE;SSID;PASSWORD;HOSTNAME;MQTT_SERVER;MQTT_USERNAME;MQTT_PASSWD;MQTT_PORT;"
|
2016-03-11 00:43:24 +01:00
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-03-24 14:54:55 +01:00
|
|
|
int EepromSaveConfig(uint8_t bootMode, String ssid, String password, String host, String mqttServer, String mqttUser, String mqttPasswd, int mqttPort) {
|
2016-03-11 00:43:24 +01:00
|
|
|
String eeprom;
|
|
|
|
|
2016-03-14 17:18:36 +01:00
|
|
|
eeprom = String(bootMode) + ";" + ssid + ";" + password + ";" + host + ";" + mqttServer + ";" + mqttUser + ";" + mqttPasswd + ";" + String(mqttPort) + ";";
|
2016-03-11 00:43:24 +01:00
|
|
|
|
2016-03-30 00:49:57 +02:00
|
|
|
if (eeprom.length() > CONFIG_EEPROM_SIZE )
|
2016-03-11 00:43:24 +01:00
|
|
|
return -EMSGSIZE;
|
|
|
|
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN("Saving " + eeprom);
|
2016-03-11 00:43:24 +01:00
|
|
|
|
2016-03-30 00:49:57 +02:00
|
|
|
for (int i = 0; i < eeprom.length() && i < CONFIG_EEPROM_SIZE; i++) {
|
2016-03-11 00:43:24 +01:00
|
|
|
EEPROM.write(i, eeprom.charAt(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
EEPROM.commit();
|
|
|
|
delay(100);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-24 14:54:55 +01:00
|
|
|
int EepromSaveBootMode(uint8_t bootMode) {
|
2016-03-14 01:47:43 +01:00
|
|
|
EEPROM.write(0, String(bootMode).charAt(0));
|
2016-03-11 00:43:24 +01:00
|
|
|
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-03-30 00:49:57 +02:00
|
|
|
if (i >= CONFIG_EEPROM_SIZE)
|
2016-03-14 01:47:43 +01:00
|
|
|
**element = '\0';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-24 14:54:55 +01:00
|
|
|
void EepromReadConfig(uint8_t &bootMode, char **ssid, char **password, char **host, char **mqttServer, char **mqttUser, char **mqttPasswd, int &mqttPort) {
|
2016-03-11 00:43:24 +01:00
|
|
|
|
|
|
|
int i = 2;
|
|
|
|
|
2016-03-14 17:18:36 +01:00
|
|
|
uint8_t boot = EEPROM.read(0);
|
|
|
|
char *mqttPortString = "";
|
2016-03-11 00:43:24 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-03-14 01:47:43 +01:00
|
|
|
readConfElement(ssid, i);
|
|
|
|
readConfElement(password, i);
|
|
|
|
readConfElement(host, i);
|
|
|
|
readConfElement(mqttServer, i);
|
|
|
|
readConfElement(mqttUser, i);
|
|
|
|
readConfElement(mqttPasswd, i);
|
2016-03-14 17:18:36 +01:00
|
|
|
readConfElement(&mqttPortString, i);
|
|
|
|
mqttPort = atoi(mqttPortString);
|
2016-03-11 00:43:24 +01:00
|
|
|
}
|