Reconnect: keep the wait across short sessions, name a timeout and a lost link, setServer()
The wait between attempts started at 1 s again when the broker accepted the session, so a session that was closed right after it opened (two boards with one client id) came back every 2 s without end. It now starts again only after a session that lasted 60 s; across shorter ones it keeps doubling. An attempt given up after 30 s prints "the broker did not answer within 30 s", and loss of Wi-Fi prints "Wi-Fi is down, waiting for it" once per loss. setServer(host, port) before begin() names another broker; the host is not copied. basicLight: static RAM 30,616 B (+16), flash 333,701 B (+264); 91 host tests (bridge logic 50). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
d3e7ee7f2a
commit
42af413a6e
6 changed files with 164 additions and 23 deletions
|
|
@ -5,7 +5,7 @@
|
|||
* @contributors chaos511
|
||||
*
|
||||
* @description Companion library of the Alex2MQTT Alexa skill: the devices a sketch declares become Alexa
|
||||
* endpoints through the MQTT broker at alex2mqtt.stormysdream.club.
|
||||
* endpoints through the MQTT broker at alex2mqtt.stormysdream.club, or the one of setServer().
|
||||
*
|
||||
* <root>/discover in answered with one discovery object per device on <root>/discover_r
|
||||
* <root>/<endpointId>/alexaDirective in the directive, handed to the handler of the device (onDirective(), or
|
||||
|
|
@ -47,8 +47,14 @@ public:
|
|||
// The username and the password are not copied: they have to stay valid for as long as the client is used.
|
||||
// Starts SNTP; loop() opens the MQTT session once Wi-Fi is up and the clock is set, or after 5 s of Wi-Fi
|
||||
// without an answer. A session that ends or is refused is opened again after 1 s, then 2 s, 4 s ... up to once
|
||||
// a minute, while Wi-Fi is up; the reason is printed and returned by getDisconnectReason().
|
||||
// a minute, while Wi-Fi is up; the reason is printed and returned by getDisconnectReason(). The wait is 1 s
|
||||
// again after a session that lasted a minute.
|
||||
void begin(const char *username, const char *password, const char *rootTopic);
|
||||
|
||||
// Before begin(), for a broker other than alex2mqtt.stormysdream.club:1883: a name or an address as text.
|
||||
// The host is not copied: it has to stay valid for as long as the client is used. After begin() the call is
|
||||
// ignored with an error.
|
||||
void setServer(const char *host, uint16_t port);
|
||||
Alex2ESPState getState() const;
|
||||
|
||||
// Call from the sketch's loop(): connects, answers discovery requests and hands one directive per call to its
|
||||
|
|
@ -81,6 +87,8 @@ private:
|
|||
static const unsigned long DISCOVERY_RETRY_MS = 20; // Pause before a refused discovery publish is tried again
|
||||
|
||||
AsyncMqttClient mqttClient; // MQTT client instance
|
||||
const char *serverHost; // The broker: the default, or what setServer() was given
|
||||
uint16_t serverPort;
|
||||
String rootTopic; // Root topic for communication
|
||||
String discoverTopic; // The topic we listen on for discovery messages
|
||||
String discoverTopicSend; // The topic we send discovery messages
|
||||
|
|
@ -93,6 +101,8 @@ private:
|
|||
bool useSntp;
|
||||
unsigned long clockWaitStarted; // millis() when begin() ran, or when Wi-Fi was last seen down before the first connect
|
||||
bool linkWaitLogged; // The wait for Wi-Fi before the first connect has been reported
|
||||
bool linkWasUp; // Wi-Fi was up when loop() looked last
|
||||
bool attemptTimedOut; // The connect that is being closed had no answer within CONNECT_TIMEOUT_MS
|
||||
bool clockWarned; // The unset clock has been reported
|
||||
unsigned long lastClockWarning; // millis() of that report
|
||||
unsigned long waitingSince; // millis() of the connect while CONNECTING, of the disconnect while DISCONNECTED
|
||||
|
|
@ -119,6 +129,7 @@ private:
|
|||
void onMessage(char *topic, char *payload, AsyncMqttClientMessageProperties properties, size_t length, size_t index, size_t total);
|
||||
|
||||
//loop processing function
|
||||
void watchLink();
|
||||
void connectWhenClockIsSet();
|
||||
void connect();
|
||||
void warnAboutClock();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue