homieiot / homie-esp8266

💡 ESP8266 framework for Homie, a lightweight MQTT convention for the IoT
http://homieiot.github.io/homie-esp8266
MIT License
1.36k stars 308 forks source link

A way to store values between resets? #154

Closed alphalove closed 8 years ago

alphalove commented 8 years ago

Hello, I've tried a few other MQTT implementations for the 8266, but this one looks great, well done!

Is it possible to add functionality within this framework to store values that will persist between resets?

My purpose is controlling lights and power sockets and I'd like the devices to set the stored / previous state should they be reset (and can't reach the MQTT broker etc.).

Perhaps even a put() similar to the get() function for the config values?

Thanks again!

(*sorry, I wasn't sure how to tag this as a 'feature request' rather than an issue!)

jpmens commented 8 years ago

One way of doing this is to use the SPIFFS (sp?), i.e. opening, writing, reading a file on the file system of the ESP8266. I suppose this could be done from within the framework, but it might be difficult to "coordinate" the locally stored values with values obtained from retained topics on a connected MQTT broker. For example, device comes online, reads "lamp ON" from SPIFFS, and then possibly gets "lamp OFF" from the broker; this would lead to flickering.

alphalove commented 8 years ago

Hello jpmens, Thanks for the quick reply! I'm new to the use of SPIFFS, but I'll have a look now. Should it be used similarly to how you might use EEPROM.write() in aduinos? Or would it be considered over kill?

The status of the switch should be written to permanent memory every time it changes. I suppose a persistent message from the broker could be conflicting and cause flickering... But I think I'd use the setSetupFunction to send the stored value to the broker on device startup, therefore syncing them back up if they were different.

Thanks again.

benzino77 commented 8 years ago

You could try: ESP.rtcUserMemoryWrite ESP.rtcUserMemoryRead

marvinroger commented 8 years ago

As said above, an Homie sketch is also a normal Arduino sketch, so you can use EEPROM, SPIFFS, RTC memory etc as you would normally do. If you want a simple approach, I would recommend you Embedis, which allows you to do:

String my_value;
Embedis::get("key", my_value);
marvinroger commented 8 years ago

Closing this issue as I am trying to tidy up the issue tracker for the v2. :)