Skip to content

Commit 4f9c4fb

Browse files
authored
chore: refactor example
1 parent 40ff9ed commit 4f9c4fb

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

examples/Settings/MultiWiFi/MultiWiFi.ino

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -68,50 +68,35 @@ bool onSetModuleSetting(const String& id, const String& value) {
6868

6969
const char* password = doc["password"].as<const char*>();
7070
const char* ssid = doc["ssid"].as<const char*>();
71+
bool connectNow = doc["connectNow"] | false;
7172

7273
if (id == SET_WIFI_PRIMARY) { // Set primary WiFi
7374
spws.updatePrimarySettings(ssid, password);
7475
} else if (id == SET_WIFI_SECONDARY) { // Set secondary WiFi
7576
spws.updateSecondarySettings(ssid, password);
7677
}
77-
78-
bool connectNow = doc["connectNow"] | false;
79-
if(connectNow) {
80-
return connectToWiFi(ssid, password);
81-
}
82-
83-
return true;
78+
79+
return connectNow ? connectToWiFi(ssid, password) : true;
8480
}
8581

8682
bool setupLittleFS() {
87-
// Sets up the LittleFS.
8883
#if defined(ESP8266)
89-
if (!LittleFS.begin()) {
84+
if (!LittleFS.begin() && formatLittleFSIfFailed) {
9085
#elif defined(ESP32)
91-
if (!LittleFS.begin(true)) {
86+
if (!LittleFS.begin(true) && formatLittleFSIfFailed) {
9287
#endif
93-
94-
Serial.println("An Error has occurred while mounting LittleFS");
95-
96-
if (formatLittleFSIfFailed) {
97-
Serial.println("Formatting LittleFS...");
98-
if (LittleFS.format()) {
99-
Serial.println("LittleFS formatted successfully!");
100-
return true;
101-
} else {
102-
Serial.println("LittleFS formatting error!");
103-
return false;
104-
}
88+
Serial.println("Formatting LittleFS...");
89+
if (LittleFS.format()) {
90+
Serial.println("LittleFS formatted successfully!");
91+
return true;
10592
} else {
106-
Serial.println("LittleFS error!");
93+
Serial.println("LittleFS formatting error!");
10794
return false;
10895
}
10996
} else {
11097
Serial.println("LittleFS Mount successful");
11198
return true;
11299
}
113-
114-
return true;
115100
}
116101

117102
// setup function for WiFi connection
@@ -121,7 +106,7 @@ void setupWiFi() {
121106
// Load settings from file or using defaults if loading fails.
122107
spws.begin();
123108

124-
const SinricProWiFiSettings::wifi_settings_t& settings = spws.getWiFiSettings();
109+
auto& settings = spws.getWiFiSettings();
125110
bool connected = false;
126111

127112
if (spws.isValidSetting(settings.primarySSID, settings.primaryPassword)) {
@@ -131,6 +116,12 @@ void setupWiFi() {
131116
if (!connected && spws.isValidSetting(settings.secondarySSID, settings.secondaryPassword)) {
132117
connected = connectToWiFi(settings.secondarySSID, settings.secondaryPassword);
133118
}
119+
120+
if (connected) {
121+
Serial.println("Connected to WiFi!");
122+
} else {
123+
Serial.println("Failed to connect to WiFi!");
124+
}
134125
}
135126

136127
bool connectToWiFi(const char* ssid, const char* password) {
@@ -146,8 +137,6 @@ bool connectToWiFi(const char* ssid, const char* password) {
146137
#elif defined(ESP8266)
147138
WiFi.setSleepMode(WIFI_NONE_SLEEP);
148139
WiFi.begin(ssid, password);
149-
#elif defined(ARDUINO_ARCH_RP2040)
150-
WiFi.begin(ssid, password);
151140
#endif
152141

153142
int timeout = 0;

0 commit comments

Comments
 (0)