-
Notifications
You must be signed in to change notification settings - Fork 7.4k
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
Storing email in NVS (IDFGH-3522) #5475
Comments
Hi @AbdeenM, the code to store the email address looks OK. Dots "." shouldn't be an issue, not sure about "@" right now. We'll have a look at this. Thanks for posting this here. |
Hi @0xjakob thanks for the comment, you are right i forgot to mention i did several tests, for example storing "abdeen" alone, then "abdeen.mohd" then "abdeen.mohamed" as values and it worked fine so "." is not the issue. |
@AbdeenM
This code works, the email address is printed after the reboot. I wonder whether it's an encoding issue... Actually, '@' also exists in the normal ascii table. So if the issue persists, one way to solve this would be to convert the string to ascii before saving it. |
HI @0xjakob thank you for the response! /**
* @brief Run tests for saving to NVS.
*/
void test_nvs() {
// Initialize NVS
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
// NVS partition was truncated and needs to be erased
// Retry nvs_flash_init
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
ESP_ERROR_CHECK( err );
// Open
printf("\n");
printf("Opening Non-Volatile Storage (NVS) handle... ");
nvs_handle my_handle;
err = nvs_open("storage", NVS_READWRITE, &my_handle);
if (err != ESP_OK) {
printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err));
} else {
printf("Done\n");
// Read
printf("Reading restart counter from NVS ... ");
int32_t restart_counter = 0; // value will default to 0, if not set yet in NVS
char data [128] = "[email protected]";
char str [128];
size_t length;
err = nvs_get_str(my_handle, "user_email", str, &length);
switch (err) {
case ESP_OK:
printf("Done\n");
printf("email address: = %s\n", str);
break;
case ESP_ERR_NVS_NOT_FOUND:
printf("The email addr is not initialized yet!\n");
// Write
printf("Updating email address NVS ... ");
restart_counter++;
err = nvs_set_str(my_handle, "user_email", data);
printf((err != ESP_OK) ? "Failed!\n" : "Done\n");
// Commit written value.
// After setting any values, nvs_commit() must be called to ensure changes are written
// to flash storage. Implementations may write to storage at other times,
// but this is not guaranteed.
printf("Committing updates in NVS ... ");
err = nvs_commit(my_handle);
printf((err != ESP_OK) ? "Failed!\n" : "Done\n");
break;
default :
printf("Error (%s) reading!\n", esp_err_to_name(err));
}
// Close
nvs_close(my_handle);
}
printf("\n");
// Restart module
for (int i = 10; i >= 0; i--) {
printf("Restarting in %d seconds...\n", i);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
printf("Restarting now.\n");
fflush(stdout);
esp_restart();
} what you are doing is pretty much what am doing as well, i really don't get what could be wrong on my main code. i have attached a gist, please have a look and let me know what you think.
Thanks again |
It looks like you are calculating string_size based on device_id value, but
then using it to get user_email.
…On Sun, Jun 28, 2020, 11:26 Abdeen Mohamed ***@***.***> wrote:
HI @0xjakob <https://github.com/0xjakob> thank you for the response!
Okay so ran your code and it does work fine, i even modified it alittle
and it works with no issues (see below);
/** * @brief Run tests for saving to NVS. */void test_nvs() {
// Initialize NVS
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
// NVS partition was truncated and needs to be erased
// Retry nvs_flash_init
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
ESP_ERROR_CHECK( err );
// Open
printf("\n");
printf("Opening Non-Volatile Storage (NVS) handle... ");
nvs_handle my_handle;
err = nvs_open("storage", NVS_READWRITE, &my_handle);
if (err != ESP_OK) {
printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err));
} else {
printf("Done\n");
// Read
printf("Reading restart counter from NVS ... ");
int32_t restart_counter = 0; // value will default to 0, if not set yet in NVS
char data [128] = ***@***.***";
char str [128];
size_t length;
err = nvs_get_str(my_handle, "user_email", str, &length);
switch (err) {
case ESP_OK:
printf("Done\n");
printf("email address: = %s\n", str);
break;
case ESP_ERR_NVS_NOT_FOUND:
printf("The email addr is not initialized yet!\n");
// Write
printf("Updating email address NVS ... ");
restart_counter++;
err = nvs_set_str(my_handle, "user_email", data);
printf((err != ESP_OK) ? "Failed!\n" : "Done\n");
// Commit written value.
// After setting any values, nvs_commit() must be called to ensure changes are written
// to flash storage. Implementations may write to storage at other times,
// but this is not guaranteed.
printf("Committing updates in NVS ... ");
err = nvs_commit(my_handle);
printf((err != ESP_OK) ? "Failed!\n" : "Done\n");
break;
default :
printf("Error (%s) reading!\n", esp_err_to_name(err));
}
// Close
nvs_close(my_handle);
}
printf("\n");
// Restart module
for (int i = 10; i >= 0; i--) {
printf("Restarting in %d seconds...\n", i);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
printf("Restarting now.\n");
fflush(stdout);
esp_restart();
}
what you are doing is pretty much what am doing as well, i really don't
get what could be wrong on my main code. i have attached a gist
<https://gist.github.com/AbdeenM/6b6de7080952e59e2c1052f7e12bc5ea>,
please have a look and let me know what you think.
The flow is as follows:
1. once the device launches, it checks if wifi ssid and password are
available. (if not it runs wifi provisioning)
2. after confirming wifi is provisioned it checks if aws is
provisioned. (if not it runs aws provisioning)
3. after confirming aws is provisioned it connects to aws and handles
other functions.
Thanks again
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#5475 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABBFY6QBVJXHKY25GBDJ7XLRY4EDBANCNFSM4ODXXPEQ>
.
|
@igrr Of course! that was it. thank you so much for the catch, that fixed my problem. |
Environment
Problem Description
So am trying to save an email “[email protected]” to the devices memory using the function nvs_set_str() with a key “user_email” but the string fails to be written to memory, meaning after I store the email and fetch it from memory and log it I receive weird symbols mostly question marks as in “??/ ????” Or something similar. Even if I restart the device and fetch the key value I get the same thing.
Am positive the way am storing or my code is correct as I am storing other details along with the email and they are stored and fetched successfully.
My question is, is there a different or specific way to handle storing emails?
I suspect my issue could be because the email in question has a “.” or “@“ if that’s the cause is there a specific way to handle these characters? Because I need to store any given user email that may be supplied.
I have tried posting the issue in aws-freertos github repo but they suggested i revert to you.
Expected Behavior
Email storied in NVS.
Actual Behavior
Email not storing in NVS.
Code to reproduce this issue
The text was updated successfully, but these errors were encountered: