Page 1 of 1

ESP32, eeprom/nvs

Posted: Sun Feb 23, 2020 11:04 am
by der_kps
Hello,
I'm using ESP32 with uPython and would like to store some variables in a non-volatile storage.
There is not really an eeprom but Arduino libraries emulate EEPROM using a sector (4 kilobytes) of flash memory.
Is there any similar solution in python available?

Regards

Re: ESP32, eeprom/nvs

Posted: Sun Feb 23, 2020 11:13 am
by pythoncoder
MicroPython on ESP32 supports a filesystem so you can use normal Python file I/O for nonvolatile storage.

Re: ESP32, eeprom/nvs

Posted: Sun Feb 23, 2020 11:26 am
by der_kps
Thank's for the answer!
Do you have a link with example code to write/read 'my_var' in a nvs adress area?

Re: ESP32, eeprom/nvs

Posted: Sun Feb 23, 2020 11:46 pm
by jimmo
The NVS on ESP32 is like a mini-filesystem. They partition the flash, and the NVS partition implements the bare minimum to support read/write of key/value pairs.

As Peter said, on MicroPython, there isn't really the same need for NVS as we already have a full filesystem which uses up the full flash.

So just regular Python code to read/write data into a file is sufficient. The simplest option is just to use json

Writing:

Code: Select all

import json
config = {
  'name': 'value',
  ...
}
with f = open('config.json', 'w'):
  json.dump(config, f)
Reading:

Code: Select all

import json
with f = open('config.json', 'r'):
  config = json.load(f)
  print(config['name'])
The other good option is to write it as a Python file that can be just imported directly, but I think the JSON option works well in the general case.

Re: ESP32, eeprom/nvs

Posted: Tue Feb 25, 2020 1:20 pm
by der_kps
Thanks for your json-code!
I wrote in my uPyCraft this code and pleased ;) ESP32 to execute it

import json
config = {
'my_var': 'my_val',
}
with f = open('config.json', 'w'):
json.dump(config, f)
with f = open('config.json', 'r'):
config = json.load(f)
print(config['my_var'])

A syntax error occours: the equal-sign f = open
I would like to understand the benefit of using json.
What's going wrong?

Re: ESP32, eeprom/nvs

Posted: Tue Feb 25, 2020 10:51 pm
by jimmo
Oh wow...sorry, somehow even after 12 years of writing Python my brain refuses write the correct syntax of the "with" statement (or the "except", but that's another story).

Anyway, what I should have written! (sorry!)

Code: Select all

import json
config = {
  'name': 'value',
  ...
}
with open('config.json', 'w') as f:
  json.dump(config, f)

Code: Select all

import json
with open('config.json', 'r') as f:
  config = json.load(f)
  print(config['name'])

Re: ESP32, eeprom/nvs

Posted: Fri Feb 28, 2020 3:25 pm
by der_kps
Hello again!
Meanwhile I've solved my problem using the filesystem peter told about.
It looks like the well known ini.file in pc-programming. :lol:

I think I've a problem what happens or should happen with json.
To store the password 1234 in a file I type the 1st line
config = {'pwd': '1234'}
The next line creates a file named config.json (or config.txt or pwd.txt ...)
An AttributeError is caused by json.dump(config, f) : object has no attribute 'dump'
Looking in the existing config.json, it's empty.

Trying to read an existing file pwd_f (made by filesystem) instead of config.json I get: object has no attribute 'load'.

As I said, what's going on? :?:

Re: ESP32, eeprom/nvs

Posted: Fri Feb 28, 2020 9:12 pm
by jimmo
It sounds like you've replaced the json import? Maybe with a variable named json or something?

Can you try doing this at the REPL:

>>> import json
>>> json.dump

(you should get a function)

Re: ESP32, eeprom/nvs

Posted: Sat Feb 29, 2020 7:06 pm
by der_kps
Hello,
the script I' ve posted on Sat Feb 22, 2020 7:30 pm works today :D
Don't know why, but it works and will be a much better solution to store/read 5 variables by explicit name than I did with the filesystem.

Thanks for your help and patience!

Regards