communicate between pika and paho-mqtt python

425 views
Skip to first unread message

andy li

unread,
Oct 13, 2021, 2:40:27 AM10/13/21
to rabbitmq-users
Hi guys,

I'm new to rabittmq.
I am trying to set up rabbitmq for paho-mqtt with python.
First I tried to use rabbitmq to listen to paho-mqtt while send and receive within paho-matt, which couldn't be done.
Second I tried to communicate between pika and paho-mqtt, in which pika is not receiving any messages. I enabled the mqtt plugin and sever is running.
It would be appreciated for both situations.  For the second situation, below is the code:
publisher.py:
'''
mqtt through rabbitmq
'''
#import paho.mqtt.publish as publish
import paho.mqtt.client as mqtt
import random, time, json
from datetime import datetime
 
mqttBrokerHost = '127.0.0.1'
mqttBrokerPort = 1883
mqttUser = 'guest'
mqttPassword = 'guest'
mqttTopic = 'generated_numbers'
mqttClientId = 'Numbers'

mqtt_client = mqtt.Client(mqttClientId)
mqtt_client.username_pw_set(mqttUser, mqttPassword)
mqtt_client.connect(mqttBrokerHost, mqttBrokerPort)


while True:
    rand_int = random.randint(1, 100)
    now = datetime.now().isoformat()
    MQTT_MSG = json.dumps({'TIME_STAMP': now,
                           'NUMBER': rand_int})
    mqtt_client.publish(mqttTopic, MQTT_MSG)
    print(MQTT_MSG)
    time.sleep(1)

receiver.py:
'''
mqtt through rabbitmq
'''
import pika, sys, json, time

mqttBrokerHost = '127.0.0.1'
mqttBrokerPort = 1883
mqttUser = 'guest'
mqttPassword = 'guest'
mqttTopic = 'generated_numbers'
mqttClientId = 'Numbers'

# set up the credentials same as mqtt publisher
user_pwd = pika.PlainCredentials(mqttUser, mqttPassword)

# define exchange info
exchangeTopic = 'amq.topic'
exchangeType = 'topic'

# set up connections in rabbitmq
connection = pika.BlockingConnection(
    pika.ConnectionParameters(
        host=mqttBrokerHost,
        #credentials=user_pwd
    ))
channel = connection.channel()

# declare exchange info
channel.exchange_declare(
    exchange=exchangeTopic, exchange_type=exchangeType, durable=True
)

# declare queue, delete the queue when consumer quits
result = channel.queue_declare(queue='', exclusive=True)
queue_name = result.method.queue

# binding keys
binding_key = mqttTopic

# binding queues
channel.queue_bind(
    exchange=exchangeTopic, queue=queue_name, routing_key=binding_key)

# set up callback
def callback(ch, method, properties, body):
    print(' [x] Received %r' % body.decode())
    #ch.basic_ack(delivery_tag=method.delivery_tag)

#channel.basic_qos(prefetch_count=1)
channel.basic_consume(queue=queue_name,
                      # auto_ack: redeliver if not consumed
                      # auto_ack=True, # no message acknowledgment with True
                      on_message_callback=callback)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()


   


#print(rand_int)
#publish.single('paho/test/single', hostname="localhost", port=1883)
#client.publish('RANDOM_NUMBER', rand_int)
#print('Published ' + str(rand_int) + ', topic RANDOM_NUMBER')
'''
import rabbitpy
creates a queue named mqtt-messages,  
binds it to the amq.topic,
exchange using the routing key #.

with rabbitpy.Connection() as connection:
    with connection.channel() as channel:
        queue = rabbitpy.Queue(channel, 'mqtt-messages')
        queue.declare()
        queue.bind('amq.topic', '#')
'''





Reply all
Reply to author
Forward
0 new messages