Storing WiFi credentials in flash memory on a Wemod D1 R2
Posted: Tue Feb 14, 2017 6:27 pm
Hi all,
I'm trying to store my wifi credentials on my Wemos D1 R2. I'm using the ESP library to access the flash memory and I'm having a heck of a time getting it figured out. I have written a test bed that demonstrates the problem. What I try to write to flash seems to write OK, but when I try to read it back, it never matches.
/*
* The purpose of this program is to initialize the flash memory on the
* board where configuration information is stored. This needs to be performed before the board
* is loaded with the final sketch
*/
#include <ESP8266WiFi.h>
#define CONFIG_SECTOR 0x80-4
#define CONFIG_ADDRESS ( CONFIG_SECTOR * 4096 )
#define IS_CONFIGURED 0xFEFE
#define NOT_CONFIGURED 0xFFFF
typedef struct {
int id;
char name[16];
char location[64];
byte macAddress[6]; // MAC address of the wifi
char ssid[64];
char pw[64];
} Configuration __attribute__ ((packed));
Configuration config;
Configuration verifyConfig;
byte deviceData[sizeof(config)];
// Serial Data
#define ReceivedBufferLength 20
char receivedBuffer[ReceivedBufferLength + 1]; // store the serial command
byte receivedBufferIndex = 0;
void setup() {
Serial.begin(115200);
Serial.println("");
config.id = NOT_CONFIGURED;
strcpy(config.name, "Unknown");
strcpy(config.location, "Unknown location");
WiFi.macAddress(config.macAddress); // Get the MAC address
strcpy(config.ssid, "********");
strcpy(config.pw, "********");
Serial.print("Original name = ");
Serial.println(config.name);
// Write the initialized configuration to the board.
Serial.println("Erasing the memory...");
ESP.flashEraseSector(CONFIG_SECTOR);
delay(1000); // Just in case timing matters after a write
Serial.println("Flashing the initialized configuration...");
ESP.flashWrite(CONFIG_ADDRESS, (uint32_t *)&config, (size_t)sizeof(config));
delay(1000); // Just in case timing matters after a write
// Verify what we've written
Serial.println("Verifying the written data...");
ESP.flashRead(CONFIG_ADDRESS, (uint32_t *)&config, (size_t)sizeof(config));
if(verifyConfig.id == NOT_CONFIGURED)
Serial.println("Initialized. Please unplug");
else {
Serial.print("Error verifying initialization. ID field read as ");
Serial.println(verifyConfig.id);
Serial.print("Name = ");
Serial.println(verifyConfig.name);
}
}
Any ideas what I am doing wrong? I need to be able to store the SSID and password onboard the wemos so that it will survive a loss of power. Thanks in advance!
I'm trying to store my wifi credentials on my Wemos D1 R2. I'm using the ESP library to access the flash memory and I'm having a heck of a time getting it figured out. I have written a test bed that demonstrates the problem. What I try to write to flash seems to write OK, but when I try to read it back, it never matches.
/*
* The purpose of this program is to initialize the flash memory on the
* board where configuration information is stored. This needs to be performed before the board
* is loaded with the final sketch
*/
#include <ESP8266WiFi.h>
#define CONFIG_SECTOR 0x80-4
#define CONFIG_ADDRESS ( CONFIG_SECTOR * 4096 )
#define IS_CONFIGURED 0xFEFE
#define NOT_CONFIGURED 0xFFFF
typedef struct {
int id;
char name[16];
char location[64];
byte macAddress[6]; // MAC address of the wifi
char ssid[64];
char pw[64];
} Configuration __attribute__ ((packed));
Configuration config;
Configuration verifyConfig;
byte deviceData[sizeof(config)];
// Serial Data
#define ReceivedBufferLength 20
char receivedBuffer[ReceivedBufferLength + 1]; // store the serial command
byte receivedBufferIndex = 0;
void setup() {
Serial.begin(115200);
Serial.println("");
config.id = NOT_CONFIGURED;
strcpy(config.name, "Unknown");
strcpy(config.location, "Unknown location");
WiFi.macAddress(config.macAddress); // Get the MAC address
strcpy(config.ssid, "********");
strcpy(config.pw, "********");
Serial.print("Original name = ");
Serial.println(config.name);
// Write the initialized configuration to the board.
Serial.println("Erasing the memory...");
ESP.flashEraseSector(CONFIG_SECTOR);
delay(1000); // Just in case timing matters after a write
Serial.println("Flashing the initialized configuration...");
ESP.flashWrite(CONFIG_ADDRESS, (uint32_t *)&config, (size_t)sizeof(config));
delay(1000); // Just in case timing matters after a write
// Verify what we've written
Serial.println("Verifying the written data...");
ESP.flashRead(CONFIG_ADDRESS, (uint32_t *)&config, (size_t)sizeof(config));
if(verifyConfig.id == NOT_CONFIGURED)
Serial.println("Initialized. Please unplug");
else {
Serial.print("Error verifying initialization. ID field read as ");
Serial.println(verifyConfig.id);
Serial.print("Name = ");
Serial.println(verifyConfig.name);
}
}
Any ideas what I am doing wrong? I need to be able to store the SSID and password onboard the wemos so that it will survive a loss of power. Thanks in advance!