Search code examples
luaesp8266pwm

Keep the memory to last value of the variable


I am using ESP8266 and i am programming it with Lua language. I created a PWM function and i created a server in ESP8266. I can change PWM duty cycle through this server(You can see figure). First i set to PWM with a default value and i am changing the PWM through server with respect to my application. But when i disconnected the ESP8266 from power and connected again to power, duty cycle returned to default value. I want to save last PWM value that i entered through server and when i disconnected the ESP8266 from power and connected again to power, duty cycle set the PWM value to last value i entered through server. For example on the below i set the default PWM value to '512', when i changed the PWM with respect to my application, i saved the last value and after the reconnecting process(i informed above) i took the last PWM value from memory and PWM duty cycle starts with this value. Is there any permanent memory like EEPROM i can use to save last PWM value or any other solution anyone suggest? (i have to use Lua language)

PWM adjusting server

--default PWM value 
pwm.setup(2,1000,512);
pwm.start(2);

Solution

  • If you need to persist data so that it survives power cycling then you have to write it to flash memory through the SPIFFS file system. File write example from the docs:

    -- open 'init.lua' in 'a+' mode
    file.open("init.lua", "a+")
    -- write 'foo bar' to the end of the file
    file.write('foo bar')
    file.close()
    

    If you need something less permanent then you can use the rtcmem module which provides access to RTC memory. The data held there will at least survive deep sleeps.