persistence, retain last state on stand alone application

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
puppetmaster886
New user
Posts: 5
Joined: 14 Jul 2018, 00:11

persistence, retain last state on stand alone application

#1 Post by puppetmaster886 » 15 Aug 2018, 16:11

Hi, I am working on a standalone thermostat.
The user has 2 buttons to set the temperature (it is stored on a dummy device), and then it will control a heater to regulate the temperature.

How can I store the last temperature set by the user to retain it after power loss? there is any way to store a value on the internal flash?

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: persistence, retain last state on stand alone application

#2 Post by TD-er » 15 Aug 2018, 22:29

Not at the moment.
The only thing that somewhat is stored, are the plugin output values. Those are stored in RTC memory, which will survive a crash or warm reboot.
As soon as the power is lost, even those are gone.

But I guess having some plugin to store values, would be a nice addition.

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: persistence, retain last state on stand alone application

#3 Post by enesbcs » 16 Aug 2018, 11:12

TD-er wrote: 15 Aug 2018, 22:29 The only thing that somewhat is stored, are the plugin output values. Those are stored in RTC memory, which will survive a crash or warm reboot.
As soon as the power is lost, even those are gone.

But I guess having some plugin to store values, would be a nice addition.
I am currently working on an integrated thermostat plugin with OLED an 3 button.
https://github.com/enesbcs/ESPEasyPlugi ... rmOLED.ino
Hardware part is ready, testing is in progress now.
Image

Are there any simple example code, how can i store values to flash? I think it can be stored when the setpoint changes, ideally it will not be too frequent.


User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: persistence, retain last state on stand alone application

#5 Post by grovkillen » 16 Aug 2018, 20:30

And I know that membrane button sticker ;)
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: persistence, retain last state on stand alone application

#6 Post by TD-er » 16 Aug 2018, 20:34

enesbcs wrote: 16 Aug 2018, 11:12 [...]

Are there any simple example code, how can i store values to flash? I think it can be stored when the setpoint changes, ideally it will not be too frequent.
I've been thinking about this. I guess we could add some thing to the dummy plugin, to set some numeric value, which will be active when the plugin runs its init procedure (at boot, when applying settings, etc)
Then add some command to set these values from rules and add some routine to check every N seconds to see if it has changed and save the settings of that plugin. (or add a command to write it to the settings)
So a command would then need:
- Plugin Index
- Preset value index
- new value.

This value should then have the same range as the normal Dummy.

Only caveat will be what to do when the new preset differs from the current set output value?

Another option could be to make some command to "copy current values to preset values of dummy with idx N"

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: persistence, retain last state on stand alone application

#7 Post by enesbcs » 16 Aug 2018, 22:23

I'm thinking about simple save when changed & restore on init from Spiffs to 4 plugin values. I am not sure how it works exactly, as i am not a real programmer, just a hobbist. :)

in PLUGIN_INIT:

Code: Select all

if (SPIFFS.exists("thermo.dat"))
  {
    fs::File f = SPIFFS.open("thermo.dat", "r");
    if (f)
    {
      f.readBytes( &UserVar+BaseVarIndex, (sizeof(UserVar[BaseVarIndex])*4) );
      f.close();
    }
  }
in PLUGIN_READ - when value change detected:

Code: Select all

    fs::File f = SPIFFS.open("thermo.dat", "w");
    if (f)
    {
      f.write( &UserVar+BaseVarIndex, (sizeof(UserVar[BaseVarIndex])*4) );
      f.close();
    }

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: persistence, retain last state on stand alone application

#8 Post by enesbcs » 17 Aug 2018, 22:45

I spent some time with it, but with the following modifications, it seems to work OK.

Code: Select all

f.read( ((uint8_t *)&UserVar[event->BaseVarIndex] + 0), 16 );

f.write( ((uint8_t *)&UserVar[event->BaseVarIndex] + 0), 16 );
flashCount() function is not used anymore?
Now the setpoint data and relay state survives power off&power on.

I am sure that beyond the nice DIN rail box and button sticker, the 1.3" OLED display is also familiar to everyone. :) Altough the 0.96" sized display sharper, i have to use a new font with 18 height, becaues smaller fonts simply seems very ugly on the 1.3" display. I like very much the P036 FrameOLED plugin, and many thanks for it, but i have to modify it very much to work as i planned. The most important thing to me to see the image of the flame when relay is active... :)

I planned the following features, which seems to work now:
- 3 button (with built in pull-up enabled): 1/ left=decrease setpoint in mode2, decrease time in mode3, 2/ right=increase setpoint in mode2, increase time in mode3, 3/ mode change button (mode1=off,mode2=auto-setpoint controlled,mode3=manual on for specific time in minutes)
- reports its state in mqtt
- can be controlled by commands, rules, url..
- remember it's status on power outage

Commands:
oledframedcmd is the same as in frameoled, as copied from there
thermo,setpoint,22 // set the setpoint to 22 celsius
thermo,setpoint,p0.5 // increase actual setpoint with 0.5 celsius
thermo,setpoint,m0.5 // decrease actual setpoint with 0.5 celsius
thermo,mode,0 // set relay permanently off
thermo,mode,1 // set relay on automatically if current temperature below setpoint
thermo,mode,2,5 // set relay on manually for 5 minutes
Attachments
IMG_20180817_214152_1.jpg
IMG_20180817_214152_1.jpg (84.76 KiB) Viewed 5993 times

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: persistence, retain last state on stand alone application

#9 Post by TD-er » 18 Aug 2018, 13:16

The flash count is still checked, but you'll have to call that counter function.
And writing like that can be tricky. I don't know how writes are handled at a lower level when writing less than a page on the flash chip.

I will also have a look at your changes for the OLED plugin, to see what you're missing in the current one :)

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: persistence, retain last state on stand alone application

#10 Post by enesbcs » 18 Aug 2018, 14:29

TD-er wrote: 18 Aug 2018, 13:16 I will also have a look at your changes for the OLED plugin, to see what you're missing in the current one :)
The FrameOLED plugin is perfect for an universal device. Perhaps a command for changing pages "scrolling", scroll to specified page or stop scrolling can be handy sometimes.

My modifications was created especially for thermostats, this is not something that can be used generally. ThermOLED is an integrated plugin. (3-in-1: display+buttons+relay)
Attachments
thermo_plugin_settings.jpg
thermo_plugin_settings.jpg (44.33 KiB) Viewed 5967 times

Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests