ChipChop Support Forum
Log in
Log out
Join the forum
My Details
REPLIES: 3
VIEWS: 83
BACK
REPLY TO THIS POST
HansaG
23 Nov 2024
Last state of a component.
Hi there,

How can I retrive the last state of a component (like a toggle switch) after a reboot?

Thanks in advance.
Gizmo
23 Nov 2024
ACCEPTED

Hi Hansa,

I am not sure what exactly do you mean? Do you mean to retrieve the last state from ChipChop when the device wakes up?

I haven't been asked for that before but I guess it can be done as there is already in the Actions the option to "forward" the entire status of one device to another (Send copy of trigger status) but I have to have a think how I could do that for the same device (I can't be echoing the entire status on every heartbeat, that would be unreasonable amount of data traffic and also would slow down the response times)

Anyway, for what you need if I understand you correctly that wouldn't be practical as you will have a big delay waiting for the initial connection to be established, handshakes etc. There is a much better and quicker mechanism already in place :-)

If I remember correctly, you use ESP boards, right?

Option 1

All you need to do is to use the Code Builder to generate a project template and include the Auto Mode (pin control) plugin, it controls automatically whatever digital output pin you assign to it.

The Auto Mode automatically uses the Preferences Manager plugin to save the state in the ESP's flash of the pin it's controlling on every change of state and can automatically restore the state on the device restart.
In the Auto Mode settings where you specify the pin that you want to be controlled under "mode" just select "Restore on restart" and that's it.
Note: if you use the Auto Mode you can still intercept changes through the ChipChop_onCommandReceived and do extra logic just don't need to call digitalWrite or ChipChop.updateStatus(), there is also a hook into the plugin's events but I'd have to explain that in a different post how to use it.

I use that a lot in my house on the electric radiators. Occasionally we have a short power cuts which kills the power for a fraction of a second so when the ESPs controlling the radiators restart they check if the heating was on and immediately restore the heating so we don't freeze our asses.
That all happens even before they were connected to ChipChop



Option 2

Same thing use the Code Builder to generate a starting project template but just include the Preferences Manager, then you can manually any time the pin changes state use: PrefsManager.save(some_group_name, component_name, value); and to retrieve on restart PrefsManager.get(group_name, component_name);

Simplified example:



int switch1_pin = 2;
String switch1_state = "OFF";

    void ChipChop_onCommandReceived(String component, value, String command_source, int command_age){
        if(component == "switch1"){
            if(value == "ON"){

                digitalWrite(switch_pin, LOW);
                switch_state = "ON";
                
                PrefsManager.save("switches","switch1","ON");
            }else{

                digitalWrite(switch_pin, HIGH);
                switch_state = "OFF";
                PrefsManager.save("switches","switch1","OFF");
            }

            // confirm that the state has changed on the device so you can see it happening in the App or Dev Console
            ChipChop.updateStatus("switch1",value);
        }

    }

    void setup(){

        ... blah blah, wifi setup etc

        
        String last_switch1_status = PrefsManager.get("switches","switch1"); // safest option is to use strings with the saved data
        
        if(last_switch1_status == ""){ // this is probably the first time ever that this device has started and there is nothing saved for it
            last_switch1_status = "OFF";
            switch1_state = "ON"; // just a little faking here so the code below executes in this situation
        }

        // we are going to be super lazy here and simply reuse the ChipChop function above to do all the donkey work instead of repeating all the if statements but only if the state has changed
        // effectively it's same thing as if we have requested the last status from ChipChop but without waiting for the connection, it all happens internally on the ESP

        if(last_switch1_status != switch1_state){
    
            ChipChop_onCommandReceived("switch1", last_switch1_status, "", 0);

        }
        
    }




Option 3

Similar method but use manually LittleFS, EEPROM or something like the preferences.h library so save the component state into the flash and retrieve it on restart... fiy, the ChipChop Preferences Manager does a similar job to preferences.h library but it's a bit smaller and maybe simpler to use, also quite handy to record logs etc



Let me know what you think (sorry if I misunderstood what you are asking) I'm always open to suggestions if it's something else you had in mind


G



Attached images
HansaG
23 Nov 2024

Thank you so much.!
You answered my question in detail.
I think I would that the option 3 route, as I had the same idea of saving it in the memory and get it back, but wanted get your opinion first.
So thanks again. was realy helpful.
Gizmo
23 Nov 2024

pfewww...I'm off the hook, don't need to do anything :-)))

Defo it's best to do it on-device especially it you deal with something mission critical like door locks, alarms etc