How can a variable value be saved and retrieved from nonvolatile memory?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
brett
Posts: 14
Joined: Sun Jan 15, 2017 3:17 am

How can a variable value be saved and retrieved from nonvolatile memory?

Post by brett » Fri Oct 06, 2017 12:55 am

I have the following program running, but I would like the value of the trackerCounter variable to be saved when the power is lost. I found this for the microBit but I'm not sure if it would work on the esp8266 or how to best implement it. https://support.microbit.org/support/so ... atile-data

Do you guys have any suggestions? I'm also sure that the code can be cleaned up some. If you want to make suggestions on that I would be interested in hearing what you have to say too. I'm still very much a beginner.

Code: Select all

import machine
import time
import tm1637
from machine import Pin

remote = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_UP)
response = machine.Pin(13, machine.Pin.IN, machine.Pin.PULL_UP)
reset = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_UP)
led = machine.Pin(2, machine.Pin.OUT)
beeper = machine.Pin(0, machine.Pin.OUT)
tm = tm1637.TM1637(clk=Pin(4), dio=Pin(5))

buttonCounter = 0
trackerCounter = 0

while True:
    remotefirst = remote.value()
    responsefirst = response.value()
    resetfirst = reset.value()
    time.sleep(0.01)
    remotesecond = remote.value()
    responsesecond = response.value()
    resetsecond = reset.value()
    if remotefirst and not remotesecond and buttonCounter != 0:
        print('beeper')
        beeper(1)
        time.sleep(0.1)
        beeper(0)
        buttonCounter = buttonCounter + 1
        print('buttonCounter =', buttonCounter)
        trackerCounter = trackerCounter + 1
        print('trackerCounter =', trackerCounter)
        tm.number(trackerCounter)
    if remotefirst and not remotesecond and buttonCounter == 0:
        print('Remote button pressed')
        led(1)
        buttonCounter = buttonCounter + 1
        print('buttonCounter =', buttonCounter)
        trackerCounter = trackerCounter + 1
        print('trackerCounter =', trackerCounter)
        tm.number(trackerCounter)
    if responsefirst and not responsesecond:
        print('Response')
        led(0)
        buttonCounter = 0
        print('buttonCounter =', buttonCounter)
        print('trackerCounter =', trackerCounter)
        tm.number(trackerCounter)
    if resetfirst and not resetsecond:
        print('Reset')
        led(0)
        buttonCounter = 0
        print('buttonCounter =', buttonCounter)
        trackerCounter = 0
        print('trackerCounter =', trackerCounter)
        tm.number(trackerCounter)

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

Re: How can a variable value be saved and retrieved from nonvolatile memory?

Post by pythoncoder » Fri Oct 06, 2017 5:55 am

The approach is to use a file as detailed in the reference you quote. The ESP8266 has a filesystem which exists in the device's Flash memory, so if you write data to a file and close the file the data will be retained if the power goes down.

This is a general Python programming topic rather than one specific to a particular hardware device. You might want to read up on file I/O and learn how to use it on a PC in standard Python 3 before trying it on the ESP8266.
Peter Hinch
Index to my micropython libraries.

brett
Posts: 14
Joined: Sun Jan 15, 2017 3:17 am

Re: How can a variable value be saved and retrieved from nonvolatile memory?

Post by brett » Sat Oct 07, 2017 11:40 pm

Thanks for the response! I got it to work using that link. I'm not sure I should have responded since it's in the wrong forum but I wanted thank you for all your activity on these forums.

Post Reply