Search code examples
azuremqttubuntu-18.04node-redmosquitto

Configuring Mosquitto on VM to Connect to Azure WebApp


I am running the MQTT mosquitto broker (v.1.4.15) on a Azure Virtual Machine running Ubuntu (v18.04.1). My aim is to connect multiple microcontrollers to the broker. In order to monitor and control the microcontrollers remotely, I've setup an Azure WebApp on which I am running Node-RED. I am trying to establish a connection between the web-app and the broker. In order to configure Mosquitto properly, I am using this tutorial from DigitalOcean. However, because I'm new to this part of the work (usually working on embeded systems) I'm trying to establish a connection without SSL first for testing and then add SSL.

I've password protected mosquitto and am able to pub and sub on a test topic on local host (using the following commands on the VM terminal)

$ mosquitto_pub -h localhost -t "test" -m "message" -u "user" -P "password"
$ mosquitto_sub -h localhost -t "test" -u "user" -P "password"

I've also configured mosquitto to listen to the port 8883 on the VM: /etc/mosquitto/conf.d/default.conf

allow_anonymous false
password_file /etc/mosquitto/passwd

listener 1883 localhost

listener 8883

Lastly, I've updated the firwall on the VM to allow connections to port 8883.

$ sudo ufw allow 8883

However, when I try connecting an MQTT node on the WebApp on Node-RED to the broker, the status remains "connecting". Is there a way to test this other than on the WebApp (maybe through the cmd prompt) or am I missing somehting?

Furthermore, when restarting mosquitto (after the edit to the config file and to the ufw) and running mosquitto:

$ sudo systemctl restart mosquitto
$ mosquitto

I get the following message from the terminal indicating that mosquitto is only listening to port 1883.

1610290040: mosquitto version 1.4.15 (build date Tue, 18 Jun 2019 11:42:22 -0300) starting
1610290040: Using default config.
1610290040: Opening ipv4 listen socket on port 1883.
1610290040: Error: Address already in use

Solution

  • To follow up on the first comment.

    You need to open the port in the Azure portal as well as in ufw

    Details of how can be found here:

    https://learn.microsoft.com/en-us/azure/virtual-machines/windows/nsg-quickstart-portal