Sojimanatsu
Sojimanatsu

Reputation: 601

MQTT Broker Connection Refused Error in Python

I have been trying to connect to broker port 1884. But it gets every time connection refused error.

    def __init__(self, host='127.0.0.1', port=8080, broker_address='127.0.0.1', broker_port=1884, start_config=None):
    self.app = Bottle()
    """Create the http service app"""

    self.broker = SimpleBroker(address=broker_address, port=broker_port)
    self.broker.start()
    """Create and run the broker (broker always runs as a daemon)"""

    self.brokerAddress = broker_address
    """address of the API broker - usually identical to own address, but others for testing"""

    self.mqttc = Client()
    """mqtt client for publishing messages to API broker - delivered to edge and preprocessing layer"""

    self.mqttc.connect(host=broker_address, port=broker_port)
    """connect to the broker upon startup"""

and then the execution part:

if __name__ == "__main__":
myAPI = APIServer('127.0.0.1', 8080, '127.0.0.1', start_config='all wheels')
myAPI.start()

The error message:

Traceback (most recent call last):
File "/root/PycharmProjects/Thesis2019/API/APIServer.py", line 328, in <module>
myAPI = APIServer('127.0.0.1', 8080, '127.0.0.1', start_config='all wheels')
File "/root/PycharmProjects/Thesis2019/API/APIServer.py", line 42, in __init__
self.mqttc.connect(host=broker_address, port=broker_port)
File "/root/PycharmProjects/Thesis2019/venv/lib/python3.6/site-packages/paho/mqtt/client.py", line 839, in connect
return self.reconnect()
File "/root/PycharmProjects/Thesis2019/venv/lib/python3.6/site-packages/paho/mqtt/client.py", line 962, in reconnect
sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0))
File "/usr/lib/python3.6/socket.py", line 724, in create_connection
raise err
File "/usr/lib/python3.6/socket.py", line 713, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

On the other hand when i execute it in debug mode, i got :

[2019-04-09 23:43:05,243] :: WARNING :: hbmqtt.broker.plugins.topic_taboo   :: 'topic-check' section not found in context configuration
[2019-04-09 23:43:05,243] :: INFO :: transitions.core :: Exited state new
[2019-04-09 23:43:05,244] :: INFO :: transitions.core :: Entered state starting
[2019-04-09 23:43:05,248] :: INFO :: hbmqtt.broker :: Listener 'default' bind to 127.0.0.1:1884 (max_connections=100)
[2019-04-09 23:43:05,248] :: INFO :: transitions.core :: Exited state starting
[2019-04-09 23:43:05,248] :: INFO :: transitions.core :: Entered state started

Additional to all the connection refused part, it just starts it without problem in debug mode i guess?

It works on Windows, but not on Mac or Linux

Why i have connection refused error each time i try to execute it ? What am i missing?

Thanks in advance for the ideas.

Upvotes: 2

Views: 5631

Answers (1)

Sojimanatsu
Sojimanatsu

Reputation: 601

The solution is:

The Mosquitto default port connection is set to 1883 in the config file, where I was trying to connect through port 1884.

I thought that, this properties are set whenever you change the number. Which is not correct since they are static. not changing dynamically.(sure there is a way too.)

Apart from that, the API Broker and Broker port numbers have to be swapped. I don't know why but windows can bind on top of it without problems. but Mac OS and Linux are throwing Connection Refused error.

  • The Pycharm is launched with root privilege.
  • Firewall is checked, and it is disabled.
  • None of these ports are open, checked it with lsof.
  • Check config file of the Mosquitto, if the allow_annoymous is True
  • Control your port connections 10 times.

These are still valid check before going crazy. %90 of the problems are caused because of one of these issues.

If you have further help or questions, you can comment in here and I will try to help for future inquiries.

Upvotes: 1

Related Questions