Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create ESP32_Connect_WiFi_Preferences.ino
- Loading branch information
1 parent
e54d124
commit edc838b
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
Projects/ESP32/Preferences/ESP32_Connect_WiFi_Preferences.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
Rui Santos | ||
Complete project details at https://RandomNerdTutorials.com/esp32-save-data-permanently-preferences/ | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files. | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
*/ | ||
|
||
#include <Preferences.h> | ||
#include "WiFi.h" | ||
|
||
Preferences preferences; | ||
|
||
String ssid; | ||
String password; | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
Serial.println(); | ||
|
||
preferences.begin("credentials", false); | ||
|
||
ssid = preferences.getString("ssid", ""); | ||
password = preferences.getString("password", ""); | ||
|
||
if (ssid == "" || password == ""){ | ||
Serial.println("No values saved for ssid or password"); | ||
} | ||
else { | ||
// Connect to Wi-Fi | ||
WiFi.mode(WIFI_STA); | ||
WiFi.begin(ssid.c_str(), password.c_str()); | ||
Serial.print("Connecting to WiFi .."); | ||
while (WiFi.status() != WL_CONNECTED) { | ||
Serial.print('.'); | ||
delay(1000); | ||
} | ||
Serial.println(WiFi.localIP()); | ||
} | ||
} | ||
|
||
void loop() { | ||
// put your main code here, to run repeatedly: | ||
} |