Created
August 13, 2020 11:16
-
-
Save Neumi/c96227416cf64a58bfcf09882d7f893f to your computer and use it in GitHub Desktop.
Small Arduino code snippet to save/read EPS8266 or ESP32 WIFI password and SSID from/to EEPROM.
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
#include <EEPROM.h> | |
int eeprom_size = 512; // depends on microcontroller | |
int max_ssid_size = 32; | |
int max_psk_size = 63; | |
void setup() | |
{ | |
EEPROM.begin(eeprom_size); | |
Serial.begin(115200); | |
Serial.println(""); | |
eraseEEPROM(); | |
storePSK("test_password_test_1231231231233test_password_test_1231231231mj"); | |
storeSSID("test_netowrk_name1234567890qwert"); | |
Serial.println("Start:"); | |
Serial.print(readPSK()); | |
Serial.print(","); | |
Serial.println(readSSID()); | |
} | |
void loop() | |
{ | |
} | |
void storePSK(String router_PSK) { | |
if (router_PSK.length() > max_psk_size) { | |
Serial.println("PSK too long!"); | |
return; | |
} | |
for (int i = 0; i < router_PSK.length(); i++) | |
{ | |
EEPROM.write(max_ssid_size + i, router_PSK[i]); | |
} | |
EEPROM.commit(); | |
} | |
void storeSSID(String router_SSID) { | |
if (router_SSID.length() > max_ssid_size) { | |
Serial.println("SSID too long!"); | |
return; | |
} | |
for (int i = 0; i < router_SSID.length(); i++) | |
{ | |
EEPROM.write(0 + i, router_SSID[i]); | |
} | |
EEPROM.commit(); | |
} | |
String readPSK() { | |
String router_PSK; | |
for (int i = 0; i < max_psk_size; i++) | |
{ | |
router_PSK = router_PSK + char(EEPROM.read(max_ssid_size + i)); | |
} | |
return router_PSK; | |
} | |
String readSSID() { | |
String router_SSID; | |
for (int i = 0; i < max_ssid_size; i++) | |
{ | |
router_SSID = router_SSID + char(EEPROM.read(0 + i)); | |
} | |
return router_SSID; | |
} | |
void eraseEEPROM() { | |
EEPROM.begin(eeprom_size); | |
for (int i = 0 ; i < eeprom_size ; i++) { | |
EEPROM.write(i, 0); | |
} | |
delay(200); | |
EEPROM.commit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment