Skip to content

MQTT Set-UP

Setting Up MQTT Broker on Ubuntu, Windows, and MacOS

Introduction

MQTT (Message Queuing Telemetry Transport) is a lightweight, publish-subscribe network protocol that transports messages between devices. Setting up an MQTT broker on Ubuntu, Windows, or MacOS involves installing and configuring the MQTT software, such as Mosquitto, a popular open-source MQTT broker.

The URL below will take you to the direct website where you can download MQTT

https://mqttx.app/docs/downloading-and-installation

Common Requirements

  • Internet Connection: For downloading the necessary software.
  • Terminal Access: For executing commands in Ubuntu and MacOS.
  • Command Prompt or PowerShell: For Windows.

Setting Up MQTT on Ubuntu

Step 1: Install Mosquitto

i) Update Package List:

bashCopy code
sudo apt-get update

ii) Install Mosquitto:

bashCopy code
sudo apt-get install mosquitto mosquitto-clients

Step 2: Configure Mosquitto

i) Open Configuration File:

bashCopy code
sudo nano /etc/mosquitto/mosquitto.conf

ii) Edit Configurations (Optional):

  • Customize settings like port, persistence, and logging.
  • Save and exit the editor.

Step 3: Start the Mosquitto Service

i) Start Mosquitto:

bashCopy code
sudo systemctl start mosquitto

ii) Enable Mosquitto on Boot:

bashCopy code
sudo systemctl enable mosquitto

Step 4: Verify Installation

i) Check Status:

bashCopy code
sudo systemctl status mosquitto

Setting Up MQTT on Windows

Step 1: Download and Install Mosquitto

i) Download: Go to the Mosquitto official website and download the latest version for Windows.

ii) Install: Run the installer and follow the on-screen instructions.

Step 2: Configure Mosquitto

i) Navigate to Mosquitto Directory:

  • Open Command Prompt and navigate to the Mosquitto installation directory.

ii) Edit Configurations (Optional):

  • Edit mosquitto.conf to customize settings.

Step 3: Run Mosquitto

i) Start Mosquitto Broker:

  • In Command Prompt, run:

    cmdCopy code
    mosquitto -v -c mosquitto.conf
    

Step 4: Verify Installation

i) Check Connection:

  • Use a MQTT client to test the connection to the broker.

Setting Up MQTT on MacOS

Step 1: Install Mosquitto using Homebrew

i) Install Homebrew (if not installed):

bashCopy code
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

ii) Install Mosquitto:

bashCopy code
brew install mosquitto

Step 2: Configure Mosquitto

i) Edit Configuration File:

  • Use a text editor to edit /usr/local/etc/mosquitto/mosquitto.conf.

Step 3: Start Mosquitto

ii) Start Mosquitto Broker:

bashCopy code
mosquitto -c /usr/local/etc/mosquitto/mosquitto.conf

Step 4: Verify Installation

i) Check Broker Status:

  • Use a MQTT client to verify the broker is running.

EC2

SETTING UP MQTT on EC2

Setting up an MQTT broker on an Amazon EC2 instance involves several steps, including launching the EC2 instance, installing the MQTT broker software (like Mosquitto), and configuring the necessary network settings. Here's a step-by-step guide:

Step 1: Launch an EC2 Instance

i) Log in to AWS Console: Go to the AWS Management Console and log in.

ii) Launch Instance:

  • Navigate to the EC2 dashboard.
  • Click on "Launch Instance".
  • Choose an Amazon Machine Image (AMI), such as Ubuntu Server.
  • Select an instance type (e.g., t2.micro for testing purposes).
  • Configure instance details, add storage, and add tags as needed.
  • Configure Security Group:
    • Create a new security group.
    • Set rules to allow inbound traffic on TCP ports 1883 (standard MQTT) and 8883 (MQTT over TLS), and SSH (port 22) for server access.
  • Review and launch the instance.
  • Select or create a new key pair for SSH access.

Step 2: Connect to Your EC2 Instance

i) SSH into Your Instance:

  • Use the key pair downloaded during setup.
  • SSH command :

    bashCopy code
    
    ssh -i ~/.ssh/id_rsa ubuntu@3.71.202.151
    

Step 3: Install MQTT Broker (e.g., Mosquitto)

i) Update Packages (Ubuntu/Debian):

bashCopy code
sudo apt-get update
sudo apt-get upgrade

ii) Install Mosquitto:

bashCopy code
sudo apt-get install mosquitto mosquitto-clients

Step 4: Configure Mosquitto

i) Edit Configuration File:

  • Open Mosquitto configuration file in a text editor:

    bashCopy code
    sudo nano /etc/mosquitto/mosquitto.conf
    
  • You can configure settings like listener ports, persistence, and security settings.

ii) Restart Mosquitto Service:

bashCopy code
sudo systemctl restart mosquitto

Step 5: Adjust Security Group and Network ACLs

i) Security Group:

  • Ensure your EC2 instance's security group allows inbound traffic on the MQTT ports (1883 and 8883).

ii) Network ACLs:

  • Adjust your VPC's network access control lists (ACLs) if necessary to allow MQTT traffic.

Step 6: Testing MQTT Broker

i) Test Locally on EC2 Instance:

  • Use Mosquitto clients to publish and subscribe to test messages:

    bashCopy code
    mosquitto_sub -h localhost -t test
    mosquitto_pub -h localhost -t test -m "Hello MQTT"
    

ii) Test Remotely:

  • Use an MQTT client on a different machine and connect to your EC2 instance's public IP on port 1883.

OPEN AND PROTECTED MQTT

SETTING UP OPEN MQTT AND PROTECTED MQTT

Setting up both an open (unencrypted) and protected (encrypted) MQTT broker typically involves configuring an MQTT broker like Mosquitto to handle both types of connections. Here's a step-by-step guide to set this up, assuming you're using Mosquitto as your MQTT broker:

Pre-requisites

  • A server or computer where you can install and run Mosquitto.
  • Basic knowledge of terminal or command-line interface.
  • (For protected MQTT) SSL/TLS certificates. You can generate self-signed certificates or obtain them from a certificate authority.

Setting Up Open MQTT Broker

The open MQTT broker refers to a broker that does not use SSL/TLS for client connections. It's simpler to set up:

i) Install Mosquitto:

  • On Ubuntu/Debian:

    bashCopy code
    sudo apt-get update
    sudo apt-get install mosquitto mosquitto-clients
    
  • On Windows or MacOS, download the installer from Mosquitto’s official website.

ii) Basic Configuration:

  • Open the Mosquitto configuration file in a text editor:

    bashCopy code
    sudo nano /etc/mosquitto/mosquitto.conf
    
  • Ensure the listener is set to a standard MQTT port (default is 1883):

    yamlCopy code
    listener 1883
    
  • Save and close the file.

iii) Restart Mosquitto:

bashCopy code
sudo systemctl restart mosquitto

iv) Firewall Settings:

  • Make sure your firewall allows inbound connections on the MQTT port (1883).

Setting Up Protected MQTT Broker

A protected MQTT broker uses SSL/TLS to encrypt data transmitted between clients and the broker. This setup requires SSL/TLS certificates.

i) Generate SSL/TLS Certificates:

  • You can use OpenSSL to create a self-signed certificate or obtain one from a certificate authority.
  • Example to create a self-signed certificate:

    bashCopy code
    openssl req -new -x509 -days 365 -extensions v3_ca -keyout ca.key -out ca.crt
    openssl req -new -out server.csr -keyout server.key
    openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 365
    

ii) Configure Mosquitto for SSL/TLS:

  • Open the Mosquitto configuration file:

    bashCopy code
    sudo nano /etc/mosquitto/mosquitto.conf
    
  • Add or update the following lines to enable SSL/TLS:

    vbnetCopy code
    listener 8883
    cafile /path/to/ca.crt
    certfile /path/to/server.crt
    keyfile /path/to/server.key
    
  • Replace /path/to/ with the actual paths to your certificate files.

  • Save and close the file.

iii) Restart Mosquitto:

bashCopy code
sudo systemctl restart mosquitto

iv) Firewall Settings:

  • Ensure your firewall allows inbound connections on port 8883 (the default port for MQTT over SSL/TLS).

Testing Your Setup

i) Test Open MQTT Connection:

  • Use an MQTT client to connect to your broker on port 1883.

ii) Test Protected MQTT Connection:

  • Use an MQTT client that supports SSL/TLS to connect to port 8883.
  • You may need to provide the CA certificate file in the client configuration.

SUMMARY

Mqtt for ubuntu, window and macos

With Mosquitto installed and running on your Ubuntu, Windows, or MacOS system, you now have a functioning MQTT broker. This setup allows for testing and development of MQTT-based applications and systems. Remember to configure your firewall and network settings appropriately to allow MQTT traffic (default port: 1883).

EC2

You now have an MQTT broker running on your EC2 instance. Remember to secure your MQTT broker, especially if it's exposed to the public internet. Consider implementing TLS/SSL encryption, user authentication, and proper network security measures.

Open mqtt & protected mqtt

You now have both an open and a protected MQTT broker running. The open broker listens on the standard MQTT port (1883), while the protected broker uses SSL/TLS encryption on port 8883. Always ensure that your protected broker is correctly configured with valid SSL/TLS certificates to maintain security.