@ -31,7 +31,7 @@ WiFiClientSecure _mqtt_client_secure;
# endif // MQTT_USE_ASYNC
# endif // MQTT_USE_ASYNC
bool _mqtt_enabled = MQTT_ENABLED ;
bool _mqtt_enabled = MQTT_ENABLED ;
unsigned char _mqtt_connection_tries = 0 ;
unsigned long _mqtt_reconnect_delay = MQTT_RECONNECT_DELAY_MIN ;
String _mqtt_topic ;
String _mqtt_topic ;
String _mqtt_setter ;
String _mqtt_setter ;
String _mqtt_getter ;
String _mqtt_getter ;
@ -216,6 +216,7 @@ void _mqttCallback(unsigned int type, const char * topic, const char * payload)
void _mqttOnConnect ( ) {
void _mqttOnConnect ( ) {
DEBUG_MSG_P ( PSTR ( " [MQTT] Connected! \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Connected! \n " ) ) ;
_mqtt_reconnect_delay = MQTT_RECONNECT_DELAY_MIN ;
# if MQTT_SKIP_RETAINED
# if MQTT_SKIP_RETAINED
_mqtt_connected_at = millis ( ) ;
_mqtt_connected_at = millis ( ) ;
@ -316,137 +317,134 @@ bool mqttEnabled() {
void mqttConnect ( ) {
void mqttConnect ( ) {
if ( _mqtt_enabled & ! _mqtt . connected ( ) ) {
// Disable MQTT after MQTT_MAX_TRIES attemps in a row
# if MQTT_MAX_TRIES > 0
static unsigned long last_try = millis ( ) ;
if ( millis ( ) - last_try < MQTT_TRY_INTERVAL ) {
if ( + + _mqtt_connection_tries > MQTT_MAX_TRIES ) {
DEBUG_MSG_P ( PSTR ( " [MQTT] MQTT_MAX_TRIES met, disabling MQTT \n " ) ) ;
mqttEnabled ( false ) ;
_mqtt_connection_tries = 0 ;
return ;
}
} else {
_mqtt_connection_tries = 0 ;
}
last_try = millis ( ) ;
# endif
// Do not connect if disabled
if ( ! _mqtt_enabled ) return ;
if ( _mqtt_user ) free ( _mqtt_user ) ;
if ( _mqtt_pass ) free ( _mqtt_pass ) ;
// Do not connect if already connected
if ( _mqtt . connected ( ) ) return ;
char * host = strdup ( getSetting ( " mqttServer " , MQTT_SERVER ) . c_str ( ) ) ;
if ( strlen ( host ) = = 0 ) return ;
unsigned int port = getSetting ( " mqttPort " , MQTT_PORT ) . toInt ( ) ;
_mqtt_user = strdup ( getSetting ( " mqttUser " , MQTT_USER ) . c_str ( ) ) ;
_mqtt_pass = strdup ( getSetting ( " mqttPassword " , MQTT_PASS ) . c_str ( ) ) ;
if ( _mqtt_will ) free ( _mqtt_will ) ;
_mqtt_will = strdup ( ( _mqtt_topic + MQTT_TOPIC_STATUS ) . c_str ( ) ) ;
// Check reconnect interval
static unsigned long last = 0 ;
if ( millis ( ) - last < _mqtt_reconnect_delay ) return ;
last = millis ( ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Connecting to broker at %s:%d \n " ) , host , port ) ;
// Increase the reconnect delay
_mqtt_reconnect_delay + = MQTT_RECONNECT_DELAY_STEP ;
if ( _mqtt_reconnect_delay > MQTT_RECONNECT_DELAY_MAX ) {
_mqtt_reconnect_delay = MQTT_RECONNECT_DELAY_MAX ;
}
# if MQTT_USE_ASYNC
char * host = strdup ( getSetting ( " mqttServer " , MQTT_SERVER ) . c_str ( ) ) ;
if ( strlen ( host ) = = 0 ) return ;
unsigned int port = getSetting ( " mqttPort " , MQTT_PORT ) . toInt ( ) ;
_mqtt . setServer ( host , port ) ;
_mqtt . setKeepAlive ( MQTT_KEEPALIVE ) . setCleanSession ( false ) ;
_mqtt . setWill ( _mqtt_will , MQTT_QOS , MQTT_RETAIN , " 0 " ) ;
if ( ( strlen ( _mqtt_user ) > 0 ) & & ( strlen ( _mqtt_pass ) > 0 ) ) {
DEBUG_MSG_P ( PSTR ( " [MQTT] Connecting as user %s \n " ) , _mqtt_user ) ;
_mqtt . setCredentials ( _mqtt_user , _mqtt_pass ) ;
}
if ( _mqtt_user ) free ( _mqtt_user ) ;
if ( _mqtt_pass ) free ( _mqtt_pass ) ;
if ( _mqtt_will ) free ( _mqtt_will ) ;
# if ASYNC_TCP_SSL_ENABLED
_mqtt_user = strdup ( getSetting ( " mqttUser " , MQTT_USER ) . c_str ( ) ) ;
_mqtt_pass = strdup ( getSetting ( " mqttPassword " , MQTT_PASS ) . c_str ( ) ) ;
_mqtt_will = strdup ( ( _mqtt_topic + MQTT_TOPIC_STATUS ) . c_str ( ) ) ;
bool secure = getSetting ( " mqttUseSSL " , MQTT_SSL_ENABLED ) . toInt ( ) = = 1 ;
_mqtt . setSecure ( secure ) ;
if ( secure ) {
DEBUG_MSG_P ( PSTR ( " [MQTT] Using SSL \n " ) ) ;
unsigned char fp [ 20 ] = { 0 } ;
if ( mqttFormatFP ( getSetting ( " mqttFP " , MQTT_SSL_FINGERPRINT ) . c_str ( ) , fp ) ) {
_mqtt . addServerFingerprint ( fp ) ;
} else {
DEBUG_MSG_P ( PSTR ( " [MQTT] Wrong fingerprint \n " ) ) ;
}
DEBUG_MSG_P ( PSTR ( " [MQTT] Connecting to broker at %s:%d \n " ) , host , port ) ;
# if MQTT_USE_ASYNC
_mqtt . setServer ( host , port ) ;
_mqtt . setKeepAlive ( MQTT_KEEPALIVE ) . setCleanSession ( false ) ;
_mqtt . setWill ( _mqtt_will , MQTT_QOS , MQTT_RETAIN , " 0 " ) ;
if ( ( strlen ( _mqtt_user ) > 0 ) & & ( strlen ( _mqtt_pass ) > 0 ) ) {
DEBUG_MSG_P ( PSTR ( " [MQTT] Connecting as user %s \n " ) , _mqtt_user ) ;
_mqtt . setCredentials ( _mqtt_user , _mqtt_pass ) ;
}
# if ASYNC_TCP_SSL_ENABLED
bool secure = getSetting ( " mqttUseSSL " , MQTT_SSL_ENABLED ) . toInt ( ) = = 1 ;
_mqtt . setSecure ( secure ) ;
if ( secure ) {
DEBUG_MSG_P ( PSTR ( " [MQTT] Using SSL \n " ) ) ;
unsigned char fp [ 20 ] = { 0 } ;
if ( mqttFormatFP ( getSetting ( " mqttFP " , MQTT_SSL_FINGERPRINT ) . c_str ( ) , fp ) ) {
_mqtt . addServerFingerprint ( fp ) ;
} else {
DEBUG_MSG_P ( PSTR ( " [MQTT] Wrong fingerprint \n " ) ) ;
}
}
}
# endif // ASYNC_TCP_SSL_ENABLED
# endif // ASYNC_TCP_SSL_ENABLED
DEBUG_MSG_P ( PSTR ( " [MQTT] Will topic: %s \n " ) , _mqtt_will ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] QoS: %d \n " ) , MQTT_QOS ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Retain flag: %d \n " ) , MQTT_RETAIN ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Will topic: %s \n " ) , _mqtt_will ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] QoS: %d \n " ) , MQTT_QOS ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Retain flag: %d \n " ) , MQTT_RETAIN ) ;
_mqtt . connect ( ) ;
_mqtt . connect ( ) ;
# else // not MQTT_USE_ASYNC
# else // not MQTT_USE_ASYNC
bool response = true ;
bool response = true ;
# if ASYNC_TCP_SSL_ENABLED
# if ASYNC_TCP_SSL_ENABLED
bool secure = getSetting ( " mqttUseSSL " , MQTT_SSL_ENABLED ) . toInt ( ) = = 1 ;
if ( secure ) {
DEBUG_MSG_P ( PSTR ( " [MQTT] Using SSL \n " ) ) ;
if ( _mqtt_client_secure . connect ( host , port ) ) {
char fp [ 60 ] = { 0 } ;
if ( mqttFormatFP ( getSetting ( " mqttFP " , MQTT_SSL_FINGERPRINT ) . c_str ( ) , fp ) ) {
if ( _mqtt_client_secure . verify ( fp , host ) ) {
_mqtt . setClient ( _mqtt_client_secure ) ;
} else {
DEBUG_MSG_P ( PSTR ( " [MQTT] Invalid fingerprint \n " ) ) ;
response = false ;
}
_mqtt_client_secure . stop ( ) ;
yield ( ) ;
bool secure = getSetting ( " mqttUseSSL " , MQTT_SSL_ENABLED ) . toInt ( ) = = 1 ;
if ( secure ) {
DEBUG_MSG_P ( PSTR ( " [MQTT] Using SSL \n " ) ) ;
if ( _mqtt_client_secure . connect ( host , port ) ) {
char fp [ 60 ] = { 0 } ;
if ( mqttFormatFP ( getSetting ( " mqttFP " , MQTT_SSL_FINGERPRINT ) . c_str ( ) , fp ) ) {
if ( _mqtt_client_secure . verify ( fp , host ) ) {
_mqtt . setClient ( _mqtt_client_secure ) ;
} else {
} else {
DEBUG_MSG_P ( PSTR ( " [MQTT] Wrong fingerprint \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Invalid fingerprint \n " ) ) ;
response = false ;
response = false ;
}
}
_mqtt_client_secure . stop ( ) ;
yield ( ) ;
} else {
} else {
DEBUG_MSG_P ( PSTR ( " [MQTT] Client connection failed \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Wrong fingerprint \n " ) ) ;
response = false ;
response = false ;
}
}
} else {
} else {
_mqtt . setClient ( _mqtt_client ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Client connection failed \n " ) ) ;
response = false ;
}
}
# else // not ASYNC_TCP_SSL_ENABLED
} else {
_mqtt . setClient ( _mqtt_client ) ;
_mqtt . setClient ( _mqtt_client ) ;
}
# endif // ASYNC_TCP_SSL_ENABLED
if ( response ) {
# else // not ASYNC_TCP_SSL_ENABLED
_mqtt . setServer ( host , por t ) ;
_mqtt . setClient ( _mqtt_client ) ;
if ( ( strlen ( _mqtt_user ) > 0 ) & & ( strlen ( _mqtt_pass ) > 0 ) ) {
DEBUG_MSG_P ( PSTR ( " [MQTT] Connecting as user %s \n " ) , _mqtt_user ) ;
response = _mqtt . connect ( getIdentifier ( ) . c_str ( ) , _mqtt_user , _mqtt_pass , _mqtt_will , MQTT_QOS , MQTT_RETAIN , " 0 " ) ;
} else {
response = _mqtt . connect ( getIdentifier ( ) . c_str ( ) , _mqtt_will , MQTT_QOS , MQTT_RETAIN , " 0 " ) ;
}
# endif // ASYNC_TCP_SSL_ENABLED
DEBUG_MSG_P ( PSTR ( " [MQTT] Will topic: %s \n " ) , _mqtt_will ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] QoS: %d \n " ) , MQTT_QOS ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Retain flag: %d \n " ) , MQTT_RETAIN ) ;
if ( response ) {
}
_mqtt . setServer ( host , port ) ;
if ( response ) {
_mqttOnConnect ( ) ;
_mqtt_connected = true ;
if ( ( st rl en( _mqtt_user ) > > < span class="mi">0) & & ( strlen ( _mqtt_pass ) > 0 ) ) {
DEBUG_MSG_P ( PSTR ( " [MQTT] Connecting as user %s \n " ) , _mqtt_user ) ;
response = _mqtt . connect ( getIdentifier ( ) . c_str ( ) , _mqtt_user , _mqtt_pass , _mqtt_will , MQTT_QOS , MQTT_RETAIN , " 0 " ) ;
} else {
} else {
DEBUG_MSG_P ( PSTR ( " [MQTT] Connection failed \n " ) ) ;
response = _mqtt . connect ( getIdentifier ( ) . c_str ( ) , _mqtt_will , MQTT_QOS , MQTT_RETAIN , " 0 " ) ;
}
}
# endif // MQTT_USE_ASYNC
DEBUG_MSG_P ( PSTR ( " [MQTT] Will topic: %s \n " ) , _mqtt_will ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] QoS: %d \n " ) , MQTT_QOS ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Retain flag: %d \n " ) , MQTT_RETAIN ) ;
}
if ( response ) {
_mqttOnConnect ( ) ;
} else {
DEBUG_MSG_P ( PSTR ( " [MQTT] Connection failed \n " ) ) ;
}
free ( host ) ;
# endif // MQTT_USE_ASYNC
}
free ( host ) ;
}
}
@ -463,7 +461,6 @@ void mqttConfigure() {
_mqtt_forward = ! _mqtt_getter . equals ( _mqtt_setter ) ;
_mqtt_forward = ! _mqtt_getter . equals ( _mqtt_setter ) ;
// Enable
// Enable
_mqtt_connection_tries = 0 ;
if ( getSetting ( " mqttServer " , MQTT_SERVER ) . length ( ) = = 0 ) {
if ( getSetting ( " mqttServer " , MQTT_SERVER ) . length ( ) = = 0 ) {
mqttEnabled ( false ) ;
mqttEnabled ( false ) ;
} else {
} else {
@ -553,22 +550,14 @@ void mqttSetup() {
void mqttLoop ( ) {
void mqttLoop ( ) {
# if MQTT_USE_ASYNC
if ( WiFi . status ( ) ! = WL_CONNECTED ) return ;
if ( ! _mqtt_enabled ) return ;
if ( WiFi . status ( ) ! = WL_CONNECTED ) return ;
if ( _mqtt . connected ( ) ) return ;
# if MQTT_USE_ASYNC
static unsigned long last = 0 ;
if ( millis ( ) - last > MQTT_RECONNECT_DELAY ) {
last = millis ( ) ;
mqttConnect ( ) ;
}
mqttConnect ( ) ;
# else // not MQTT_USE_ASYNC
# else // not MQTT_USE_ASYNC
if ( WiFi . status ( ) ! = WL_CONNECTED ) return ;
if ( _mqtt . connected ( ) ) {
if ( _mqtt . connected ( ) ) {
_mqtt . loop ( ) ;
_mqtt . loop ( ) ;
@ -580,13 +569,7 @@ void mqttLoop() {
_mqtt_connected = false ;
_mqtt_connected = false ;
}
}
if ( _mqtt_enabled ) {
static unsigned long last = 0 ;
if ( millis ( ) - last > MQTT_RECONNECT_DELAY ) {
last = millis ( ) ;
mqttConnect ( ) ;
}
}
mqttConnect ( ) ;
}
}