mirror of
https://github.com/lumapu/ahoy.git
synced 2025-05-14 17:36:38 +02:00
MQTT subscribe only when reconnect successful
This commit is contained in:
parent
b48256b2e8
commit
e59ca5f388
1 changed files with 11 additions and 8 deletions
|
@ -54,7 +54,7 @@ class mqtt {
|
||||||
|
|
||||||
bool isConnected(bool doRecon = false) {
|
bool isConnected(bool doRecon = false) {
|
||||||
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:isConnected"));
|
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:isConnected"));
|
||||||
if(doRecon)
|
if(doRecon && !mClient->connected())
|
||||||
reconnect();
|
reconnect();
|
||||||
return mClient->connected();
|
return mClient->connected();
|
||||||
}
|
}
|
||||||
|
@ -71,6 +71,7 @@ class mqtt {
|
||||||
DPRINTLN(DBG_DEBUG, F("mqtt.h:reconnect"));
|
DPRINTLN(DBG_DEBUG, F("mqtt.h:reconnect"));
|
||||||
DPRINTLN(DBG_DEBUG, F("MQTT mClient->_state ") + String(mClient->state()) );
|
DPRINTLN(DBG_DEBUG, F("MQTT mClient->_state ") + String(mClient->state()) );
|
||||||
DPRINTLN(DBG_DEBUG, F("WIFI mEspClient.status ") + String(mEspClient.status()) );
|
DPRINTLN(DBG_DEBUG, F("WIFI mEspClient.status ") + String(mEspClient.status()) );
|
||||||
|
boolean resub = false;
|
||||||
if(!mClient->connected()) {
|
if(!mClient->connected()) {
|
||||||
if(strlen(mDevName) > 0) {
|
if(strlen(mDevName) > 0) {
|
||||||
// der Server und der Port müssen neu gesetzt werden,
|
// der Server und der Port müssen neu gesetzt werden,
|
||||||
|
@ -78,16 +79,18 @@ class mqtt {
|
||||||
mClient->setServer(mCfg->broker, mCfg->port);
|
mClient->setServer(mCfg->broker, mCfg->port);
|
||||||
mClient->setBufferSize(MQTT_MAX_PACKET_SIZE);
|
mClient->setBufferSize(MQTT_MAX_PACKET_SIZE);
|
||||||
if((strlen(mCfg->user) > 0) && (strlen(mCfg->pwd) > 0))
|
if((strlen(mCfg->user) > 0) && (strlen(mCfg->pwd) > 0))
|
||||||
mClient->connect(mDevName, mCfg->user, mCfg->pwd);
|
resub = mClient->connect(mDevName, mCfg->user, mCfg->pwd);
|
||||||
else
|
else
|
||||||
mClient->connect(mDevName);
|
resub = mClient->connect(mDevName);
|
||||||
}
|
}
|
||||||
// ein Subscribe ist nur nach einem connect notwendig
|
// ein Subscribe ist nur nach einem connect notwendig
|
||||||
char topic[MQTT_TOPIC_LEN + 13 ]; // "/devcontrol/#" --> + 6 byte
|
if(resub) {
|
||||||
// ToDo: "/devcontrol/#" is hardcoded
|
char topic[MQTT_TOPIC_LEN + 13 ]; // "/devcontrol/#" --> + 6 byte
|
||||||
snprintf(topic, MQTT_TOPIC_LEN + 13, "%s/devcontrol/#", mCfg->topic);
|
// ToDo: "/devcontrol/#" is hardcoded
|
||||||
DPRINTLN(DBG_INFO, F("subscribe to ") + String(topic));
|
snprintf(topic, MQTT_TOPIC_LEN + 13, "%s/devcontrol/#", mCfg->topic);
|
||||||
mClient->subscribe(topic); // subscribe to mTopic + "/devcontrol/#"
|
DPRINTLN(DBG_INFO, F("subscribe to ") + String(topic));
|
||||||
|
mClient->subscribe(topic); // subscribe to mTopic + "/devcontrol/#"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue