0
0
IOT Protocolsdevops~20 mins

MQTT client with Python (paho-mqtt) in IOT Protocols - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MQTT Python Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
MQTT Client Connection Result

What will be the output when this Python MQTT client code tries to connect to a broker that is offline?

IOT Protocols
import paho.mqtt.client as mqtt

client = mqtt.Client()
result = client.connect('localhost', 1883, 60)
print(result)
A1
B2
C0
D3
Attempts:
2 left
💡 Hint

Check the meaning of return codes from connect() in paho-mqtt.

Configuration
intermediate
2:00remaining
Setting MQTT Client Keepalive Interval

Which option correctly sets the keepalive interval to 120 seconds when connecting with paho-mqtt client?

IOT Protocols
import paho.mqtt.client as mqtt
client = mqtt.Client()
client.connect('broker.hivemq.com', 1883, ???)
A120
Bclient.keepalive = 120
Cclient.connect('broker.hivemq.com', 1883, 120)
Dclient.connect('broker.hivemq.com', 1883, keepalive=120)
Attempts:
2 left
💡 Hint

Look at the connect() method signature for the keepalive parameter position.

🔀 Workflow
advanced
3:00remaining
MQTT Client Message Handling Sequence

What is the correct sequence of MQTT client callbacks to receive a message after subscribing?

A1,2,3,4
B1,3,2,4
C3,1,2,4
D2,1,3,4
Attempts:
2 left
💡 Hint

Remember you must connect before subscribing and start the loop to process messages.

Troubleshoot
advanced
3:00remaining
Diagnosing MQTT Client Subscription Failure

Given this code snippet, why does the client never receive messages?

import paho.mqtt.client as mqtt

def on_message(client, userdata, msg):
    print(f"Received: {msg.payload.decode()}")

client = mqtt.Client()
client.on_message = on_message
client.connect('broker.hivemq.com', 1883, 60)
client.subscribe('test/topic')
# Missing client.loop_start() or loop_forever()
AThe client.connect() call is missing the keepalive parameter
BThe subscription topic is invalid and causes silent failure
CThe on_message callback is assigned incorrectly
DThe client never processes network events because the loop is not started
Attempts:
2 left
💡 Hint

Think about how paho-mqtt processes incoming messages.

Best Practice
expert
4:00remaining
Secure MQTT Client Connection with TLS

Which code snippet correctly configures a paho-mqtt client to securely connect to a broker using TLS with certificate verification?

IOT Protocols
import paho.mqtt.client as mqtt

client = mqtt.Client()
# Configure TLS here
client.connect('mqtt.example.com', 8883, 60)
client.loop_start()
Aclient.tls_set(ca_certs='ca.crt')
Bclient.tls_set(ca_certs='ca.crt', certfile='client.crt', keyfile='client.key')
Cclient.tls_insecure_set(True)
Dclient.tls_set()
Attempts:
2 left
💡 Hint

Look for the method that sets CA and client certificates for TLS.