Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Paho » on_log callback not being triggered(Python paho.MQTT on_log callback not being triggered)
on_log callback not being triggered [message #1849551] Mon, 24 January 2022 18:12
Gary Prade is currently offline Gary PradeFriend
Messages: 1
Registered: January 2022
Junior Member
env: Python 3.9 paho.MQTT on RasPi OS or Mac OS 12.1:

Trying to get on_log set up for debugging a periodic connection issue, but the callback isn't being triggered. All other callbacks are working as expected. I added an enable_logging and it works but I'd like to use the callback to control the volume of log entries. The pertinent code is below. Thanks.

class MQ:
    """
    Container and methods for MQ related tasks
    """

    def __init__(self):
        self.connected = False
        self.client = None
        self.msg_time = datetime(2000, 1, 1)
        self.mq_topics = {
        }

        # Instantiate MQ and set up callbacks
        self.client = mqtt.Client()
        self.client.on_message = self.on_message
        self.client.on_connect = self.on_connect
        self.client.on_publish = self.on_publish
        self.client.on_subscribe = self.on_subscribe
        self.client.on_log = self.on_log
        logger.debug("MQ client instantiation complete")
        self.client.enable_logger(logger=logger)

        # Issue connect request to the MQ broker.  This will loop forever waiting for the broker to become available
        logger.debug("Waiting for MQ broker to become available")
        while not self.connected:
            try:
                self.client.connect("192.168.0.18", 1884, 30)
            except IOError as e:
                if e.errno != ENETUNREACH:
                    raise
                logger.warning('Network error - retrying')
                sleep(5)
                continue
            except socket.timeout as e:
                logger.warning("Socket timeout trying to connect to MQTT broker - will retry after delay")
                sleep(30)
                continue
            logger.debug('Connect to MQ broker initiated')
            break
        logger.debug("Connect request successfully issued to MQ broker")

        # Start processing MQ messages/responses to us
        self.client.loop_start()
        logger.debug("MQ processing loop started")
        # This will loop forever until the connection to the MQ broker is complete
        logger.debug("Waiting for MQTT broker to connect")
        while not self.connected:
            sleep(1)
        logger.debug("Successfully connected to MQ broker")

    def subscribe(self, topic):
        self.client.subscribe(topic, 0)
        logger.debug("Subscribe request for " + topic)

    def stop(self):
        # Stop the MQ processing of messages/responses
        self.client.loop_stop()
        logger.debug("MQ processing loop terminated")
        self.client.disconnect()
        logger.info("MQ disconnected from broker")

    def on_connect(self, mq_client, obj, flags, rc):
        if rc == 0:
            logger.debug("(Re)connected to HomeAssist MQ broker")
            self.connected = True
        else:
            logger.warning("Error connecting to MQTT broker; rc={}".format(rc))

    def on_disconnect(self, mq_client, obj, flags, rc):
        logger.debug("MQ Disconnected: rc = " + str(rc))

    def on_message(self, mq_client, obj, msg):
        global MQ_topics

        value_string = msg.payload.decode('utf-8')
        topics = msg.topic.split('/')
        logger.debug(f"MQ msg: topic={msg.topic}, value={value_string}")
# Message processing removed for brevity


    def on_publish(self, mq_client, obj, mid):
        logger.debug("Publish confirmed: id = " + str(mid))

    def on_subscribe(self, mq_client, obj, mid, granted_qos):
        logger.debug("Subscribed: id = " + str(mid) + ", qos = " + str(granted_qos))

    def on_log(self, mq_client, obj, level, string):
        # verb = string.split('(').strip()
        # if verb[0] in ['Sending PINGREQ', 'Received PINGRESP', 'Received PUBLISH']:
        #   return
        logger.debug('LOG: {}'.format(string))
# end class MQ


# Instantiation of MQ in main
    MQ_client = MQ()
    MQ_client.subscribe("Tempest/outTemp_F")

Previous Topic:Illegal reflective access by org.eclipse.paho.client.mqttv3.internal.FileLock went away in JDK 17?
Next Topic:RPi Python MQTT - occasional errors
Goto Forum:
  


Current Time: Fri May 03 07:28:28 GMT 2024

Powered by FUDForum. Page generated in 0.03018 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top