-
Notifications
You must be signed in to change notification settings - Fork 2
/
save_wifi_factory.ino
51 lines (42 loc) · 1.31 KB
/
save_wifi_factory.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <EEPROM.h>
const int eepromSize = 512;
const int eepromStartAddress = 0;
void writeStringToEEPROM(int addr, const String &data) {
for (size_t i = 0; i < data.length(); i++) {
EEPROM.write(addr + i, data[i]);
}
EEPROM.write(addr + data.length(), 0); // Null-terminate the string
EEPROM.commit();
}
String readStringFromEEPROM(int addr) {
String data = "";
char c;
do {
c = EEPROM.read(addr);
if (c != 0) {
data += c;
addr++;
}
} while (c != 0);
return data;
}
void setup() {
Serial.begin(115200);
EEPROM.begin(eepromSize);
// Clean the EEPROM by writing zeros
for (int i = 0; i < eepromSize; i++) {
EEPROM.write(i, 0);
}
EEPROM.commit();
// Define and save the JSON data
String jsonData = "{\"Ssid\":\"ssid_of_your_wifi\",\"Password\":\"password_of_your_wifi\"}";
writeStringToEEPROM(eepromStartAddress, jsonData);
// Read the JSON data from EEPROM
String savedJson = readStringFromEEPROM(eepromStartAddress);
// Print the saved SSID and Password
Serial.println("Saved SSID: " + savedJson.substring(savedJson.indexOf("Ssid") + 7, savedJson.indexOf("\",\"Password\"")));
Serial.println("Saved Password: " + savedJson.substring(savedJson.indexOf("Password") + 11, savedJson.length() - 2));
}
void loop() {
// Your main code can go here if needed
}