Skip to content

Commit

Permalink
Create ESP32_Connect_WiFi_Preferences.ino
Browse files Browse the repository at this point in the history
  • Loading branch information
RuiSantosdotme committed Mar 2, 2021
1 parent e54d124 commit edc838b
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Projects/ESP32/Preferences/ESP32_Connect_WiFi_Preferences.ino
@@ -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:
}

0 comments on commit edc838b

Please sign in to comment.