ESP32, eeprom/nvs
ESP32, eeprom/nvs
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
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
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: ESP32, eeprom/nvs
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.
Index to my micropython libraries.
Re: ESP32, eeprom/nvs
Thank's for the answer!
Do you have a link with example code to write/read 'my_var' in a nvs adress area?
Do you have a link with example code to write/read 'my_var' in a nvs adress area?
Re: ESP32, eeprom/nvs
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:
Reading:
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.
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)
Code: Select all
import json
with f = open('config.json', 'r'):
config = json.load(f)
print(config['name'])
Re: ESP32, eeprom/nvs
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?
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
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!)
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
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.
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?
Meanwhile I've solved my problem using the filesystem peter told about.
It looks like the well known ini.file in pc-programming.
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
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)
Can you try doing this at the REPL:
>>> import json
>>> json.dump
(you should get a function)
Re: ESP32, eeprom/nvs
Hello,
the script I' ve posted on Sat Feb 22, 2020 7:30 pm works today
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
the script I' ve posted on Sat Feb 22, 2020 7:30 pm works today
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