Skip to content

Preferences doesnt work on esp32-c6 #11395

Closed
@josef2600

Description

@josef2600

Board

esp32-c6

Device Description

esp32-c6 dev

Hardware Configuration

Image
or any other partition setting with spiffs. with or without zigbee.

Version

v3.2.0

IDE Name

Arduino IDE

Operating System

win 10

Flash frequency

80Mhz

PSRAM enabled

no

Upload speed

256000

Description

the Preferences doesn't save or load anything. i tested on many partition scheme, including zigbee. all are with spiffs. no matter what partition is selected or any zigbee is enabled or not (in both zigbee config).
as you can see in the code, i even save something in it inside setup, but nothing is saved, no matter how many times i restart it! and i do erase flash in programming.
the Arduino IDE version is irrelevant. i tested with old and newest one. no changes.

Sketch

#include <Arduino.h>
#include <string.h>
#include <algorithm>
#include <cstring>
#include <esp_err.h>
#include <esp_log.h>
#include <esp_mac.h>
#include <esp_system.h>
#include <freertos/FreeRTOS.h>
#include <freertos/event_groups.h>
#include <freertos/queue.h>
#include <freertos/task.h>
#include <nvs.h>
#include <nvs_flash.h>
#include <driver/uart.h>
#include <esp_attr.h>
#include <esp_check.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <esp_zigbee_core.h>
#include <ha/esp_zigbee_ha_standard.h>
#include <hal/ieee802154_ll.h>
#include <Preferences.h>  // For storing settings in flash

 Preferences my_preferences;

void setup()
{
  Serial.begin(115200);
  vTaskDelay(pdMS_TO_TICKS(500));
  //nvs_flash_init();
  //Ieee802154 my_ieee802154({.channel = my_frq_channel, .pan_id = my_pan_id}, onMessageCallback);
  vTaskDelay(pdMS_TO_TICKS(200));
  
  esp_err_t ret = nvs_flash_init();
  if (ret != ESP_OK)
  {
    nvs_flash_erase();
    nvs_flash_init();
    ESP_LOGI(DEVICE_NAME, "setup: nvs flash didnt intialized", 0);
  }  
  
  
  my_task_discovery_delay = 10;
  
  // Read saved configuration from flash using Preferences
  
  delay(10);
  my_preferences.begin(my_zigbee_set_per, false);
  
  schLennn = my_preferences.getBytesLength("me_pan_id");
  if (schLennn > 0)
  {
    my_pan_id = my_preferences.getUShort("me_pan_id", 0);
    if (my_pan_id == 0 || my_pan_id == 0xffff)
    {
      //my_preferences.end();
      ESP_LOGI(DEVICE_NAME, "setup: Bad PAN ID loaded: 0x%04X .trying to load the original", my_pan_id);
      my_pan_id = my_default_pan_id;
      delay(10);
      //my_preferences.begin(my_zigbee_set_per, false);
      my_preferences.putUShort("me_pan_id", my_pan_id);
      //my_preferences.end();
      delay(10);
      //my_preferences.begin(my_zigbee_set_per, true);
    }
    ESP_LOGI(DEVICE_NAME, "setup: New PAN ID loaded: 0x%04X", my_pan_id);
  }
  else
  {
    my_preferences.putUShort("me_pan_id", my_default_pan_id);
  }
  my_preferences.putUChar("me_frq_channel", my_frq_channel);
  schLennn = my_preferences.getBytesLength("me_frq_channel");
  if (schLennn > 0)
  {
    my_frq_channel = my_preferences.getUChar("me_frq_channel", 0);
    if (my_frq_channel == 0 || my_frq_channel > 26)
    {
      //my_preferences.end();
      ESP_LOGI(DEVICE_NAME, "setup: Wrong Frequency Channel loaded: %u .trying to load the original", my_frq_channel);
      my_frq_channel = my_default_frq_channel;
      delay(10);
      //my_preferences.begin(my_zigbee_set_per, false);
      my_preferences.putUChar("me_frq_channel", my_frq_channel);
      //my_preferences.end();
      delay(10);
      //my_preferences.begin(my_zigbee_set_per, true);
    }
    ESP_LOGI(DEVICE_NAME, "setup: New Frequency Channel loaded: %u", my_frq_channel);
  }
  else
  {
    my_preferences.putUChar("me_frq_channel", my_frq_channel);
  }
  
  delay(10);
  my_preferences.end();
  delay(10);
  
}

Debug Message

[   718][E][Preferences.cpp:526] getBytesLength(): nvs_get_blob len fail: me_pan_id NOT_FOUND
[   726][E][Preferences.cpp:526] getBytesLength(): nvs_get_blob len fail: me_frq_channel NOT_FOUND

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions