Save variable to file

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
mbinno
 
Posts: 8
Joined: Fri Feb 17, 2023 3:48 pm

Save variable to file

Post by mbinno »

Hi

I'm searching for a solution to save a variable and have access after repower the device. Is this possible?

The Eprom solution seems not the best... So I search for another solution.

I found this guide: https://learn.adafruit.com/cpu-temperat ... filesystem

But I still don't know If I can read the saved values.

Can someone help?

Many thanks in advance

Michael

User avatar
millercommamatt
 
Posts: 831
Joined: Tue Jul 31, 2018 4:57 pm

Re: Save variable to file

Post by millercommamatt »

What device?
How much data and what format?
What have you already tried and what about that didn't suit your needs?

User avatar
sj_remington
 
Posts: 997
Joined: Mon Jul 27, 2020 4:51 pm

Re: Save variable to file

Post by sj_remington »

What MCU and programming language are you using?

If the MCU has EEPROM, that is very convenient, very reliable and easy to use with the Arduino IDE.

User avatar
blakebr
 
Posts: 957
Joined: Tue Apr 17, 2012 6:23 pm

Re: Save variable to file

Post by blakebr »

SJ,

If the problem with EEPROM is speed or the 10,000 or 100,000 read/write limit, check out the FRAM storage chips. No delay while waiting for write and 10,000,000,000 read/write cycles. I have one under test and after 2,325,000 read/write cycles, no problems.

Bruce

User avatar
mbinno
 
Posts: 8
Joined: Fri Feb 17, 2023 3:48 pm

Re: Save variable to file

Post by mbinno »

to all, thanks a lot

I'm using the Adafruit ESP32-S2 Reverse TFT Feather with circuitpython.

I already try to save some text according to the linked guide to a file on the Feather drive. But it doesn't work...Not sure, but I think the guide seems to be pretty old... And of course, I'm not a code monster :-)

Regarding the data, I have only store one int value, but a high number of times (around 1'000'000 times, every 0.5s to 1s). That's the reason why I believe that the Eprom solution will not work, at least not for this high number of cycles.

Yes,the FRAM storage chips could work for me. But this means, I need to attach a additional device. If it's possible to save the int value on the existing feather, I definitely prefer this.

User avatar
mbinno
 
Posts: 8
Joined: Fri Feb 17, 2023 3:48 pm

Re: Save variable to file

Post by mbinno »

Regarding the save to file solution, I try this:

boot.py:

import board
import digitalio
import storage

switch = digitalio.DigitalInOut(board.D13)
switch.direction = digitalio.Direction.INPUT
switch.pull = digitalio.Pull.UP

# If the D13 is connected to ground with a wire
# CircuitPython can write to the drive
storage.remount("/", switch.value)


code.py:

import time
import board
import digitalio
import storage
led = digitalio.DigitalInOut(board.LED)
led.switch_to_output()

var_test = 0

while True:
var_test = var_test + 1
str_var_test = str(var_test)
time.sleep(0.5)
print(var_test)
try:
print("in try")
with open("var_test.txt", "a") as fp:
while True:
fp.write(str_var_test)
fp.write("\n")
print("write to file")
time.sleep(0.5)

except OSError as e:
print("os error")
delay = 0.5
if e.args[0] == 28:
delay = 0.25

else:
pass

I'm able to allow circuitpython with the D13 pin to save to the drive. This should work. It also creates a text file var_test.txt but it remains empty.

User avatar
blakebr
 
Posts: 957
Joined: Tue Apr 17, 2012 6:23 pm

Re: Save variable to file

Post by blakebr »

mbinno,

The EEPROM, Flash memory on the PCB and MCU Flash memory all have write thresholds less tan the one million writes you want to do.
Regarding the data, I have only store one int value, but a high number of times (around 1'000'000 times, every 0.5s to 1s). That's the reason why I believe that the Eprom solution will not work, at least not for this high number of cycles.
I would suggest a test run to see how long the above three long term memory types last. Also I don't think any MCU can manage one million writes in less than one second. But I could be wrong, ask my wife. ;-)

Bruce

User avatar
mbinno
 
Posts: 8
Joined: Fri Feb 17, 2023 3:48 pm

Re: Save variable to file

Post by mbinno »

@ Bruce I ordered two FRAM memories for testing. It's not planned to save 1'000'000 writes in less than one second. It's planned to save around 1'000'000 writes every 1s. ;-)

Is it possible to save a integer with his max. length directly to the FRAM? I can't find anything in the documentation...

User avatar
blakebr
 
Posts: 957
Joined: Tue Apr 17, 2012 6:23 pm

Re: Save variable to file

Post by blakebr »

mbinno,

The FRAM library only saves bytes. I had to implement a 4 byte number storage function in CircuitPython. This lets me go to 2_147_483_648 before it rolls over. I am sure the Admins here have a more elegant way.

Bruce

Code: Select all

int_size     = 4         # Bytes
Loops        = 0x98+0x00 # 152/0x98 # FRAM memory locations
Starts       = 0x98+0x04 # 156/0x9c # FRAM 4 bytes per intiger number
this_run     = 0x98+0x08 # 160/0xa0 #
next_num_0c  = 0x98+0x0c # 164/0xa4 #
next_num_10  = 0x98+0x10 # 168/0xa8 #
GROVE_4 = (board.GP7, board.GP6) # My I2C port
FRAM50 = adafruit_fram.FRAM_I2C(GROVE_4], write_protect=False, wp_pin=None)#, address=0x50)

Code: Select all

####
def read_FRAM(adr): # 4 Byte Intiger
  #print(adr)
  if(adr % int_size): return(None)
  num = 0
  for i in range(int_size):
    b = 3 - i
    num *= 0x100
    num += (FRAM50[b+adr][0])
    #print(i, FRAM50[b+adr][0])
  return(num)
####
def write_FRAM(adr, val): # 4 Byte Intiger
  #print(adr,val)
  if(adr % int_size): return(None)
  for i in range(int_size):
    num = val & 0xFF
    #print(i, num)
    FRAM50[i+adr] = num
    val = int(val/0x100)
  return(num)
####
def inc_FRAM(adr):
  val = read_FRAM(adr)
  val += 1
  write_FRAM(adr, val)
  return(read_FRAM(adr))
####

Locked
Please be positive and constructive with your questions and comments.

Return to “Feather - Adafruit's lightweight platform”