Smart Cities - Setting up Raspberry Pi for Mosquitto
Credits
- Joe Gonzales - School Amateur Radio Club Network - VK3RSC
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.
- Open a terminal window and run the following commands (you can copy and past them into a terminal window)
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.
- Enter the commands in the Terminal.
cd ~
python3 -m venv env
- Activate the virtual environment
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
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
- Explainer
- 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 configuation file is in place using the command cd /etc/mosquitto/mosquitto.conf
- Explainer
- 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
systemctl restart mosquitto