Load settings from SD card.

This commit is contained in:
elmo128 2023-09-12 09:29:50 +02:00
parent 37ca603ed5
commit 43f5d8fd5b
3 changed files with 137 additions and 4 deletions

View File

@ -2,8 +2,15 @@
#define _ESP32CAM_H_
#define PIN_BUTTON_1 0
#define LED_PIN 2
#define LED_PIN 33
#define NO_DISPLAY
#define SDMMC_CLK 14
#define SDMMC_CMD 15
#define SDMMC_D0 2
#define SDMMC_D1 4
#define SDMMC_D2 12
#define SDMMC_D3 13
#endif // _ESP32_CAM_H_

View File

@ -0,0 +1,121 @@
#ifndef _SDCARD_H_
#define _SDCARD_H_
#include <FS.h>
#include <SD_MMC.h>
#include <SD.h>
#include <ArduinoJson.h>
#include "..\drivers.h"
#include "..\storage.h"
#include "..\SPIStorage\SPIStorage.h"
#define JSON_CONFIG_FILE "/config.json"
class SDCard
{
private:
bool cardInitialized_;
public:
SDCard()
{
cardInitialized_ = initSDcard();
}
~SDCard()
{
if (cardInitialized_)
SD_MMC.end();
}
bool initSDcard()
{
if (cardInitialized_)
return cardInitialized_;
bool oneBitMode = true;
#if defined (SDMMC_D0) && defined (SDMMC_D1) && defined (SDMMC_D2) && defined (SDMMC_D3)
if (SD_MMC.cardType() == CARD_NONE)
SD_MMC.setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0, SDMMC_D1, SDMMC_D2, SDMMC_D3);
oneBitMode = false;
#elif defined (SDMMC_D0) && !(defined (SDMMC_D1) && defined (SDMMC_D2) && defined (SDMMC_D3))
if (SD_MMC.cardType() == CARD_NONE)
SD_MMC.setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0);
#else
Serial.println("SD card interface not available.");
return false;
#endif // dataPinsDefined
if ((!SD_MMC.begin("/sdcard", oneBitMode)) || (SD_MMC.cardType() == CARD_NONE))
{
Serial.println("No card available.");
return false;
}
return true;
}
TSettings loadConfigFile()
{
// Load existing configuration file
// Read configuration from FS json
Serial.println("Mounting SD card...");
TSettings Settings;
if (initSDcard())
{
Serial.println("mounted SD card.");
if (SD_MMC.exists(JSON_CONFIG_FILE))
{
// The file exists, reading and loading
Serial.println("reading config file from sd.");
File configFile = SD_MMC.open(JSON_CONFIG_FILE, "r");
if (configFile)
{
Serial.println("Opened configuration file");
StaticJsonDocument<512> json;
DeserializationError error = deserializeJson(json, configFile);
configFile.close();
serializeJsonPretty(json, Serial);
if (!error)
{
Serial.println("Parsing JSON");
strcpy(Settings.WifiSSID, json["SSID"]);
strcpy(Settings.WifiPW, json["Password"]);
strcpy(Settings.PoolAddress, json["PoolURL"]);
strcpy(Settings.BtcWallet, json["WalletID"]);
Settings.PoolPort = json["Port"].as<int>();
Settings.Timezone = json["Timezone"].as<int>();
Settings.holdsData = true;
}
else
{
// Error loading JSON data
Serial.println("Failed to load json config");
}
}
SD_MMC.end();
}
}
else
{
// Error mounting file system
Serial.println("Failed to mount SD card.");
}
return Settings;
}
void SD2SPIStorage(SPIStorage* spifs)
{
TSettings Settings = loadConfigFile();
if (Settings.holdsData)
{
spifs->saveConfigFile(&Settings);
WiFi.begin(Settings.WifiSSID, Settings.WifiPW);
Serial.println("Settings copied from SD card. Rebooting now.");
ESP.restart();
}
}
};
#endif // _SDCARD_H_

View File

@ -8,7 +8,7 @@
#include "wManager.h"
#include "monitor.h"
#include "drivers/display.h"
#include "drivers/memoryCard/SDCard.h"
#include "drivers/SPIStorage/SPIStorage.h"
#include "drivers/storage.h"
@ -96,9 +96,14 @@ void init_WifiManager()
Settings = SPIFS.loadConfigFile();
if (!Settings.holdsData)
{
Serial.println(F("Forcing config mode as there is no saved config"));
SDCard sdc;
if (sdc.loadConfigFile().holdsData)
{
Serial.println(F("No config file on internal flash, force config mode."));
sdc.SD2SPIStorage(&SPIFS); // reboot on success.
forceConfig = true;
};
};
// Reset settings (only for development)
//wm.resetSettings();