1

I have a Mosquitto MQTT broker (mosquitto version 2.0.11) running on a Raspberry Pi.

It is publishing regular messages on a specific topic, which can be seen by running

$  mosquitto_sub -h 192.168.27.177 -d -t HouseMon -t notification

from another Raspberry Pi.

I'm writing a Python script with an MQTT client to do more processing on the messages. I'm trying the Paho MQTT client. I've reviewed Steve's Internet, and this older article here

I can connect and subscribe, but the client never receives a message. The on_connect callback works, but apparently the on_message callback does not. The loop_start() function has been called, and works for the on_connect callback.

Below is my code - based on the examples shown on steves-internet-guide.com. (Note - I've been through the tutorial, but have never seen a complete example of working code - only snippets. Did I miss it?)

This code remains in the while not q.empty(): loop indefinitely waiting for a message. The print statements in the on_message callback are never executed either.

Wireshark on the client machine's interface captures the MQTT Connect command, the MQTT Connect Ack, the Subscribe Request, and the Subscribe Ack. After that, there is no MQTT traffic. Wireshark Capture

import paho.mqtt.client as mqtt
from queue import Queue
import time
import uuid
import random


# MQTT config
broker_address = "192.168.27.177" 
mqtt_topic = "HouseMon"
message_received = "none"
client_id = f'python-mqtt-hm-{random.randint(0, 1000)}'

#client_connected_flag = False

q=Queue()


def on_message_do(myclient, userdata, message):
    global message_received
    message_received = str(message.payload.decode("utf-8"))
    q.put(message_received) #put messages on queue
    
    print("message received " ,message_received)
    print("message topic=",message.topic)
    print("message qos=",message.qos)
    print("message retain flag=",message.retain)

def on_connect_do(myclient, userdata, flags, rc):
    print("Connected flags "+str(flags)+" result code "+str(rc))
    myclient.connected_flag=True

def on_subscribed_do(myclient, userdata, mid, granted_qos):
    print("Subscribed -  callback mid "+str(mid)+" qos "+str(granted_qos))
    myclient.subscribed_flag=True
    

print (client_id)
mqtt.Client.connected_flag = False #create flag in class
mqtt.Client.subscribed_flag = False #create flag in class
myclient = mqtt.Client(client_id)

myclient.on_connect = on_connect_do   # define callback
myclient.loop_start() #start the loop
print("connecting to broker at ",broker_address)
myclient.connect(broker_address, port=1883, keepalive=60, bind_address="")   # connect to broker

timer = 0
print("waiting for connect to broker..")
while not myclient.connected_flag:
    time.sleep(1)
    timer += 1
    print(timer,end=" ",flush=True)


print("Subscribing to topic",mqtt_topic)
myclient.on_message = on_subscribed_do        #attach subscribed function to callback
myclient.on_message = on_message_do        #attach message function to callback
(subresult, submid) = myclient.subscribe(mqtt_topic)

if subresult == 0:   # 0 is success 
    print("Subscribe succeeded with result "+str(subresult)+"  mid "+str(submid))
else:
    print("Subscribe failed with result "+str(subresult)+"  mid "+str(submid))
    

timer = 0
try:
    print("Enter message queue loop")
    while True:
        while not q.empty():
            qmessage = q.get()
            print("queued: ",qmessage)
        time.sleep(1)
        timer += 1
        print(timer,end=" ",flush=True)
        
except:    
    myclient.loop_stop() #stop the loop
    print ("\n message_received: |",message_received,"|")

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.