diff --git a/src/drivers/devices/esp32CAM.h b/src/drivers/devices/esp32CAM.h index 08e0377..0dbb5f6 100644 --- a/src/drivers/devices/esp32CAM.h +++ b/src/drivers/devices/esp32CAM.h @@ -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_ \ No newline at end of file diff --git a/src/drivers/memoryCard/SDCard.h b/src/drivers/memoryCard/SDCard.h new file mode 100644 index 0000000..893e737 --- /dev/null +++ b/src/drivers/memoryCard/SDCard.h @@ -0,0 +1,121 @@ +#ifndef _SDCARD_H_ +#define _SDCARD_H_ + +#include +#include +#include + +#include + +#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(); + Settings.Timezone = json["Timezone"].as(); + 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_ diff --git a/src/wManager.cpp b/src/wManager.cpp index 3151e58..ea90a76 100644 --- a/src/wManager.cpp +++ b/src/wManager.cpp @@ -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,8 +96,13 @@ void init_WifiManager() Settings = SPIFS.loadConfigFile(); if (!Settings.holdsData) { - Serial.println(F("Forcing config mode as there is no saved config")); - forceConfig = true; + 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)