Value of variable lost after device restart

I have an ON/OFF button on the dashboard. It gets switched-OFF anytime the device is restarted or re-connect . Any advice is appreciated. The code is below:

#include <ThingerSmartConfig.h>
#include "arduino_secrets.h"

int Alarme ;

ThingerSmartConfig thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

void setup() {
  // open serial for debugging
  Serial.begin(115200);

thing["Alarme"] << [](pson & in) {
     if(in.is_empty()){
        in = Alarme;
    }
    else {
        Alarme= in ;
    }
  };
}
void loop() {
  thing.handle();
  Serial.print(Alarme);
}

Hi,

What I would do is to store the status into a device’s property, set a routine to store it after each change and make a routine so the device reads it when it restarts.

Here is the documentation related → DEVICES ADMINISTRATION - Thinger.io Documentation.

Let us know how it goes.

Hi Ega,
Thank you for your advice. I just implemented that as:

// Read the value of Alarme from non-volatile memory
  EEPROM.begin(sizeof(Alarme));
  EEPROM.get(0, Alarme);
  EEPROM.end();
  
thing["Alarme"] << [](pson & in) {
     if(in.is_empty()){
        in = Alarme;
    }
    else{
      Alarme= in ;
      EEPROM.begin(sizeof(Alarme));
      EEPROM.put(0, Alarme);
      EEPROM.commit();
      EEPROM.end();
    }
      Serial.println("Alarme: ");
      Serial.println(Alarme);
  };

In the serial monitor I can see that during execution of setup() the value of Alarme is read correctly from the eeprom however the ON/OFF widget in the dashboard does not update.
To try to force update the ON/OFF widget I tried this test:

thing["Alarme"] << [](pson & in) {
     if(in.is_empty()){
        in = HIGH;
    }
    else {
        Alarme= in ;
    }
  };

but it doesn’t work.

Hi,

The eeprom memory is an option too, but note that it has a limited writing cycles, so I recommend to take it in account about how often it will be written according the running process.

I would recommend you to keep things separated, a good practice is to have different functions to make specific tasks and set conditions to invoke them when neccesary, for example, at “Alarme” resource I would not have the whole eeprom routine but have that defined as a function and invoke that from that routine.

The dashboard widget does not update on real time, you need to refresh the webpage to make it request the state from the device.

Hope this helps.

1 Like