Skip to content

eeprom can't work #2174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nitroge opened this issue Dec 10, 2018 · 5 comments
Closed

eeprom can't work #2174

nitroge opened this issue Dec 10, 2018 · 5 comments

Comments

@nitroge
Copy link

nitroge commented Dec 10, 2018

#include <MyConfig.h>
#include <M5Stack.h>
MyConfig myConfig;
MyConfig::WifiInfo info;
void setup()
{

Serial.begin(9600);
Serial.println();
Serial.println("start");
myConfig.saveWifiInfo(info);
delay(100);
MyConfig::WifiInfo temp = myConfig.readWifiInfo();
delay(100);
Serial.println(temp.ssid.c_str());
Serial.println(temp.pwd.c_str());
Serial.println("done");

}

void loop()
{
// put your main code here, to run repeatedly:
}

#ifndef MY_CHONFIG_H
#define MY_CHONFIG_H
#include <WString.h>
class MyConfig
{
public:
struct WifiInfo
{
String ssid;
String pwd;
};
void saveWifiInfo(WifiInfo info);
WifiInfo readWifiInfo();
};

#endif

#include <MyConfig.h>
#include "EEPROM.h"

EEPROMClass INFO("eeprom0", 0x1000);

void MyConfig::saveWifiInfo(MyConfig::WifiInfo info)
{
INFO.put(0, info);
INFO.commit();
}

MyConfig::WifiInfo MyConfig::readWifiInfo()
{
MyConfig::WifiInfo temp;
temp = INFO.get(0, temp);
return temp;
}

output:
start

0

done

@lbernstone
Copy link
Contributor

Not sure if it is an issue with the structure you are using? This works for me:

#include <EEPROM.h>

EEPROMClass INFO("eeprom0", 0x1000);

void setup() {
  Serial.begin(115200);
  INFO.put(0,(char*)"test");
    
}

void loop() {
  char* t=INFO.get(0,t);
  Serial.println(t);
  delay(1000);
}

@nitroge
Copy link
Author

nitroge commented Dec 11, 2018

Thank you for your answer.
Please try this.
step 1 upload this
#include <EEPROM.h>

EEPROMClass INFO("eeprom0", 0x1000);

void setup() {
Serial.begin(115200);
INFO.put(0,(char*)"test");

}

void loop() {
char* t=INFO.get(0,t);
Serial.println(t);
delay(1000);
}

This output is correct.

step 2 upload this

#include <EEPROM.h>

EEPROMClass INFO("eeprom0", 0x1000);

void setup() {
Serial.begin(115200);
// INFO.put(0,(char*)"test");

}

void loop() {
char* t=INFO.get(0,t);
Serial.println(t);
delay(1000);
}

no output

This output is error.

@SocketNet
Copy link

I had the same problem. 😂

@lbernstone
Copy link
Contributor

Ok, I looked through the code to refresh my memory of how this works.
You need to have a .begin() for the EEPROM instance. Initializing creates the data structures, but does not attach them to the physical partition. begin does so, and will return false if it is unable to open the partition you have named.

@nitroge nitroge closed this as completed Dec 14, 2018
@nitroge
Copy link
Author

nitroge commented Dec 14, 2018

I had the same problem. 😂

EEPROM.begin(EEPROM.length()) 改为固定大小似乎可以, 比如 EEPROM.begin(2048);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants