@ -15,24 +15,38 @@ Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
# include <vector>
# include <vector>
# include <utility>
# include <utility>
# include <Ticker.h>
# include <Ticker.h>
# include <TimeLib.h>
# if MQTT_LIBRARY == MQTT_ASYNC // AsyncMqttClient
# include <AsyncMqttClient.h>
AsyncMqttClient _mqtt ;
# else // Arduino-MQTT or PubSubClient
WiFiClient _mqtt_client ;
bool _mqtt_connected = false ;
# include "WiFiClientSecure.h"
# if SECURE_CLIENT == SECURE_CLIENT_AXTLS
using namespace axTLS ;
# elif SECURE_CLIENT == SECURE_CLIENT_BEARSSL
using namespace BearSSL ;
BearSSL : : X509List * _ca_list = nullptr ;
# endif
# if MQTT_USE_ASYNC // Using AsyncMqttClient
# include <AsyncMqttClient.h>
AsyncMqttClient _mqtt ;
# else // Using PubSubClient
# include <PubSubClient.h>
PubSubClient _mqtt ;
bool _mqtt_connected = false ;
WiFiClient _mqtt_client ;
# if ASYNC_TCP_SSL_ENABLED
WiFiClientSecure _mqtt_client_secure ;
# endif // ASYNC_TCP_SSL_ENABLED
WiFiClientSecure _mqtt_client_secure ;
# endif // MQTT_USE_ASYNC
# if MQTT_LIBRARY == MQTT_ARDUINO // Using Arduino-MQTT
# include <MQTTClient.h>
# ifdef MQTT_MAX_PACKET_SIZE
MQTTClient _mqtt ( MQTT_MAX_PACKET_SIZE ) ;
# else
MQTTClient _mqtt ( ) ;
# endif
# else // Using PubSubClient
# include <PubSubClient.h>
PubSubClient _mqtt ;
# endif
# endif
bool _mqtt_enabled = MQTT_ENABLED ;
bool _mqtt_enabled = MQTT_ENABLED ;
bool _mqtt_use_json = false ;
bool _mqtt_use_json = false ;
@ -92,109 +106,176 @@ void _mqttConnect() {
DEBUG_MSG_P ( PSTR ( " [MQTT] Connecting to broker at %s:%u \n " ) , _mqtt_server . c_str ( ) , _mqtt_port ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Connecting to broker at %s:%u \n " ) , _mqtt_server . c_str ( ) , _mqtt_port ) ;
# if MQTT_USE_ASYNC
_mqtt_connecting = true ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Client ID: %s \n " ) , _mqtt_clientid . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] QoS: %d \n " ) , _mqtt_qos ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Retain flag: %d \n " ) , _mqtt_retain ? 1 : 0 ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Keepalive time: %ds \n " ) , _mqtt_keepalive ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Will topic: %s \n " ) , _mqtt_will . c_str ( ) ) ;
_mqtt_connecting = true ;
# if MQTT_LIBRARY == MQTT_ASYNC
_mqtt . setServer ( _mqtt_server . c_str ( ) , _mqtt_port ) ;
_mqtt . setServer ( _mqtt_server . c_str ( ) , _mqtt_port ) ;
_mqtt . setClientId ( _mqtt_clientid . c_str ( ) ) ;
_mqtt . setClientId ( _mqtt_clientid . c_str ( ) ) ;
_mqtt . setKeepAlive ( _mqtt_keepalive ) ;
_mqtt . setKeepAlive ( _mqtt_keepalive ) ;
_mqtt . setCleanSession ( false ) ;
_mqtt . setCleanSession ( false ) ;
_mqtt . setWill ( _mqtt_will . c_str ( ) , _mqtt_qos , _mqtt_retain , " 0 " ) ;
_mqtt . setWill ( _mqtt_will . c_str ( ) , _mqtt_qos , _mqtt_retain , MQTT_STATUS_OFFLINE ) ;
if ( _mqtt_user . length ( ) & & _mqtt_pass . length ( ) ) {
if ( _mqtt_user . length ( ) & & _mqtt_pass . length ( ) ) {
DEBUG_MSG_P ( PSTR ( " [MQTT] Connecting as user %s \n " ) , _mqtt_user . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Connecting as user %s \n " ) , _mqtt_user . c_str ( ) ) ;
_mqtt . setCredentials ( _mqtt_user . c_str ( ) , _mqtt_pass . c_str ( ) ) ;
_mqtt . setCredentials ( _mqtt_user . c_str ( ) , _mqtt_pass . c_str ( ) ) ;
}
}
# if ASYNC_TCP_SSL_ENABLED
# if SECURE_CLIENT != SECURE_CLIENT_NONE
bool secure = getSetting ( " mqttUseSSL " , MQTT_SSL_ENABLED ) . toInt ( ) = = 1 ;
bool secure = getSetting ( " mqttUseSSL " , MQTT_SSL_ENABLED ) . toInt ( ) = = 1 ;
_mqtt . setSecure ( secure ) ;
_mqtt . setSecure ( secure ) ;
if ( secure ) {
if ( secure ) {
DEBUG_MSG_P ( PSTR ( " [MQTT] Using SSL \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Using SSL \n " ) ) ;
unsigned char fp [ 20 ] = { 0 } ;
if ( sslFingerPrintArray ( getSetting ( " mqttFP " , MQTT_SSL_FINGERPRINT ) . c_str ( ) , fp ) ) {
_mqtt . addServerFingerprint ( fp ) ;
int check = getSetting ( " mqttScCheck " , MQTT_SECURE_CLIENT_CHECK ) . toInt ( ) ;
if ( check = = SECURE_CLIENT_CHECK_FINGERPRINT ) {
DEBUG_MSG_P ( PSTR ( " [MQTT] Using fingerprint verification, trying to connect \n " ) ) ;
unsigned char fp [ 20 ] = { 0 } ;
if ( sslFingerPrintArray ( getSetting ( " mqttFP " , MQTT_SSL_FINGERPRINT ) . c_str ( ) , fp ) ) {
_mqtt . addServerFingerprint ( fp ) ;
} else {
DEBUG_MSG_P ( PSTR ( " [MQTT] Wrong fingerprint, cannot connect \n " ) ) ;
_mqtt_last_connection = millis ( ) ;
return ;
}
} else if ( check = = SECURE_CLIENT_CHECK_CA ) {
DEBUG_MSG_P ( PSTR ( " [MQTT] CA verification is not supported with AsyncMqttClient, cannot connect \n " ) ) ;
_mqtt_last_connection = millis ( ) ;
return ;
} else {
} else {
DEBUG_MSG_P ( PSTR ( " [MQTT] Wrong fingerprint \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] !!! SSL connection will not be validated !!! \n " ) ) ;
}
}
}
}
# endif // ASYNC_TCP_SSL_ENABLED
DEBUG_MSG_P ( PSTR ( " [MQTT] Client ID: %s \n " ) , _mqtt_clientid . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] QoS: %d \n " ) , _mqtt_qos ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Retain flag: %d \n " ) , _mqtt_retain ? 1 : 0 ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Keepalive time: %ds \n " ) , _mqtt_keepalive ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Will topic: %s \n " ) , _mqtt_will . c_str ( ) ) ;
# endif // SECURE_CLIENT != SECURE_CLIENT_NONE
_mqtt . connect ( ) ;
_mqtt . connect ( ) ;
# else // not MQTT_USE_ASYNC
# else // Using PubSubClient or Arduino-MQTT
bool response = true ;
bool verified = true ; // Connection verified
bool secure = false ; // Whether to use SSL
# if ASYNC_TCP_SSL_ENABLED
# if SECURE_CLIENT != SECURE_CLIENT_NONE
bool secure = getSetting ( " mqttUseSSL " , MQTT_SSL_ENABLED ) . toInt ( ) = = 1 ;
secure = getSetting ( " mqttUseSSL " , MQTT_SSL_ENABLED ) . toInt ( ) = = 1 ;
if ( secure ) {
if ( secure ) {
DEBUG_MSG_P ( PSTR ( " [MQTT] Using SSL \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Using SSL \n " ) ) ;
if ( _mqtt_client_secure . connect ( _mqtt_server . c_str ( ) , _mqtt_port ) ) {
// Use MFLN if configured and on BearSSL
# if SECURE_CLIENT == SECURE_CLIENT_BEARSSL
uint16_t requested_mfln = getSetting ( " mqttScMFLN " , MQTT_SECURE_CLIENT_MFLN ) . toInt ( ) ;
if ( requested_mfln ) {
bool supported = _mqtt_client_secure . probeMaxFragmentLength ( _mqtt_server . c_str ( ) , _mqtt_port , requested_mfln ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] MFLN buffer size %u supported: %s \n " ) , requested_mfln , supported ? " YES " : " NO " ) ;
if ( supported ) {
_mqtt_client_secure . setBufferSizes ( requested_mfln , requested_mfln ) ;
}
}
# endif
// Default verification: CA in case of BearSSL, fingerprint otherwise
int check = getSetting ( " mqttScCheck " , MQTT_SECURE_CLIENT_CHECK ) . toInt ( ) ;
if ( check = = SECURE_CLIENT_CHECK_FINGERPRINT ) {
DEBUG_MSG_P ( PSTR ( " [MQTT] Using fingerprint verification, trying to connect \n " ) ) ;
char fp [ 60 ] = { 0 } ;
char fp [ 60 ] = { 0 } ;
if ( sslFingerPrintChar ( getSetting ( " mqttFP " , MQTT_SSL_FINGERPRINT ) . c_str ( ) , fp ) ) {
if ( sslFingerPrintChar ( getSetting ( " mqttFP " , MQTT_SSL_FINGERPRINT ) . c_str ( ) , fp ) ) {
if ( _mqtt_client_secure . verify ( fp , _mqtt_server . c_str ( ) ) ) {
_mqtt . setClient ( _mqtt_client_secure ) ;
// BearSSL needs to have the fingerprint set prior to connecting
# if SECURE_CLIENT == SECURE_CLIENT_BEARSSL
_mqtt_client_secure . setFingerprint ( fp ) ; // Always returns true
# endif
if ( _mqtt_client_secure . connect ( _mqtt_server . c_str ( ) , _mqtt_port ) ) {
// AxTLS does the fingerprint check *after* connecting
# if SECURE_CLIENT == SECURE_CLIENT_AXTLS
if ( ! _mqtt_client_secure . verify ( fp , _mqtt_server . c_str ( ) ) ) {
DEBUG_MSG_P ( PSTR ( " [MQTT] Fingerprint did not match \n " ) ) ;
verified = false ;
}
# endif
} else {
} else {
DEBUG_MSG_P ( PSTR ( " [MQTT] Invalid fingerprint \n " ) ) ;
response = false ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Client connection failed, incorrect fingerprint? \n " ) ) ;
verified = false ;
}
}
_mqtt_client_secure . stop ( ) ;
yield ( ) ;
} else {
} else {
DEBUG_MSG_P ( PSTR ( " [MQTT] Wrong fingerprint \n " ) ) ;
response = false ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Invalid fingerprint, cannot connect \n " ) ) ;
verified = false ;
}
}
} else if ( check = = SECURE_CLIENT_CHECK_CA ) {
# if SECURE_CLIENT == SECURE_CLIENT_BEARSSL
# ifndef _mqtt_client_ca
DEBUG_MSG_P ( PSTR ( " [MQTT] No CA certificate defined, cannot connect \n " ) ) ;
verified = false ;
# else
DEBUG_MSG_P ( PSTR ( " [MQTT] Using CA verification, trying to connect \n " ) ) ;
// We need to allocate using new in order to keep the list in memory
_ca_list = new BearSSL : : X509List ( _mqtt_client_ca ) ;
// If NTP is not synced yet, the connect() call may fail.
// This is not an issue, MQTT will reconnect after MQTT_RECONNECT_DELAY_MIN
# if NTP_SUPPORT
_mqtt_client_secure . setX509Time ( ntpLocal2UTC ( now ( ) ) ) ;
# else
_mqtt_client_secure . setX509Time ( now ( ) ) ;
# endif
_mqtt_client_secure . setTrustAnchors ( _ca_list ) ;
if ( ! _mqtt_client_secure . connect ( _mqtt_server . c_str ( ) , _mqtt_port ) ) {
DEBUG_MSG_P ( PSTR ( " [MQTT] CA verification failed - possible reasons are an incorrect certificate or unsynced clock \n " ) ) ;
verified = false ;
}
# endif // defined _mqtt_client_ca
# else
DEBUG_MSG_P ( PSTR ( " [MQTT] CA verification is not supported with AxTLS client, cannot connect \n " ) ) ;
verified = false ;
# endif
} else {
} else {
DEBUG_MSG_P ( PSTR ( " [MQTT] Client connection failed \n " ) ) ;
response = false ;
}
DEBUG_MSG_P ( PSTR ( " [MQTT] !!! SSL connection will not be validated !!! \n " ) ) ;
} else {
_mqtt . setClient ( _mqtt_client ) ;
# if SECURE_CLIENT == SECURE_CLIENT_BEARSSL
_mqtt_client_secure . setInsecure ( ) ;
# endif
}
}
}
# else // not ASYNC_TCP_SSL_ENABLED
_mqtt . setClient ( _mqtt_client ) ;
# endif // SECURE_CLIENT != SECURE_CLIENT_NONE
# endif // ASYNC_TCP_SSL_ENABLED
if ( response ) {
_mqtt . setServer ( _mqtt_server . c_str ( ) , _mqtt_port ) ;
if ( _mqtt_user . length ( ) & & _mqtt_pass . length ( ) ) {
DEBUG_MSG_P ( PSTR ( " [MQTT] Connecting as user %s \n " ) , _mqtt_user . c_str ( ) ) ;
response = _mqtt . connect ( _mqtt_clientid . c_str ( ) , _mqtt_user . c_str ( ) , _mqtt_pass . c_str ( ) , _mqtt_will . c_str ( ) , _mqtt_qos , _mqtt_retain , " 0 " ) ;
} else {
response = _mqtt . connect ( _mqtt_clientid . c_str ( ) , _mqtt_will . c_str ( ) , _mqtt_qos , _mqtt_retain , " 0 " ) ;
}
DEBUG_MSG_P ( PSTR ( " [MQTT] Client ID: %s \n " ) , _mqtt_clientid . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] QoS: %d \n " ) , _mqtt_qos ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Retain flag: %d \n " ) , _mqtt_retain ? 1 : 0 ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Keepalive time: %ds \n " ) , _mqtt_keepalive ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Will topic: %s \n " ) , _mqtt_will . c_str ( ) ) ;
if ( verified ) {
# if MQTT_LIBRARY == MQTT_ARDUINO // Arduino-MQTT
_mqtt . begin ( _mqtt_server . c_str ( ) , _mqtt_port , ( secure ? _mqtt_client_secure : _mqtt_client ) ) ;
_mqtt . setWill ( _mqtt_will . c_str ( ) , MQTT_STATUS_OFFLINE , _mqtt_qos , _mqtt_retain ) ;
verified = _mqtt . connect ( _mqtt_clientid . c_str ( ) , _mqtt_user . c_str ( ) , _mqtt_pass . c_str ( ) ) ;
# else // PubSubClient
_mqtt . setClient ( secure ? _mqtt_client_secure : _mqtt_client ) ;
_mqtt . setServer ( _mqtt_server . c_str ( ) , _mqtt_port ) ;
if ( _mqtt_user . length ( ) & & _mqtt_pass . length ( ) ) {
DEBUG_MSG_P ( PSTR ( " [MQTT] Connecting as user %s \n " ) , _mqtt_user . c_str ( ) ) ;
verified = _mqtt . connect ( _mqtt_clientid . c_str ( ) , _mqtt_user . c_str ( ) , _mqtt_pass . c_str ( ) , _mqtt_will . c_str ( ) , _mqtt_qos , _mqtt_retain , MQTT_STATUS_OFFLINE ) ;
} else {
verified = _mqtt . connect ( _mqtt_clientid . c_str ( ) , _mqtt_will . c_str ( ) , _mqtt_qos , _mqtt_retain , MQTT_STATUS_OFFLINE ) ;
}
# endif
}
}
if ( response ) {
if ( verified ) {
_mqttOnConnect ( ) ;
_mqttOnConnect ( ) ;
} else {
} else {
DEBUG_MSG_P ( PSTR ( " [MQTT] Connection failed \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Connection failed \n " ) ) ;
_mqtt_last_connection = millis ( ) ;
mqttDisconnect ( ) ; // Clean up
_mqtt_last_connection = millis ( ) ;
}
}
# endif // MQTT_USE _ASYNC
# endif // MQTT_LIBRARY == MQTT _ASYNC
}
}
@ -318,9 +399,9 @@ void _mqttBackwards() {
}
}
void _mqttInfo ( ) {
void _mqttInfo ( ) {
DEBUG_MSG_P ( PSTR ( " [MQTT] Async %s, SSL %s, Autoconnect %s \n " ) ,
MQTT_USE _ASYNC ? " ENABLED " : " DISABLED " ,
ASYNC_TCP_SSL_ENABLED ? " EN ABLED" : " DIS ABLED" ,
DEBUG_MSG_P ( PSTR ( " [MQTT] Library %s, SSL %s, Autoconnect %s \n " ) ,
( MQTT_LIBRARY = = MQTT _ASYNC ? " AsyncMqttClient " : ( MQTT_LIBRARY = = MQTT_ARDUINO ? " Arduino-MQTT " : " PubSubClient " ) ) ,
SECURE_CLIENT = = SECURE_CLIENT_NONE ? " DIS ABLED" : " EN ABLED" ,
MQTT_AUTOCONNECT ? " ENABLED " : " DISABLED "
MQTT_AUTOCONNECT ? " ENABLED " : " DISABLED "
) ;
) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Client %s, %s \n " ) ,
DEBUG_MSG_P ( PSTR ( " [MQTT] Client %s, %s \n " ) ,
@ -367,7 +448,7 @@ void _mqttWebSocketOnConnected(JsonObject& root) {
root [ " mqttKeep " ] = _mqtt_keepalive ;
root [ " mqttKeep " ] = _mqtt_keepalive ;
root [ " mqttRetain " ] = _mqtt_retain ;
root [ " mqttRetain " ] = _mqtt_retain ;
root [ " mqttQoS " ] = _mqtt_qos ;
root [ " mqttQoS " ] = _mqtt_qos ;
# if ASYNC_TCP_SSL_ENABLED
# if SECURE_CLIENT != SECURE_CLIENT_NONE
root [ " mqttUseSSL " ] = getSetting ( " mqttUseSSL " , MQTT_SSL_ENABLED ) . toInt ( ) = = 1 ;
root [ " mqttUseSSL " ] = getSetting ( " mqttUseSSL " , MQTT_SSL_ENABLED ) . toInt ( ) = = 1 ;
root [ " mqttFP " ] = getSetting ( " mqttFP " , MQTT_SSL_FINGERPRINT ) ;
root [ " mqttFP " ] = getSetting ( " mqttFP " , MQTT_SSL_FINGERPRINT ) ;
# endif
# endif
@ -552,10 +633,13 @@ String mqttTopic(const char * magnitude, unsigned int index, bool is_set) {
void mqttSendRaw ( const char * topic , const char * message , bool retain ) {
void mqttSendRaw ( const char * topic , const char * message , bool retain ) {
if ( _mqtt . connected ( ) ) {
if ( _mqtt . connected ( ) ) {
# if MQTT_USE_ASYNC
# if MQTT_LIBRARY == MQTT_ASYNC // AsyncMqttClient
unsigned int packetId = _mqtt . publish ( topic , _mqtt_qos , retain , message ) ;
unsigned int packetId = _mqtt . publish ( topic , _mqtt_qos , retain , message ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Sending %s => %s (PID %d) \n " ) , topic , message , packetId ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Sending %s => %s (PID %d) \n " ) , topic , message , packetId ) ;
# else
# elif MQTT_LIBRARY == MQTT_ARDUINO // Arduino-MQTT
_mqtt . publish ( topic , message , retain , _mqtt_qos ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Sending %s => %s \n " ) , topic , message ) ;
# else // PubSubClient
_mqtt . publish ( topic , message , retain ) ;
_mqtt . publish ( topic , message , retain ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Sending %s => %s \n " ) , topic , message ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Sending %s => %s \n " ) , topic , message ) ;
# endif
# endif
@ -720,10 +804,10 @@ int8_t mqttEnqueue(const char * topic, const char * message) {
void mqttSubscribeRaw ( const char * topic ) {
void mqttSubscribeRaw ( const char * topic ) {
if ( _mqtt . connected ( ) & & ( strlen ( topic ) > 0 ) ) {
if ( _mqtt . connected ( ) & & ( strlen ( topic ) > 0 ) ) {
# if MQTT_USE_ASYNC
# if MQTT_LIBRARY == MQTT_ASYNC // AsyncMqttClient
unsigned int packetId = _mqtt . subscribe ( topic , _mqtt_qos ) ;
unsigned int packetId = _mqtt . subscribe ( topic , _mqtt_qos ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Subscribing to %s (PID %d) \n " ) , topic , packetId ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Subscribing to %s (PID %d) \n " ) , topic , packetId ) ;
# else
# else // Arduino-MQTT or PubSubClient
_mqtt . subscribe ( topic , _mqtt_qos ) ;
_mqtt . subscribe ( topic , _mqtt_qos ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Subscribing to %s \n " ) , topic ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Subscribing to %s \n " ) , topic ) ;
# endif
# endif
@ -736,10 +820,10 @@ void mqttSubscribe(const char * topic) {
void mqttUnsubscribeRaw ( const char * topic ) {
void mqttUnsubscribeRaw ( const char * topic ) {
if ( _mqtt . connected ( ) & & ( strlen ( topic ) > 0 ) ) {
if ( _mqtt . connected ( ) & & ( strlen ( topic ) > 0 ) ) {
# if MQTT_USE_ASYNC
# if MQTT_LIBRARY == MQTT_ASYNC // AsyncMqttClient
unsigned int packetId = _mqtt . unsubscribe ( topic ) ;
unsigned int packetId = _mqtt . unsubscribe ( topic ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Unsubscribing to %s (PID %d) \n " ) , topic , packetId ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Unsubscribing to %s (PID %d) \n " ) , topic , packetId ) ;
# else
# else // Arduino-MQTT or PubSubClient
_mqtt . unsubscribe ( topic ) ;
_mqtt . unsubscribe ( topic ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Unsubscribing to %s \n " ) , topic ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Unsubscribing to %s \n " ) , topic ) ;
# endif
# endif
@ -769,6 +853,9 @@ void mqttDisconnect() {
DEBUG_MSG_P ( PSTR ( " [MQTT] Disconnecting \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Disconnecting \n " ) ) ;
_mqtt . disconnect ( ) ;
_mqtt . disconnect ( ) ;
}
}
# if SECURE_CLIENT == SECURE_CLIENT_BEARSSL
delete _ca_list ;
# endif
}
}
bool mqttForward ( ) {
bool mqttForward ( ) {
@ -802,7 +889,7 @@ void mqttSetup() {
_mqttBackwards ( ) ;
_mqttBackwards ( ) ;
_mqttInfo ( ) ;
_mqttInfo ( ) ;
# if MQTT_USE_ASYNC
# if MQTT_LIBRARY == MQTT_ASYNC // AsyncMqttClient
_mqtt . onConnect ( [ ] ( bool sessionPresent ) {
_mqtt . onConnect ( [ ] ( bool sessionPresent ) {
_mqttOnConnect ( ) ;
_mqttOnConnect ( ) ;
@ -823,7 +910,7 @@ void mqttSetup() {
if ( reason = = AsyncMqttClientDisconnectReason : : MQTT_NOT_AUTHORIZED ) {
if ( reason = = AsyncMqttClientDisconnectReason : : MQTT_NOT_AUTHORIZED ) {
DEBUG_MSG_P ( PSTR ( " [MQTT] Not authorized \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Not authorized \n " ) ) ;
}
}
# if ASYNC_TCP_SSL_ENABLED
# if SECURE_CLIENT == SECURE_CLIENT_AXTLS
if ( reason = = AsyncMqttClientDisconnectReason : : TLS_BAD_FINGERPRINT ) {
if ( reason = = AsyncMqttClientDisconnectReason : : TLS_BAD_FINGERPRINT ) {
DEBUG_MSG_P ( PSTR ( " [MQTT] Bad fingerprint \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Bad fingerprint \n " ) ) ;
}
}
@ -840,13 +927,19 @@ void mqttSetup() {
DEBUG_MSG_P ( PSTR ( " [MQTT] Publish ACK for PID %d \n " ) , packetId ) ;
DEBUG_MSG_P ( PSTR ( " [MQTT] Publish ACK for PID %d \n " ) , packetId ) ;
} ) ;
} ) ;
# else // not MQTT_USE_ASYNC
# elif MQTT_LIBRARY == MQTT_ARDUINO // Arduino-MQTT
_mqtt . onMessageAdvanced ( [ ] ( MQTTClient * client , char topic [ ] , char payload [ ] , int length ) {
_mqttOnMessage ( topic , payload , length ) ;
} ) ;
# else // PubSubClient
_mqtt . setCallback ( [ ] ( char * topic , byte * payload , unsigned int length ) {
_mqtt . setCallback ( [ ] ( char * topic , byte * payload , unsigned int length ) {
_mqttOnMessage ( topic , ( char * ) payload , length ) ;
_mqttOnMessage ( topic , ( char * ) payload , length ) ;
} ) ;
} ) ;
# endif // MQTT_USE_ASYNC
# endif // MQTT_LIBRARY == MQTT _ASYNC
_mqttConfigure ( ) ;
_mqttConfigure ( ) ;
mqttRegister ( _mqttCallback ) ;
mqttRegister ( _mqttCallback ) ;
@ -877,11 +970,11 @@ void mqttLoop() {
if ( WiFi . status ( ) ! = WL_CONNECTED ) return ;
if ( WiFi . status ( ) ! = WL_CONNECTED ) return ;
# if MQTT_USE _ASYNC
# if MQTT_LIBRARY == MQTT _ASYNC
_mqttConnect ( ) ;
_mqttConnect ( ) ;
# else // not MQTT_USE _ASYNC
# else // MQTT_LIBRARY != MQTT _ASYNC
if ( _mqtt . connected ( ) ) {
if ( _mqtt . connected ( ) ) {
@ -898,7 +991,7 @@ void mqttLoop() {
}
}
# endif
# endif // MQTT_LIBRARY == MQTT_ASYNC
}
}