Skip to content

esp32-s2 chip using Preferences library to store string but lost after the restart #5622

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
Kevin-CEN opened this issue Sep 2, 2021 · 17 comments
Labels
IDE: PlaformIO Issue relates to PlatformIO IDE Resolution: Expired More info wasn't provided

Comments

@Kevin-CEN
Copy link

I wanted to use the Preferences library to store strings, using the ESP32-S2 chip, and using the official example StartCounter. ino to test, I compiled and burned without any problems, but when ESP32-S2 ran the program, the numbers didn't add up so should have been lost after the restart.
This is the output of the MCU:
ESP-ROM:esp32s2-rc4-20191025
Build:Oct 25 2019
rst:0xc (RTC_SW_CPU_RST),boot:0x8 (SPI_FAST_FLASH_BOOT)
Saved PC:0x40026168
SPIWP:0xee
mode:DIO, clock div:2
load:0x3ffe6100,len:0x424
load:0x4004c000,len:0x844
load:0x40050000,len:0x2460
entry 0x4004c180

Current counter value: 1
Restarting in 10 seconds...
ESP-ROM:esp32s2-rc4-20191025
Build:Oct 25 2019
rst:0xc (RTC_SW_CPU_RST),boot:0x8 (SPI_FAST_FLASH_BOOT)
Saved PC:0x40026168
SPIWP:0xee
mode:DIO, clock div:2
load:0x3ffe6100,len:0x424
load:0x4004c000,len:0x844
load:0x40050000,len:0x2460
entry 0x4004c180

Current counter value: 1
Restarting in 10 seconds...
ESP-ROM:esp32s2-rc4-20191025
Build:Oct 25 2019
rst:0xc (RTC_SW_CPU_RST),boot:0x8 (SPI_FAST_FLASH_BOOT)
Saved PC:0x40026168
SPIWP:0xee
mode:DIO, clock div:2
load:0x3ffe6100,len:0x424
load:0x4004c000,len:0x844
load:0x40050000,len:0x2460
entry 0x4004c180

Current counter value: 1
Restarting in 10 seconds...
ESP-ROM:esp32s2-rc4-20191025
Build:Oct 25 2019
rst:0xc (RTC_SW_CPU_RST),boot:0x8 (SPI_FAST_FLASH_BOOT)
Saved PC:0x40026168
SPIWP:0xee
mode:DIO, clock div:2
load:0x3ffe6100,len:0x424
load:0x4004c000,len:0x844
load:0x40050000,len:0x2460
entry 0x4004c180

Current counter value: 1
Restarting in 10 seconds...
############################
This is my platformio.ini configuration:
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
board_build.mcu = esp32s2
platform_packages =
framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32#2.0.0-alpha1
############################

@atanisoft
Copy link
Collaborator

@Kevin-CEN I think this may have been fixed by #5309 which should be available in the 2.0.0 release. Can you retest with the 2.0.0 release?

@Kevin-CEN
Copy link
Author

@Kevin-CEN I think this may have been fixed by #5309 which should be available in the 2.0.0 release. Can you retest with the 2.0.0 release?
Thanks, but I'm not quite sure how to get back to version 2.0.0.
Esp32-s2 doesn't have very good Arduino support.

@atanisoft
Copy link
Collaborator

@Kevin-CEN Adjust your platformio.ini to have:

framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32#2.0.0

Most of the ESP32-S2 peripherals should have compatible Arduino code already, if you find an exception to this though do log an issue for tracking/consideration.

@Kevin-CEN
Copy link
Author

@atanisoft
Thank you very much, I should be using version 2.0.0, but I added '-alpha1', if not '-alpha1' will have this error

/Users/kevin/.platformio/packages/toolchain-xtensa32s2/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status
*** [.pio/build/esp32dev/firmware.elf] Error 1

@atanisoft
Copy link
Collaborator

@Kevin-CEN That link error looks related to PIO breaking the builds, I guess you will need to wait for the 2.0.0 release to be picked up by PIO instead. Usually they will pick up a new stable release within a few days of release.

@Kevin-CEN
Copy link
Author

@atanisoft Thank you very much.The problem doesn't seem to have been solved.

@chegewara
Copy link
Contributor

chegewara commented Sep 2, 2021

I can confirm there is in general issue with NVS storage on PIO using arduino v2.0.0+.
platformio/platform-espressif32#564

There is also some topic on forum about it.

@Jason2866
Copy link
Collaborator

Mhh, i get this

ESP-ROM:esp32s2-rc4-20191025
Build:Oct 25 2019
rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DOUT, clock div:1
load:0x3ffe6100,len:0x7c
load:0x4004c000,len:0x7e4
load:0x40050000,len:0x2598
entry 0x4004c15c

Current counter value: 7
Restarting in 10 seconds...

ESP-ROM:esp32s2-rc4-20191025
Build:Oct 25 2019
rst:0xc (RTC_SW_CPU_RST),boot:0x8 (SPI_FAST_FLASH_BOOT)
Saved PC:0x400260ec
SPIWP:0xee
mode:DOUT, clock div:1
load:0x3ffe6100,len:0x7c
load:0x4004c000,len:0x7e4
load:0x40050000,len:0x2598
entry 0x4004c15c

Current counter value: 8
Restarting in 10 seconds...

ESP-ROM:esp32s2-rc4-20191025
Build:Oct 25 2019
rst:0xc (RTC_SW_CPU_RST),boot:0x8 (SPI_FAST_FLASH_BOOT)
Saved PC:0x400260ec
SPIWP:0xee
mode:DOUT, clock div:1
load:0x3ffe6100,len:0x7c
load:0x4004c000,len:0x7e4
load:0x40050000,len:0x2598
entry 0x4004c15c

Current counter value: 9
Restarting in 10 seconds...

ESP-ROM:esp32s2-rc4-20191025
Build:Oct 25 2019
rst:0xc (RTC_SW_CPU_RST),boot:0x8 (SPI_FAST_FLASH_BOOT)
Saved PC:0x400260ec
SPIWP:0xee
mode:DOUT, clock div:1
load:0x3ffe6100,len:0x7c
load:0x4004c000,len:0x7e4
load:0x40050000,len:0x2598
entry 0x4004c15c

Current counter value: 10

Compiled with Platformio

@Kevin-CEN
Copy link
Author

@Jason2866 Yours seems to be all right.Can I have a look at your platformio.ini file?

@VojtechBartoska VojtechBartoska added Status: Test needed Issue needs testing and removed Status: Test needed Issue needs testing labels Sep 3, 2021
@Jason2866
Copy link
Collaborator

Jason2866 commented Sep 3, 2021

@Kevin-CEN, reused the setup from the Project Tasmota (i am a member).
The relevant part is this https://github.com/arendst/Tasmota/blob/development/platformio_tasmota_cenv_sample.ini#L47-L60
We do build our own SDK libs since we need some changed settings in IDF (sdkconfig)

@Kevin-CEN
Copy link
Author

@Jason2866 Thank you very much.But this configuration file of yours seems too complicated, I just tried it, it will compile wrong.
My project is very simple, I just hope THAT ESP32S2 can normally use the Preferences library in the Arduino framework, is there any other way I can set it up?

@Jason2866
Copy link
Collaborator

Jason2866 commented Sep 3, 2021

@Kevin-CEN Okay, simplified :-)

[env:esp32-s2-saola-1]
platform = https://github.com/platformio/platform-espressif32.git#feature/arduino-idf-master
platform_packages = tasmota/framework-arduinoespressif32 @ 2.0.0+tasmota
board = esp32-s2-saola-1
framework = arduino

main.cpp

/*
 ESP32 startup counter example with Preferences library.

 This simple example demonstrates using the Preferences library to store how many times the ESP32 module has booted. 
 The Preferences library is a wrapper around the Non-volatile storage on ESP32 processor.

 created for arduino-esp32 09 Feb 2017 by Martin Sloup (Arcao)

 Complete project details at https://RandomNerdTutorials.com/esp32-save-data-permanently-preferences/
*/

#include <Arduino.h>
#include <Preferences.h>

Preferences preferences;

void setup() {
  Serial.begin(115200);
  Serial.println();

  // Open Preferences with my-app namespace. Each application module, library, etc
  // has to use a namespace name to prevent key name collisions. We will open storage in
  // RW-mode (second parameter has to be false).
  // Note: Namespace name is limited to 15 chars.
  preferences.begin("my-app", false);

  // Remove all preferences under the opened namespace
  //preferences.clear();

  // Or remove the counter key only
  //preferences.remove("counter");

  // Get the counter value, if the key does not exist, return a default value of 0
  // Note: Key name is limited to 15 chars.
  unsigned int counter = preferences.getUInt("counter", 0);

  // Increase counter by 1
  counter++;

  // Print the counter to Serial Monitor
  Serial.printf("Current counter value: %u\n", counter);

  // Store the counter to the Preferences
  preferences.putUInt("counter", counter);

  // Close the Preferences
  preferences.end();

  // Wait 10 seconds
  Serial.println("Restarting in 10 seconds...");
  delay(10000);

  // Restart ESP
  ESP.restart();
}

void loop() {

}

@yangminglong
Copy link

Can you explain what went wrong with platform IO?

@Jason2866
Copy link
Collaborator

Using a old setup/version of Arduino results in non working sketch for S2.

@yangminglong
Copy link

Using a old setup/version of Arduino results in non working sketch for S2.

Thank you very much. According to your instructions, I uninstalled platformIO from vscode and deleted .platformio folder. After that, I reinstalled platformIO in vscode. The following configuration is used to solve the problem that preferences cannot be saved.

[env:esp32s2]
platform = https://github.com/tasmota/platform-espressif32/releases/download/v2.0.2idf/platform-espressif32-2.0.2.zip
board = esp32-s2-saola-1
framework = arduino

@VojtechBartoska VojtechBartoska added the IDE: PlaformIO Issue relates to PlatformIO IDE label Jan 4, 2022
@VojtechBartoska
Copy link
Contributor

@Kevin-CEN Can I consider this as solved?

@VojtechBartoska
Copy link
Contributor

Closing, no asnwer.

@VojtechBartoska VojtechBartoska added the Resolution: Expired More info wasn't provided label Apr 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IDE: PlaformIO Issue relates to PlatformIO IDE Resolution: Expired More info wasn't provided
Projects
None yet
Development

No branches or pull requests

6 participants