Re-publish BTtoMQTT Theengs discovery topics on MQTT re-connect (#1290)
* Re-publish BTtoMQTT Theengs discovery topics on MQTT re-connect - Improved version of #1274, this should re-publish more - Handle return value of (MQTT) client.loop() to detect connection loss faster Signed-off-by: Sami Salonen <ssalonen@gmail.com>
This commit is contained in:
1 parent
8bc65d62dc
commit
941b1e109f
3 files changed
+20
-21
No files matched your search
+5
-4
@@ -853,8 +853,9 @@ boolean valid_service_data(const char* data, int size) {
|
|||||||
|
|
||||||
# ifdef ZmqttDiscovery
|
# ifdef ZmqttDiscovery
|
||||||
// This function always should be called from the main core as it generates direct mqtt messages
|
// This function always should be called from the main core as it generates direct mqtt messages
|
||||||
void launchBTDiscovery() {
|
// When overrideDiscovery=true, we publish discovery messages of known devices (even if no new)
|
||||||
if (newDevices == 0)
|
void launchBTDiscovery(bool overrideDiscovery) {
|
||||||
|
if (!overrideDiscovery && newDevices == 0)
|
||||||
return;
|
return;
|
||||||
if (xSemaphoreTake(semaphoreCreateOrUpdateDevice, pdMS_TO_TICKS(1000)) == pdFALSE) {
|
if (xSemaphoreTake(semaphoreCreateOrUpdateDevice, pdMS_TO_TICKS(1000)) == pdFALSE) {
|
||||||
Log.error(F("Semaphore NOT taken" CR));
|
Log.error(F("Semaphore NOT taken" CR));
|
||||||
@@ -866,8 +867,8 @@ void launchBTDiscovery() {
|
|||||||
for (vector<BLEdevice*>::iterator it = localDevices.begin(); it != localDevices.end(); ++it) {
|
for (vector<BLEdevice*>::iterator it = localDevices.begin(); it != localDevices.end(); ++it) {
|
||||||
BLEdevice* p = *it;
|
BLEdevice* p = *it;
|
||||||
Log.trace(F("Device mac %s" CR), p->macAdr);
|
Log.trace(F("Device mac %s" CR), p->macAdr);
|
||||||
// Do not launch discovery for the devices already discovered or that are not unique by their MAC Address (Ibeacon, GAEN and Microsoft Cdp)
|
// Do not launch discovery for the devices already discovered (unless we have overrideDiscovery) or that are not unique by their MAC Address (Ibeacon, GAEN and Microsoft Cdp)
|
||||||
if (!isDiscovered(p) &&
|
if ((overrideDiscovery || !isDiscovered(p)) &&
|
||||||
p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::IBEACON &&
|
p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::IBEACON &&
|
||||||
p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::MS_CDP &&
|
p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::MS_CDP &&
|
||||||
p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::GAEN) {
|
p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::GAEN) {
|
||||||
|
|||||||
+1
-1
@@ -30,7 +30,7 @@ extern void setupBT();
|
|||||||
extern bool BTtoMQTT();
|
extern bool BTtoMQTT();
|
||||||
extern void MQTTtoBT(char* topicOri, JsonObject& RFdata);
|
extern void MQTTtoBT(char* topicOri, JsonObject& RFdata);
|
||||||
extern void emptyBTQueue();
|
extern void emptyBTQueue();
|
||||||
extern void launchBTDiscovery();
|
extern void launchBTDiscovery(bool overrideDiscovery);
|
||||||
|
|
||||||
#ifdef ESP32
|
#ifdef ESP32
|
||||||
extern int btQueueBlocked;
|
extern int btQueueBlocked;
|
||||||
|
|||||||
+14
-16
@@ -180,7 +180,7 @@ char WifiManager_ssid[MAC_NAME_MAX_LEN];
|
|||||||
char ota_hostname[MAC_NAME_MAX_LEN];
|
char ota_hostname[MAC_NAME_MAX_LEN];
|
||||||
#endif
|
#endif
|
||||||
bool connectedOnce = false; //indicate if we have been connected once to MQTT
|
bool connectedOnce = false; //indicate if we have been connected once to MQTT
|
||||||
bool justReconnected = false; //indicate when we have just re-connected to MQTT
|
bool connected = false; //indicate whether we are currently connected. Used to detected re-connection
|
||||||
int failure_number_ntwk = 0; // number of failure connecting to network
|
int failure_number_ntwk = 0; // number of failure connecting to network
|
||||||
int failure_number_mqtt = 0; // number of failure connecting to MQTT
|
int failure_number_mqtt = 0; // number of failure connecting to MQTT
|
||||||
#ifdef ZmqttDiscovery
|
#ifdef ZmqttDiscovery
|
||||||
@@ -1374,23 +1374,22 @@ void loop() {
|
|||||||
if ((Ethernet.hardwareStatus() != EthernetW5100 && Ethernet.linkStatus() == LinkON) || (Ethernet.hardwareStatus() == EthernetW5100)) { //we are able to detect disconnection only on w5200 and w5500
|
if ((Ethernet.hardwareStatus() != EthernetW5100 && Ethernet.linkStatus() == LinkON) || (Ethernet.hardwareStatus() == EthernetW5100)) { //we are able to detect disconnection only on w5200 and w5500
|
||||||
#endif
|
#endif
|
||||||
failure_number_ntwk = 0;
|
failure_number_ntwk = 0;
|
||||||
if (client.connected()) {
|
if (client.loop()) { // MQTT client is still connected
|
||||||
digitalWrite(LED_INFO, LED_INFO_ON);
|
digitalWrite(LED_INFO, LED_INFO_ON);
|
||||||
failure_number_ntwk = 0;
|
failure_number_ntwk = 0;
|
||||||
|
// We have just re-connected if connected was previously false
|
||||||
client.loop();
|
bool justReconnected = !connected;
|
||||||
|
|
||||||
#ifdef ZmqttDiscovery
|
#ifdef ZmqttDiscovery
|
||||||
if (disc) {
|
// at first connection we publish the discovery payloads
|
||||||
if (!connectedOnce || (discovery_republish_on_reconnect && !justReconnected)) {
|
// or, when we have just re-connected (only when discovery_republish_on_reconnect is enabled)
|
||||||
// at first connection we publish the discovery payloads
|
bool publishDiscovery = disc && (!connectedOnce || (discovery_republish_on_reconnect && justReconnected));
|
||||||
// or, when we have just re-connected (only when discovery_republish_on_reconnect is enabled)
|
if (publishDiscovery) {
|
||||||
pubMqttDiscovery();
|
pubMqttDiscovery();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
connectedOnce = true;
|
connectedOnce = true;
|
||||||
justReconnected = true;
|
connected = true;
|
||||||
#if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
|
#if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
|
||||||
if (now > (timer_sys_measures + (TimeBetweenReadingSYS * 1000)) || !timer_sys_measures) {
|
if (now > (timer_sys_measures + (TimeBetweenReadingSYS * 1000)) || !timer_sys_measures) {
|
||||||
timer_sys_measures = millis();
|
timer_sys_measures = millis();
|
||||||
@@ -1460,7 +1459,7 @@ void loop() {
|
|||||||
#ifdef ZgatewayBT
|
#ifdef ZgatewayBT
|
||||||
# ifdef ZmqttDiscovery
|
# ifdef ZmqttDiscovery
|
||||||
if (disc)
|
if (disc)
|
||||||
launchBTDiscovery();
|
launchBTDiscovery(publishDiscovery);
|
||||||
# endif
|
# endif
|
||||||
# ifndef ESP32
|
# ifndef ESP32
|
||||||
if (BTtoMQTT())
|
if (BTtoMQTT())
|
||||||
@@ -1496,13 +1495,12 @@ void loop() {
|
|||||||
RTL_433Loop();
|
RTL_433Loop();
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} else {
|
||||||
// MQTT disconnected, we reset the justReconnected flag
|
// MQTT disconnected
|
||||||
justReconnected = false;
|
connected = false;
|
||||||
connectMQTT();
|
connectMQTT();
|
||||||
}
|
}
|
||||||
} else { // disconnected from network
|
} else { // disconnected from network
|
||||||
// Network disconnected, we reset the justReconnected flag
|
connected = false;
|
||||||
justReconnected = false;
|
|
||||||
Log.warning(F("Network disconnected:" CR));
|
Log.warning(F("Network disconnected:" CR));
|
||||||
digitalWrite(LED_ERROR, LED_ERROR_ON);
|
digitalWrite(LED_ERROR, LED_ERROR_ON);
|
||||||
delay(2000); // add a delay to avoid ESP32 crash and reset
|
delay(2000); // add a delay to avoid ESP32 crash and reset
|
||||||
|
|||||||
Reference in new issue
Block a user