diff --git a/src/EspMQTTClient.cpp b/src/EspMQTTClient.cpp index 3bcd5b0..5d196ba 100644 --- a/src/EspMQTTClient.cpp +++ b/src/EspMQTTClient.cpp @@ -116,8 +116,10 @@ void EspMQTTClient::enableHTTPWebUpdater(const char* username, const char* passw _updateServerPassword = (char*)password; _updateServerAddress = (char*)address; } +#ifdef ENABLE_DEBUG_MESSAGES else if (_enableDebugMessages) Serial.print("SYS! You can't call enableHTTPWebUpdater() more than once !\n"); +#endif } void EspMQTTClient::enableHTTPWebUpdater(const char* address) @@ -212,9 +214,10 @@ bool EspMQTTClient::handleWiFi() { if(WiFi.status() == WL_CONNECT_FAILED || millis() - _lastWifiConnectionAttemptMillis >= _wifiReconnectionAttemptDelay) { +#ifdef ENABLE_DEBUG_MESSAGES if(_enableDebugMessages) Serial.printf("WiFi! Connection attempt failed, delay expired. (%fs). \n", millis()/1000.0); - +#endif WiFi.disconnect(true); MDNS.end(); @@ -311,14 +314,18 @@ bool EspMQTTClient::handleMQTT() _mqttClient.disconnect(); _failedMQTTConnectionAttemptCount++; +#ifdef ENABLE_DEBUG_MESSAGES if (_enableDebugMessages) Serial.printf("MQTT!: Failed MQTT connection count: %i \n", _failedMQTTConnectionAttemptCount); +#endif // When there is too many failed attempt, sometimes it help to reset the WiFi connection or to restart the board. if(_handleWiFi && _failedMQTTConnectionAttemptCount == 8) { +#ifdef ENABLE_DEBUG_MESSAGES if (_enableDebugMessages) Serial.println("MQTT!: Can't connect to broker after too many attempt, resetting WiFi ..."); +#endif WiFi.disconnect(true); MDNS.end(); @@ -329,8 +336,10 @@ bool EspMQTTClient::handleMQTT() } else if(_drasticResetOnConnectionFailures && _failedMQTTConnectionAttemptCount == 12) // Will reset after 12 failed attempt (3 minutes of retry) { +#ifdef ENABLE_DEBUG_MESSAGES if (_enableDebugMessages) Serial.println("MQTT!: Can't connect to broker after too many attempt, resetting board ..."); +#endif #ifdef ESP8266 ESP.reset(); @@ -356,8 +365,10 @@ bool EspMQTTClient::handleMQTT() void EspMQTTClient::onWiFiConnectionEstablished() { +#ifdef ENABLE_DEBUG_MESSAGES if (_enableDebugMessages) Serial.printf("WiFi: Connected (%fs), ip : %s \n", millis()/1000.0, WiFi.localIP().toString().c_str()); +#endif // Config of web updater if (_httpServer != NULL) @@ -367,8 +378,10 @@ void EspMQTTClient::onWiFiConnectionEstablished() _httpServer->begin(); MDNS.addService("http", "tcp", 80); +#ifdef ENABLE_DEBUG_MESSAGES if (_enableDebugMessages) Serial.printf("WEB: Updater ready, open http://%s.local in your browser and login with username '%s' and password '%s'.\n", _mqttClientName, _updateServerUsername, _updateServerPassword); +#endif } if (_enableOTA) @@ -377,8 +390,10 @@ void EspMQTTClient::onWiFiConnectionEstablished() void EspMQTTClient::onWiFiConnectionLost() { +#ifdef ENABLE_DEBUG_MESSAGES if (_enableDebugMessages) Serial.printf("WiFi! Lost connection (%fs). \n", millis()/1000.0); +#endif // If we handle wifi, we force disconnection to clear the last connection if (_handleWiFi) @@ -396,11 +411,13 @@ void EspMQTTClient::onMQTTConnectionEstablished() void EspMQTTClient::onMQTTConnectionLost() { +#ifdef ENABLE_DEBUG_MESSAGES if (_enableDebugMessages) { Serial.printf("MQTT! Lost connection (%fs). \n", millis()/1000.0); Serial.printf("MQTT: Retrying to connect in %i seconds. \n", _mqttReconnectionAttemptDelay / 1000); } +#endif } @@ -412,8 +429,10 @@ bool EspMQTTClient::setMaxPacketSize(const uint16_t size) bool success = _mqttClient.setBufferSize(size); +#ifdef ENABLE_DEBUG_MESSAGES if(!success && _enableDebugMessages) Serial.println("MQTT! failed to set the max packet size."); +#endif return success; } @@ -423,14 +442,17 @@ bool EspMQTTClient::publish(const char* topic, const uint8_t* payload, unsigned // Do not try to publish if MQTT is not connected. if(!isConnected()) { +#ifdef ENABLE_DEBUG_MESSAGES if (_enableDebugMessages) Serial.println("MQTT! Trying to publish when disconnected, skipping."); +#endif return false; } bool success = _mqttClient.publish(topic, payload, plength, retain); +#ifdef ENABLE_DEBUG_MESSAGES if (_enableDebugMessages) { if(success) @@ -438,6 +460,7 @@ bool EspMQTTClient::publish(const char* topic, const uint8_t* payload, unsigned else Serial.println("MQTT! publish failed, is the message too long ? (see setMaxPacketSize())"); // This can occurs if the message is too long according to the maximum defined in PubsubClient.h } +#endif return success; } @@ -453,8 +476,10 @@ bool EspMQTTClient::subscribe(const String &topic, MessageReceivedCallback messa // Do not try to subscribe if MQTT is not connected. if(!isConnected()) { +#ifdef ENABLE_DEBUG_MESSAGES if (_enableDebugMessages) Serial.println("MQTT! Trying to subscribe when disconnected, skipping."); +#endif return false; } @@ -472,6 +497,7 @@ bool EspMQTTClient::subscribe(const String &topic, MessageReceivedCallback messa _topicSubscriptionList.push_back({ topic, messageReceivedCallback, NULL }); } +#ifdef ENABLE_DEBUG_MESSAGES if (_enableDebugMessages) { if(success) @@ -479,6 +505,7 @@ bool EspMQTTClient::subscribe(const String &topic, MessageReceivedCallback messa else Serial.println("MQTT! subscribe failed"); } +#endif return success; } @@ -498,8 +525,10 @@ bool EspMQTTClient::unsubscribe(const String &topic) // Do not try to unsubscribe if MQTT is not connected. if(!isConnected()) { +#ifdef ENABLE_DEBUG_MESSAGES if (_enableDebugMessages) Serial.println("MQTT! Trying to unsubscribe when disconnected, skipping."); +#endif return false; } @@ -513,13 +542,17 @@ bool EspMQTTClient::unsubscribe(const String &topic) _topicSubscriptionList.erase(_topicSubscriptionList.begin() + i); i--; +#ifdef ENABLE_DEBUG_MESSAGES if(_enableDebugMessages) Serial.printf("MQTT: Unsubscribed from %s\n", topic.c_str()); +#endif } else { +#ifdef ENABLE_DEBUG_MESSAGES if(_enableDebugMessages) Serial.println("MQTT! unsubscribe failed"); +#endif return false; } @@ -564,8 +597,10 @@ void EspMQTTClient::connectToWifi() #endif WiFi.begin(_wifiSsid, _wifiPassword); +#ifdef ENABLE_DEBUG_MESSAGES if (_enableDebugMessages) Serial.printf("\nWiFi: Connecting to %s ... (%fs) \n", _wifiSsid, millis()/1000.0); +#endif } // Try to connect to the MQTT broker and return True if the connection is successfull (blocking) @@ -575,6 +610,7 @@ bool EspMQTTClient::connectToMqttBroker() if (_mqttServerIp != nullptr && strlen(_mqttServerIp) > 0) { +#ifdef ENABLE_DEBUG_MESSAGES if (_enableDebugMessages) { if (_mqttUsername) @@ -582,6 +618,7 @@ bool EspMQTTClient::connectToMqttBroker() else Serial.printf("MQTT: Connecting to broker \"%s\" with client name \"%s\" ... (%fs)", _mqttServerIp, _mqttClientName, millis()/1000.0); } +#endif // explicitly set the server/port here in case they were not provided in the constructor _mqttClient.setServer(_mqttServerIp, _mqttServerPort); @@ -589,11 +626,14 @@ bool EspMQTTClient::connectToMqttBroker() } else { +#ifdef ENABLE_DEBUG_MESSAGES if (_enableDebugMessages) Serial.printf("MQTT: Broker server ip is not set, not connecting (%fs)\n", millis()/1000.0); success = false; +#endif } +#ifdef ENABLE_DEBUG_MESSAGES if (_enableDebugMessages) { if (success) @@ -636,6 +676,7 @@ bool EspMQTTClient::connectToMqttBroker() Serial.printf("MQTT: Retrying to connect in %i seconds.\n", _mqttReconnectionAttemptDelay / 1000); } } +#endif return success; } @@ -726,8 +767,10 @@ void EspMQTTClient::mqttMessageReceivedCallback(char* topic, uint8_t* payload, u { strTerminationPos = length - 1; +#ifdef ENABLE_DEBUG_MESSAGES if (_enableDebugMessages) Serial.print("MQTT! Your message may be truncated, please set setMaxPacketSize() to a higher value.\n"); +#endif } else strTerminationPos = length; @@ -737,9 +780,11 @@ void EspMQTTClient::mqttMessageReceivedCallback(char* topic, uint8_t* payload, u String payloadStr((char*)payload); String topicStr(topic); +#ifdef ENABLE_DEBUG_MESSAGES // Logging if (_enableDebugMessages) Serial.printf("MQTT >> [%s] %s\n", topic, payloadStr.c_str()); +#endif // Send the message to subscribers for (std::size_t i = 0 ; i < _topicSubscriptionList.size() ; i++) diff --git a/src/EspMQTTClient.h b/src/EspMQTTClient.h index 5511bf5..5e7378a 100644 --- a/src/EspMQTTClient.h +++ b/src/EspMQTTClient.h @@ -29,6 +29,10 @@ #endif +// remove macro when compiling to remove all calls to Serial.print[ln]? +// and reduce binary size +#define ENABLE_DEBUG_MESSAGES + void onConnectionEstablished(); // MUST be implemented in your sketch. Called once everythings is connected (Wifi, mqtt). typedef std::function ConnectionEstablishedCallback;