Worksheet 3 - Experimenting with Mosquitto and MQTT

From Sensors in Schools
Revision as of 18:42, 18 October 2024 by EdmondLascaris (talk | contribs) (Created page with "An MQTT broker is a central server that facilitates the communication between devices using the MQTT (Message Queuing Telemetry Transport) protocol. The broker manages the message traffic by receiving data (messages) from publishers and routing it to subscribers who have subscribed to specific topics. It ensures that messages are delivered in a lightweight, efficient manner, making it ideal for IoT systems where bandwidth and power consumption are limited. Steps to Set...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

An MQTT broker is a central server that facilitates the communication between devices using the MQTT (Message Queuing Telemetry Transport) protocol. The broker manages the message traffic by receiving data (messages) from publishers and routing it to subscribers who have subscribed to specific topics. It ensures that messages are delivered in a lightweight, efficient manner, making it ideal for IoT systems where bandwidth and power consumption are limited.

Steps to Set Up an MQTT Broker on a Raspberry Pi 1. Update Your Raspberry Pi

Before setting up the broker, it’s essential to ensure your Raspberry Pi is updated:

bash Copy code sudo apt update sudo apt upgrade 2. Install Mosquitto (MQTT Broker)

Mosquitto is one of the most widely used MQTT brokers, and it’s available in the default Raspbian repository. Install both Mosquitto (the broker) and Mosquitto clients (tools for testing):

bash Copy code sudo apt install mosquitto mosquitto-clients 3. Start and Enable Mosquitto

After installation, you need to start the Mosquitto service and enable it to start automatically on boot:

bash Copy code sudo systemctl start mosquitto sudo systemctl enable mosquitto 4. Verify the Installation

To ensure that Mosquitto is running, you can check its status:

bash Copy code sudo systemctl status mosquitto You should see an output indicating that Mosquitto is active and running.

5. Test the MQTT Broker

You can test the broker using the Mosquitto clients. Open two terminal windows:

In Terminal 1 (Subscriber): Subscribe to a topic called "test/topic" using the following command: bash Copy code mosquitto_sub -h localhost -t "test/topic" In Terminal 2 (Publisher): Publish a message to the topic "test/topic": bash Copy code mosquitto_pub -h localhost -t "test/topic" -m "Hello from MQTT" You should see the message appear in the subscriber terminal.