So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By jdavies
#62580 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!
User avatar
By jeffas
#62703 You might find this GengusKahn's contribution to this post interesting:
viewtopic.php?f=160&t=13583
The WiFi library stores credentials in flash anyway, so you may not need this flash-writing sketch at all.
(The original poster and I wanted to prevent credentials being stored, so you probably won't find our contributions on that thread useful.)
User avatar
By jdavies
#62706 Thanks for pointing me to that post. That help with the WiF aspect. However, I still need to store additional non-Wifi configuration information into flash (a device name, etc). The idea is to have someone with no wemos/arduino skills be able to set these values for their device via bluetooth and allow these setting to survive a power-down.

Looks like the Wifi part may be ok though!

- Jeff