Smart Cities - Setting up Raspberry Pi for Mosquitto
Jump to navigation
Jump to search
Credits
- Joe Gonzales - School Amateur Radio Club Network - VK3RSC
- Edmond Lascaris - Whittlesea Tech School - VK3ZUW
Raspberry Pi OS
- Starting with a fresh install of the Raspberry Pi Debian Bookworm OS with desktop (64 bit).
- Use the Raspberry Pi Imager to install the OS on a 16MB micro SD card.
- Complete the set up and updates on the Raspberry Pi.
Install the real Python IDE (not thonny) and the MQTT broker
- Open the Terminal and enter these commands
sudo apt update
sudo apt -y full-upgrade idle3 mosquitto mosquitto-clients
Install Virtual Environment
- These days Python must run in a virtual environment so that each user gets their own Python
- The virtual environment has to be activated before each use
- The linux prompt then changes from:
- <username>@home:~ $ to (env) <username>@home:~ $
- Create a virtual environment for Python called env in your /home/<username> directory.
- In Linux (and other operating systems), a virtual environment is an isolated Python environment that allows you to install and manage Python packages separately from the system-wide Python installation.
- This ensures that your Python projects have their own dependencies and versions of libraries without affecting or being affected by other projects or the global Python environment.
- Enter the commands in the Terminal.
cd ~
python3 -m venv env
- Activate the virtual environment with the commands.
cd ~/env
source bin/activate
Install libraries within Virtual Environment
- Ensure you are in the ~/env directory and the (env) virtual environment is active.
- Enter the following commands in the Terminal.
pip3 install pip paho-mqtt esphome
- This command will update pip and install paho-mqtt and esphome applications.
Configure the Python Idle3 shortcut to auto-activate the Python virtual environment
- Click Raspberry | Programming
- Right Click IDLE (using Python-3.11)
- Click Properties | Desktop Entry
- In the Command: field enter
/home/<username>/env/bin/python3.11 -m idlelib.idle
- In the Path: field enter
/home/<username>/env
- You no longer have to activate the virtual environment if you run Python Idle3
Become a Superuser
- Enter the command su -i
- The command sudo -i in Linux is used to open an interactive root shell. It is similar to sudo su, but it behaves slightly differently.
- -i (simulate initial login): Opens a login shell for the root user. This means it loads the root user's environment variables, including the root user's $PATH, just as if the root user had logged in normally.
- Alternatively, sudo su Switches to the root user but does not load the root user’s environment, so the $PATH and other settings may still be from the current user.
Create the MQTT Broker mosquitto.conf file
- The following command puts the following lines of code, up to EOF, into the file /etc/mosquitto/mosquitto.conf
cat > /etc/mosquitto/mosquitto.conf << EOF
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
pid_file /run/mosquitto/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
listener 1883
allow_anonymous true
EOF
- Navigate to check that the configuration file is in place using the command cd /etc/mosquitto/mosquitto.conf
- cat: The cat command is normally used to display or concatenate files. When used with >, it redirects the output to a file.
- >: This is the redirection operator. It takes the output of a command (in this case, cat) and writes it to a file. If the file already exists, it overwrites the content. If you use >>, it appends to the file instead.
- /etc/mosquitto/mosquitto.conf: This is the file where the content will be written (in this case, the Mosquitto MQTT broker configuration file).
- << EOF: The << syntax introduces a here document. This tells the shell to read input until it encounters the specified delimiter (EOF in this case). Everything typed between << EOF and EOF will be treated as input and sent to the file.
- EOF: This is the delimiter that ends the here document. It can be any string, but EOF is commonly used. Everything typed before this marker is written into the file.
Start the mosquitto service
- Mosquitto is an open-source MQTT broker (Message Queuing Telemetry Transport) that is widely used for machine-to-machine (M2M) and Internet of Things (IoT) communications.
- In Linux, Mosquitto provides a lightweight, messaging platform that allows devices and applications to communicate with each other using the MQTT protocol.
- Enter the command in the Terminal. You do not need to be in a Virtual Environment.
systemctl restart mosquitto
systemctl status mosquitto
Test mosquitto
- A publisher is an MQTT client that sends (or publishes) messages to a specific topic on the MQTT broker.
- Topic: A channel or identifier to which messages are sent. Topics are organized hierarchically (e.g., home/livingroom/temperature).
- Publishers send data (messages) to a topic, and any subscribers to that topic will receive the message.
- A temperature sensor could act as a publisher, sending temperature data to the topic home/livingroom/temperature.
- Test the following example.
- Open a new terminal window for the subscriber.
mosquitto_sub -h localhost -p 1883 -t MyTopic
- mosquitto_sub: This is the Mosquitto subscriber command-line tool. It allows you to subscribe to an MQTT topic and receive messages published to that topic.
- -h localhost: The -h flag specifies the host of the MQTT broker. In this case, localhost refers to the local machine, meaning the MQTT broker is running on the same computer where this command is executed.
- -p 1883: The -p flag specifies the port of the MQTT broker. Here, 1883 is the default port for non-encrypted MQTT communication. If the broker is using a different port, you can specify it here.
- -t MyTopic: The -t flag specifies the topic to subscribe to. In this case, the topic is MyTopic. This means that the client will receive any messages that are published to the MyTopic channel on the broker.
- Now open a new (second) terminal window for the publisher. The published will send the data.
mosquitto_pub -h localhost -p 1883 -t MyTopic -m "Hello World"
- -m "Hello World": The -m flag specifies the message to send. Here, the message is "Hello World". This is the actual content that is published to the topic.
- Hello World should appear in the subscriber window.