1

I am trying to connect a paho mqtt client to vernemq message broker on the virtualbox running ubuntu. I am not able to publish a message to the broker and subsequently read the message by subscribing to the same topic. When I try to run the program I dont get any error or confirmation of "connected with result code rc". I have tried the same thing on a local ubuntu machine and it works. Below are my vernemq.conf file and client.py file for publishing and subscribing.

accept_eula = yes
allow_anonymous = on
allow_register_during_netsplit = off
allow_publish_during_netsplit = off
allow_subscribe_during_netsplit = off
allow_unsubscribe_during_netsplit = off
allow_multiple_sessions = off
coordinate_registrations = on
max_inflight_messages = 20
max_online_messages = 1000
max_offline_messages = 1000
max_message_size = 0
upgrade_outgoing_qos = off
listener.max_connections = 10000
listener.nr_of_acceptors = 10
listener.tcp.default = 127.0.0.1:1883
listener.vmq.clustering = 0.0.0.0:44053
listener.http.default = 127.0.0.1:8888
systree_enabled = on
systree_interval = 20000
graphite_enabled = off
graphite_host = localhost
graphite_port = 2003
graphite_interval = 20000
shared_subscription_policy = prefer_local
plugins.vmq_passwd = off
plugins.vmq_acl = on
plugins.vmq_diversity = off
plugins.vmq_webhooks = off
plugins.vmq_bridge = off
metadata_plugin = vmq_plumtree
vmq_acl.acl_file = /etc/vernemq/vmq.acl
vmq_acl.acl_reload_interval = 10
vmq_passwd.password_file = /etc/vernemq/vmq.passwd
vmq_passwd.password_reload_interval = 10
vmq_diversity.script_dir = /usr/share/vernemq/lua
vmq_diversity.auth_postgres.enabled = off
vmq_diversity.postgres.ssl = off
vmq_diversity.postgres.password_hash_method = crypt
vmq_diversity.auth_cockroachdb.enabled = off
vmq_diversity.cockroachdb.ssl = on
vmq_diversity.cockroachdb.password_hash_method = bcrypt
vmq_diversity.auth_mysql.enabled = off
vmq_diversity.mysql.password_hash_method = password
vmq_diversity.auth_mongodb.enabled = off
vmq_diversity.mongodb.ssl = off
vmq_diversity.auth_redis.enabled = off
vmq_bcrypt.pool_size = 1
log.console = file
log.console.level = info
log.console.file = /var/log/vernemq/console.log
log.error.file = /var/log/vernemq/error.log
log.syslog = off
log.crash = on
log.crash.file = /var/log/vernemq/crash.log
log.crash.maximum_message_size = 64KB
log.crash.size = 10MB
log.crash.rotation = $D0
log.crash.rotation.keep = 5
nodename = VerneMQ@127.0.0.1
distributed_cookie = vmq
erlang.async_threads = 64
erlang.max_ports = 262144
leveldb.maximum_memory.percent = 70
vmq_bridge.tcp.br0 = a--------------.iot.eu-east-1.amazonaws.com:8883
vmq_bridge.tcp.br0.try_private = off
vmq_bridge.tcp.br0.topic.1 = /mytopic/+ both 0
vmq_bridge.tcp.br0.cleansession = on
vmq_bridge.ssl.br0.cafile = /etc/vernemq/keys/ca.pem
vmq_bridge.ssl.br0.certfile = /etc/vernemq/keys/cert.crt
vmq_bridge.ssl.br0.keyfile =  /etc/vernemq/keys/private.pem
vmq_bridge.ssl.br0.insecure = off

client.py

import paho.mqtt.client as mqtt
import time

def on_connect(client, userdata, rc):
    print("Connected with result code "+str(rc))
    client.subscribe("mytopic/image")

def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))


client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("127.0.0.1", 1883, 60)

while True:
    client.publish("mytopic/image", "hellotest", qos=0) 
    time.sleep(60)

client.loop_forever()

Can someone point out what I am doing wrong over here. My ultimate aim is to bridge this vernemq broker with AWS IoT. Thanks!

craig
  • 41
  • 4

2 Answers2

1

You will never reach the client.loop_forever() as you'll never exit the while (True) loop before it.

Change client.loop_forever() to client.start_loop() and move it before the while (True) loop.

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • 1
    I moved client.loop_start() above the while loop. But still I am not able to publish or subscribe – craig Jan 29 '20 at 02:53
0

If you are running inside VirtualBox and the script in the host machine, you will not be able to reach VerneMQ on 127.0.0.1, and you need to get the IP of the VM and connect to that with:

VBoxManage guestproperty get <vmname> "/VirtualBox/GuestInfo/Net/0/V4/IP"

Refer to https://www.virtualbox.org/manual/ch08.html

codeadict
  • 2,643
  • 1
  • 15
  • 11