Bird
Raised Fist0
Raspberry Piprogramming~20 mins

MQTT broker setup (Mosquitto) in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Mosquitto Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1:00remaining
Mosquitto Broker Default Port
What is the default port number on which the Mosquitto MQTT broker listens for client connections?
A8080
B8883
C80
D1883
Attempts:
2 left
💡 Hint
Think about the standard port used for unencrypted MQTT connections.
❓ Predict Output
intermediate
1:30remaining
Mosquitto Service Status Command
Which command correctly checks the status of the Mosquitto service on a Raspberry Pi running systemd?
Asudo systemctl status mosquitto
Bsudo service mosquitto check
Csudo mosquitto --status
Dsudo systemctl mosquitto status
Attempts:
2 left
💡 Hint
Use the systemctl command with the correct syntax to check service status.
🔧 Debug
advanced
2:00remaining
Mosquitto Configuration File Syntax Error
Given this snippet from a Mosquitto configuration file, what error will Mosquitto report when starting? listener 1883 allow_anonymous true password_file /etc/mosquitto/passwd Options:
Raspberry Pi
listener 1883
allow_anonymous true
password_file /etc/mosquitto/passwd
AError: 'password_file' path not found
BError: Invalid listener option 'password_file'
CNo error, Mosquitto starts normally
DError: 'listener' directive requires a protocol
Attempts:
2 left
💡 Hint
Check which configuration options are valid within a listener context.
🧠 Conceptual
advanced
1:30remaining
Mosquitto Authentication Setup
Which of the following steps is necessary to enable username/password authentication in Mosquitto on Raspberry Pi?
AEnable anonymous access and disable password file
BInstall a separate authentication plugin from the Raspberry Pi repository
CCreate a password file using 'mosquitto_passwd' and reference it in the config
DChange the default port to 8883
Attempts:
2 left
💡 Hint
Think about how Mosquitto manages user credentials by default.
❓ Predict Output
expert
2:30remaining
Mosquitto Broker Log Output
After starting Mosquitto with the command 'mosquitto -v' on Raspberry Pi, which output line indicates the broker is ready and listening on port 1883?
A
"1623456789: mosquitto version 2.0.15 starting"
"1623456789: Opening ipv4 listen socket on port 1883."
B"1623456789: Error: port 1883 already in use"
C"1623456789: mosquitto version 2.0.15 shutting down"
D"1623456789: Client connected from 192.168.1.10"
Attempts:
2 left
💡 Hint
Look for messages about opening sockets and starting version.

Practice

(1/5)
1. What is the primary role of the Mosquitto MQTT broker on a Raspberry Pi?
easy
A. To receive and forward messages between devices
B. To act as a web server for hosting websites
C. To store large files for backup
D. To compile Python programs

Solution

  1. Step 1: Understand MQTT broker function

    The MQTT broker acts as a middleman that receives messages from one device and forwards them to others subscribed to the same topic.
  2. Step 2: Compare with other options

    The other options describe unrelated functions like web hosting, file storage, and compiling, which are not roles of Mosquitto.
  3. Final Answer:

    To receive and forward messages between devices -> Option A
  4. Quick Check:

    MQTT broker = message forwarding [OK]
Hint: Broker forwards messages between devices [OK]
Common Mistakes:
  • Confusing broker with web server
  • Thinking Mosquitto stores files
  • Assuming it compiles code
2. Which command correctly installs Mosquitto on a Raspberry Pi running Raspberry Pi OS?
easy
A. install mosquitto sudo
B. sudo mosquitto install apt-get
C. mosquitto install sudo apt-get
D. sudo apt-get install mosquitto

Solution

  1. Step 1: Recall correct package installation syntax

    On Raspberry Pi OS, the correct command to install a package is 'sudo apt-get install package-name'. Here, the package is mosquitto.
  2. Step 2: Check each option's syntax

    Only sudo apt-get install mosquitto follows the correct syntax. The other options have incorrect word order or missing keywords.
  3. Final Answer:

    sudo apt-get install mosquitto -> Option D
  4. Quick Check:

    Install command = sudo apt-get install [OK]
Hint: Use 'sudo apt-get install mosquitto' exactly [OK]
Common Mistakes:
  • Mixing command word order
  • Omitting 'sudo' or 'install'
  • Using incorrect package manager commands
3. After installing Mosquitto, you run the command mosquitto_sub -h localhost -t test/topic in one terminal and mosquitto_pub -h localhost -t test/topic -m "Hello" in another. What will appear in the subscriber terminal?
medium
A. Error: Connection refused
B. Hello
C. mosquitto_sub
D. test/topic

Solution

  1. Step 1: Understand subscriber and publisher commands

    The subscriber listens on topic 'test/topic' on localhost. The publisher sends message 'Hello' to the same topic.
  2. Step 2: Predict subscriber output

    The subscriber will receive and display the message 'Hello' published to 'test/topic'. No error occurs if Mosquitto is running.
  3. Final Answer:

    Hello -> Option B
  4. Quick Check:

    Subscriber shows published message [OK]
Hint: Subscriber shows message sent to its topic [OK]
Common Mistakes:
  • Expecting topic name instead of message
  • Confusing command names with output
  • Assuming connection error without checking broker status
4. You try to start Mosquitto with sudo systemctl start mosquitto but get an error saying the service is not found. What is the likely cause?
medium
A. Mosquitto is not installed or service not enabled
B. You need to run the command without sudo
C. The Raspberry Pi is out of storage space
D. The MQTT topic name is incorrect

Solution

  1. Step 1: Analyze the error message

    The error 'service not found' means the system does not recognize the mosquitto service, usually because it is not installed or the service is not enabled.
  2. Step 2: Evaluate other options

    Running without sudo won't help as service commands require sudo. Storage space or topic name do not affect service recognition.
  3. Final Answer:

    Mosquitto is not installed or service not enabled -> Option A
  4. Quick Check:

    Service error = missing install or enable [OK]
Hint: Check if Mosquitto is installed and service enabled [OK]
Common Mistakes:
  • Trying without sudo
  • Blaming storage space
  • Confusing service error with topic issues
5. You want to allow remote devices to connect to your Mosquitto broker on Raspberry Pi. Which configuration change is necessary?
hard
A. Install a web server alongside Mosquitto
B. Change the MQTT topic to a public name
C. Edit /etc/mosquitto/mosquitto.conf to allow connections on all interfaces
D. Disable the Raspberry Pi firewall completely

Solution

  1. Step 1: Identify how Mosquitto listens for connections

    By default, Mosquitto listens only on localhost (127.0.0.1). To allow remote devices, you must configure it to listen on all network interfaces.
  2. Step 2: Understand configuration changes

    This is done by editing /etc/mosquitto/mosquitto.conf to set 'bind_address' or comment it out to listen on all interfaces. Changing topic names or installing web servers does not affect network access. Disabling firewall completely is unsafe and unnecessary if configured properly.
  3. Final Answer:

    Edit /etc/mosquitto/mosquitto.conf to allow connections on all interfaces -> Option C
  4. Quick Check:

    Config file change = allow remote connections [OK]
Hint: Edit config to listen on all interfaces [OK]
Common Mistakes:
  • Changing topic names instead of network settings
  • Thinking web server is needed
  • Disabling firewall without proper rules
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard