Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed and improved WiFiSettings example #399

Merged
merged 4 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions examples/Settings/MultiWiFi/MultiWiFi.ino
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@
#include <WiFi.h>
#endif

#include "FS.h"
#include "LittleFS.h"
#include "ArduinoJson.h"
#include <LittleFS.h>
#include <ArduinoJson.h>

#include "SinricPro.h"
#include "SinricProSwitch.h"
#include <SinricPro.h>
#include <SinricProSwitch.h>
#include "SinricProWiFiSettings.h"

#define APP_KEY "" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
Expand All @@ -52,7 +51,7 @@ const char* primaryPassword = ""; // Set to your primary wifi's password
const char* secondarySSID = ""; // Set to your secondary wifi's ssid
const char* secondaryPassword = ""; // Set to your secondary wifi's password

SinricProWiFiSettings spws(primarySSID, primaryPassword, secondarySSID, secondaryPassword, "/wificonfig.dat");
SinricProWiFiSettings spws(LittleFS, primarySSID, primaryPassword, secondarySSID, secondaryPassword, "/wificonfig.dat");

bool onSetModuleSetting(const String& id, const String& value) {
// Handle module settings.
Expand Down
39 changes: 16 additions & 23 deletions examples/Settings/MultiWiFi/SinricProWiFiSettings.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "SinricProWiFiSettings.h"

SinricProWiFiSettings::SinricProWiFiSettings(const char* defaultPrimarySSID, const char* defaultPrimaryPassword,
SinricProWiFiSettings::SinricProWiFiSettings(fs::FS& fs,
const char* defaultPrimarySSID, const char* defaultPrimaryPassword,
const char* defaultSecondarySSID, const char* defaultSecondaryPassword,
const char* configFileName)
: defaultPrimarySSID(defaultPrimarySSID), defaultPrimaryPassword(defaultPrimaryPassword),
: fs(fs)
, defaultPrimarySSID(defaultPrimarySSID), defaultPrimaryPassword(defaultPrimaryPassword),
defaultSecondarySSID(defaultSecondarySSID), defaultSecondaryPassword(defaultSecondaryPassword),
configFileName(configFileName) {
memset(&wifiSettings, 0, sizeof(wifiSettings));
Expand All @@ -18,8 +20,8 @@ void SinricProWiFiSettings::begin() {

void SinricProWiFiSettings::updatePrimarySettings(const char* newSSID, const char* newPassword) {
if (isValidSetting(newSSID, newPassword)) {
strncpy(wifiSettings.primarySSID, newSSID, sizeof(wifiSettings.primarySSID));
strncpy(wifiSettings.primaryPassword, newPassword, sizeof(wifiSettings.primaryPassword));
strlcpy(wifiSettings.primarySSID, newSSID, sizeof(wifiSettings.primarySSID));
strlcpy(wifiSettings.primaryPassword, newPassword, sizeof(wifiSettings.primaryPassword));
saveToFile();
} else {
Serial.println("Invalid Primary SSID or Password");
Expand All @@ -28,8 +30,8 @@ void SinricProWiFiSettings::updatePrimarySettings(const char* newSSID, const cha

void SinricProWiFiSettings::updateSecondarySettings(const char* newSSID, const char* newPassword) {
if (isValidSetting(newSSID, newPassword)) {
strncpy(wifiSettings.secondarySSID, newSSID, sizeof(wifiSettings.secondarySSID));
strncpy(wifiSettings.secondaryPassword, newPassword, sizeof(wifiSettings.secondaryPassword));
strlcpy(wifiSettings.secondarySSID, newSSID, sizeof(wifiSettings.secondarySSID));
strlcpy(wifiSettings.secondaryPassword, newPassword, sizeof(wifiSettings.secondaryPassword));
saveToFile();
} else {
Serial.println("Invalid Secondary SSID or Password");
Expand All @@ -44,12 +46,7 @@ void SinricProWiFiSettings::printSettings() const {
}

void SinricProWiFiSettings::saveToFile() {

#if defined(ESP8266)
File file = LittleFS.open(configFileName, "w");
#elif defined(ESP32)
File file = LittleFS.open(configFileName, FILE_WRITE);
#endif
File file = fs.open(configFileName, "w");

if (file) {
file.write(reinterpret_cast<const uint8_t*>(&wifiSettings), sizeof(wifiSettings));
Expand All @@ -58,11 +55,7 @@ void SinricProWiFiSettings::saveToFile() {
}

bool SinricProWiFiSettings::loadFromFile() {
#if defined(ESP8266)
File file = LittleFS.open(configFileName, "r");
#elif defined(ESP32)
File file = LittleFS.open(configFileName, FILE_READ);
#endif
File file = fs.open(configFileName, "r");

if (file && file.size() == sizeof(wifiSettings)) {
file.read(reinterpret_cast<uint8_t*>(&wifiSettings), sizeof(wifiSettings));
Expand All @@ -75,18 +68,18 @@ bool SinricProWiFiSettings::loadFromFile() {
void SinricProWiFiSettings::saveDefaultSettings() {
Serial.println("Saving default WiFi login!");

strncpy(wifiSettings.primarySSID, defaultPrimarySSID, sizeof(wifiSettings.primarySSID));
strncpy(wifiSettings.primaryPassword, defaultPrimaryPassword, sizeof(wifiSettings.primaryPassword));
strncpy(wifiSettings.secondarySSID, defaultSecondarySSID, sizeof(wifiSettings.secondarySSID));
strncpy(wifiSettings.secondaryPassword, defaultSecondaryPassword, sizeof(wifiSettings.secondaryPassword));
strlcpy(wifiSettings.primarySSID, defaultPrimarySSID, sizeof(wifiSettings.primarySSID));
strlcpy(wifiSettings.primaryPassword, defaultPrimaryPassword, sizeof(wifiSettings.primaryPassword));
strlcpy(wifiSettings.secondarySSID, defaultSecondarySSID, sizeof(wifiSettings.secondarySSID));
strlcpy(wifiSettings.secondaryPassword, defaultSecondaryPassword, sizeof(wifiSettings.secondaryPassword));

saveToFile();
}

void SinricProWiFiSettings::deleteAllSettings() {
memset(&wifiSettings, 0, sizeof(wifiSettings));
if (LittleFS.exists(configFileName)) {
LittleFS.remove(configFileName);
if (fs.exists(configFileName)) {
fs.remove(configFileName);
}
Serial.println("All WiFi settings have been deleted.");
}
Expand Down
15 changes: 8 additions & 7 deletions examples/Settings/MultiWiFi/SinricProWiFiSettings.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#pragma once

#include <Arduino.h>
#include "FS.h"
#include "LittleFS.h"
#include <FS.h>


/**
Expand All @@ -11,10 +10,10 @@
class SinricProWiFiSettings {
public:
struct wifi_settings_t {
char primarySSID[32]; ///< Primary SSID of the WiFi network.
char primaryPassword[64]; ///< Primary password of the WiFi network.
char secondarySSID[32]; ///< Secondary SSID of the WiFi network.
char secondaryPassword[64]; ///< Secondary password of the WiFi network.
char primarySSID[32+1]; ///< Primary SSID of the WiFi network.
char primaryPassword[64+1]; ///< Primary password of the WiFi network.
char secondarySSID[32+1]; ///< Secondary SSID of the WiFi network.
char secondaryPassword[64+1]; ///< Secondary password of the WiFi network.
};

/**
Expand All @@ -26,7 +25,8 @@ class SinricProWiFiSettings {
* @param defaultSecondaryPassword Default secondary password.
* @param configFileName File name for storing WiFi settings.
*/
SinricProWiFiSettings(const char* defaultPrimarySSID, const char* defaultPrimaryPassword,
SinricProWiFiSettings(fs::FS& fs,
const char* defaultPrimarySSID, const char* defaultPrimaryPassword,
const char* defaultSecondarySSID, const char* defaultSecondaryPassword,
const char* configFileName);

Expand Down Expand Up @@ -71,6 +71,7 @@ class SinricProWiFiSettings {
const wifi_settings_t& getWiFiSettings() const;

private:
fs::FS& fs; // filesystem to be used
const char* defaultPrimarySSID; ///< Default primary SSID.
const char* defaultPrimaryPassword; ///< Default primary password.
const char* defaultSecondarySSID; ///< Default secondary SSID.
Expand Down
Loading