Files
DesignForElectronics/week6/resources/Readme.md
T
2026-10-29 15:15:57 -04:00

4.9 KiB

How MQTT Works:

What is MQTT

MQTT (Message Queuing Telemetry Transport) is a lightweight, bidirectional messaging protocol designed for resource-constrained devices and low-bandwidth networks. It operates on a publish/subscribe architecture that decouples message senders from receivers, allowing for scalable and efficient machine-to-machine communication.

Core Components and Workflow

* MQTT Broker: A central server that acts as the intermediary, receiving all messages, filtering them, and distributing them to the appropriate recipients. 
* MQTT Clients: Any device or application (from microcontrollers to servers) that connects to the broker to either send or receive data. 
* Topics: Hierarchical labels (similar to file paths) that clients use to organize and subscribe to specific streams of data. 

The process functions as follows: Clients establish a persistent TCP/IP connection with the broker by exchanging CONNECT and CONNACK packets. Publishers send messages with a specific topic and payload to the broker, while subscribers send SUBSCRIBE requests to express interest in topics. The broker then forwards published messages to all clients subscribed to that topic, ensuring that publishers and subscribers never communicate directly.

Key Features

* Quality of Service (QoS): MQTT offers three levels of delivery guarantee: QoS 0 (at most once/fire-and-forget), QoS 1 (at least once/acknowledged), and QoS 2 (exactly once/four-way handshake), allowing users to balance reliability against bandwidth usage.
* Efficiency: It uses a minimal 2-byte fixed header and binary encoding to minimize data transmission overhead.
* State Management: The broker can buffer messages for disconnected clients, support retained messages (the last known state of a topic), and utilize Last Will and Testament (LWT) to notify subscribers if a client disconnects unexpectedly.

Installing an MQTT Broker

To install Mosquitto MQTT on Ubuntu, use the package manager for the stable version or add the official Personal Package Archive (PPA) for the latest release.

Standard Installation (Stable Version)

For most Ubuntu versions (including 20.04, 22.04, and 24.04), Mosquitto is available in the default repositories.

  1. Update package lists:

    sudo apt update

  2. Install the broker and client utilities:

    sudo apt install -y mosquitto mosquitto-clients

By default in Ubuntu this will start the MQTT broker as a service using systemctl.

Update Configuration

As of Mosquitto v2 remote access of the broker is turned off. If you are happy with your network security you can turn on anonymous clients, or you can secure it with a password.

sudo nano /etc/mosquitto/mosquitto.conf

Add the following to the configuration file:

# Bind MQTT to port 1883 on all available network interfaces
listener 1883 0.0.0.0
allow_anonymous true

Restart the service:

sudo systemctl restart mosquitto

You can also manually run the service in the command-line if you do not wish to have it auto-start. Stop the service, then run:

sudo mosquitto -v -c /etc/mosquitto/mosquitto.conf

To check what is listening on a particular port, in this case 1883, check using lsof:

sudo lsof -i :1883

If necessary you can kill the running application using the PID number.

Verification

Test the installation by checking the service status and version:

sudo systemctl status mosquitto
mosquitto -v

Uninstallation

To completely uninstall Mosquitto from Ubuntu, you should first stop the service, then remove the packages and configuration files using apt.

  1. Stop the Service: Prevent conflicts during removal. sudo systemctl stop mosquitto

  2. Remove Packages and Configs: Use purge to delete the software and its configuration files, and --auto-remove to delete dependencies no longer needed.

    sudo apt-get purge --auto-remove mosquitto mosquitto-clients

  3. Remove Repository (Optional): If you added the official Mosquitto PPA, remove it to keep your package sources clean.

    sudo add-apt-repository --remove ppa:mosquitto-dev/mosquitto-ppa

Port Conflicts

If something is already using the port 1883 that Mosquitto wants to use you can kill the other process using the 'fuser' command.

sudo fuser -k 1883/tcp

Installing Clients

There are a wide variety of clients available. These do not need the full functionality of the broker so they can be very lightweight.

If you device is network-enabled, such as a pico-w, there are arduino clients for MQTT.

If your device is connected to a computer, it can send messages through serial to an applciation that then uses a client to publish and subscribe to the broker.

Try setting up a virtual environment, and running the python application included here.

python3 -m venv .venv

Then activate the virtual environment:

source .venv/bin/activate

To deactivate the virtual environment:

deactivate

Now pip install the dependencies for the python based mqtt client:

pip install paho-mqtt