Doubt with variable saving.
I have a question about how to save multiple variables. I need to save variables in certain parts of the code, right now first I declare all variables with state.registerVar( &variable1); .Then I was trying to load all the variables at the beginning of the code with state.loadFromRTC() and at the end of the code save them with state.saveToRTC() but it does not work.
Do I need to run the state.saveToRTC() every time I modify one of the variables I want to save?
Code (each time the system detects that the CO2 is above the maximum, the system adds +1 to each loop, when it reaches 75, it sends a notification. But I can't get it to save the value and add +1 to the saved value.):
state.registerVar( &indice_notificaciones_co2 );
state.registerVar( &indice_notificaciones_temp );
state.registerVar( &indice_notificaciones_hum );
state.registerVar( &max_co2 );
if (state.loadFromRTC()) { // we load the values from rtc memory back into the registered variables
contador_mediciones++;
state.saveToRTC();
} else {
contador_mediciones = 0; // cold boot part
indice_notificaciones_co2 = 0;
indice_notificaciones_temp = 0;
indice_notificaciones_hum = 0;
max_co2 = 800;
state.saveToRTC();
}
if (CO2>=max_co2){ //NOTIFICATIONS
if( indice_notificaciones_co2 >= 75){//counter 10 min 5seg*120 = 10 min
notificacion_co2 = "true"; //send notification
indice_notificaciones_co2 = 0; //reset index when notification is sent
state.saveToRTC();
}
indice_notificaciones_co2++; //cpunter +1 time 10 min
state.saveToRTC();
}else{ //restablecer indice cuando co2 < 800
indice_notificaciones_co2 = 0;//NOTIFICAIONES
state.saveToRTC();
}
I am sorry to answer this late. I didn't take any notice of issues generated here.
But to answer your question directly: yes, each time you change your variables, you need to call state.saveToRTC(). This is by design because you might have unstable variable setups, aka transactions going on. Deciding manually when it is a valid state at all to restart with is a good idea as it gives you full control and makes you think of when your data is actually in a state you can restart with.