iamtheyammer

save/load wifi credentials in eeprom esp8266

Aug 26th, 2018
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include <EEPROM.h>
  2.  
  3. char wifi_ssid_private[32];
  4. char wifi_password_private[32];
  5. char clientName[10] = "newClient";
  6. char ipAddr[16] = "172.024.001.001";//Pi Access Point IP-Adr.
  7.  
  8. //startAdr: offset (bytes), writeString: String to be written to EEPROM
  9. void writeEEPROM(int startAdr, int length, char* writeString) {
  10.   EEPROM.begin(512); //Max bytes of eeprom to use
  11.   yield();
  12.   Serial.println();
  13.   Serial.print("writing EEPROM: ");
  14.   //write to eeprom
  15.   for (int i = 0; i < length; i++)
  16.     {
  17.       EEPROM.write(startAdr + i, writeString[i]);
  18.       Serial.print(writeString[i]);
  19.     }
  20.   EEPROM.commit();
  21.   EEPROM.end();          
  22. }
  23.  
  24. void readEEPROM(int startAdr, int maxLength, char* dest) {
  25.   EEPROM.begin(512);
  26.   delay(10);
  27.   for (int i = 0; i < maxLength; i++)
  28.     {
  29.       dest[i] = char(EEPROM.read(startAdr + i));
  30.     }
  31.   EEPROM.end();    
  32.   Serial.print("ready reading EEPROM:");
  33.   Serial.println(dest);
  34. }
  35.  
  36. void setup() {
  37.   Serial.begin(9600);
  38.   delay(100);
  39.  
  40.   strcat(wifi_ssid_private, "SSID1234");
  41.   strcat(wifi_password_private, "PW1234");
  42.  
  43.   writeEEPROM(0,32,wifi_ssid_private);//32 byte max length
  44.   writeEEPROM(32,32, wifi_password_private);//32 byte max length
  45.   writeEEPROM(64,10, clientName);//10 byte max length
  46.   writeEEPROM(74,16, ipAddr);//16 byte max length
  47.   /*85 byte saved in total?*/  
  48.   Serial.println("everything saved...");
  49.   readEEPROM(0,32,wifi_ssid_private);
  50.   readEEPROM(32,32,wifi_password_private);
  51.   readEEPROM(64,10,clientName);
  52.   readEEPROM(74,16,ipAddr);
  53. }
  54.  
  55. void loop() {
  56.   // put your main code here, to run repeatedly:
  57.   Serial.println("Loop");
  58.   delay(1000);
  59. }
Advertisement
Add Comment
Please, Sign In to add comment