ESP32, eeprom/nvs

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
der_kps
Posts: 20
Joined: Sat Feb 22, 2020 6:30 pm

ESP32, eeprom/nvs

Post by der_kps » Sun Feb 23, 2020 11:04 am

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

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: ESP32, eeprom/nvs

Post by pythoncoder » Sun Feb 23, 2020 11:13 am

MicroPython on ESP32 supports a filesystem so you can use normal Python file I/O for nonvolatile storage.
Peter Hinch
Index to my micropython libraries.

der_kps
Posts: 20
Joined: Sat Feb 22, 2020 6:30 pm

Re: ESP32, eeprom/nvs

Post by der_kps » Sun Feb 23, 2020 11:26 am

Thank's for the answer!
Do you have a link with example code to write/read 'my_var' in a nvs adress area?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: ESP32, eeprom/nvs

Post by jimmo » Sun Feb 23, 2020 11:46 pm

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.

der_kps
Posts: 20
Joined: Sat Feb 22, 2020 6:30 pm

Re: ESP32, eeprom/nvs

Post by der_kps » Tue Feb 25, 2020 1:20 pm

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?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: ESP32, eeprom/nvs

Post by jimmo » Tue Feb 25, 2020 10:51 pm

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'])

der_kps
Posts: 20
Joined: Sat Feb 22, 2020 6:30 pm

Re: ESP32, eeprom/nvs

Post by der_kps » Fri Feb 28, 2020 3:25 pm

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? :?:

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: ESP32, eeprom/nvs

Post by jimmo » Fri Feb 28, 2020 9:12 pm

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)

der_kps
Posts: 20
Joined: Sat Feb 22, 2020 6:30 pm

Re: ESP32, eeprom/nvs

Post by der_kps » Sat Feb 29, 2020 7:06 pm

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

Post Reply