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

Create a "safe" mode #30

Closed marvinroger closed 8 years ago

marvinroger commented 8 years ago

There is a limitation, from the Wiki

The whole Homie for ESP8266 code is designed to be non-blocking, so that you can do other tasks in the main loop(). However, the connection to the MQTT broker is blocking during ~5 seconds in case the server is unreachable. This is an Arduino for ESP8266 limitation, and we can't do anything on our side to solve this issue, not even a timeout.

This is not really true, it is possible to avoid connecting / reconnecting to the MQTT if a critical operation is happening. Let's say you are controlling shutters with a relay, you want your loop to be executed without the 5 seconds delay mentionned above, because else you will loose the current state of your shutters. Before moving the shutters, you could do:

Homie.enableSafeMode(true);

If the MQTT connection is lost while in safe mode, it does not try to reconnect. When the shutters is done moving, we call back:

Homie.enableSafeMode(false);

It would then reconnect if previously disconnected.

marvinroger commented 8 years ago

v2.0.0 will be totally asynchronous, not an issue anymore.