π¦ 1. What is MQTT? 
MQTT (Message Queuing Telemetry Transport)  is a lightweight publish/subscribe messaging protocol designed for IoT and low-bandwidth systems.
Broker = server (e.g., Mosquitto) 
Client = device/app that publishes or subscribes to topics 
Topic = a UTF-8 string like sensor/temp/office 
 
 
βοΈ 2. Install an MQTT Broker (Mosquitto) on Linux 
β
 Ubuntu / Debian 
sudo  apt updatesudo  apt install -y  mosquitto mosquitto-clients 
β
 Fedora / CentOS / RHEL 
sudo  dnf install -y  mosquitto mosquitto-clients 
β
 Arch Linux 
sudo  pacman -S  mosquitto mosquitto-clients 
Start & Enable: 
sudo  systemctl enable mosquittosudo  systemctl start mosquittoCheck status:
sudo  systemctl status mosquitto 
 
π 3. Test MQTT Locally 
In one terminal (subscriber):
mosquitto_sub  -h  localhost -t  "test/topic" In another (publisher):
mosquitto_pub  -h  localhost -t  "test/topic"  -m  "Hello MQTT!" You should see the message appear in the first terminal.
 
π 4. Add Authentication (Username + Password) 
Create password file  
sudo  mosquitto_passwd -c  /etc/mosquitto/passwd myuser
Edit Mosquitto config:  Create or modify /etc/mosquitto/conf.d/auth.conf: 
allow_anonymous false password_file /etc/mosquitto/passwd listener 1883 
Restart Mosquitto:  
sudo  systemctl restart mosquitto
Test authenticated connection:  
mosquitto_pub  -h  localhost -t  "secure/test"  -u  myuser -P  your_password -m  "Secure test"  
π 5. Enable TLS Encryption (Optional but Recommended) 
Create a self-signed cert: 
mkdir  ~/mosquitto_certs &&  cd  ~/mosquitto_certsopenssl  req -x509  -newkey  rsa:2048 -days  365 -nodes  -keyout  mqtt.key -out  mqtt.crt 
 
π² 7. Connect Devices / Clients 
Python (with paho-mqtt): 
import  paho.mqtt.client as  mqtt=  mqtt.Client()connect ("localhost" , 1883 , 60 )"test/topic" , "Hello from Python!" ) 
Raspberry Pi Pico W (MicroPython): 
Use umqtt.simple or umqtt.robust libraries to connect and publish.
 
 
π‘ 8. Remote Access and Firewalls 
Open port 1883 or 8883 in your firewall/router. 
Use ufw if on Ubuntu: 
 
 
π  9. Troubleshooting Tips 
 
 
Broker not responding 
systemctl status mosquitto 
Canβt connect remotely 
Check firewall or bind address 
 
Auth fails 
Re-check /etc/mosquitto/passwd and allow_anonymous 
 
TLS errors 
Check cert paths and validity 
 
 
 
β
 Summary Table 
 
Port 
1883 
/etc/mosquitto/mosquitto.conf 
Auth 
Off 
/etc/mosquitto/conf.d/auth.conf 
TLS 
Off 
/etc/mosquitto/conf.d/tls.conf 
Persistence 
Off 
persistence true 
Logs 
Syslog 
or log_dest file 
 
 
 
MQTT through python 
import  paho.mqtt.client as  mqttimport  uuid# MQTT broker configuration =  "localhost"   # Change if needed =  1883 =  "#" =  f"jupyter-client- { uuid. uuid4()} " # --- Callback Implementations --- def  on_connect(client, userdata, flags, reason_code, properties):print (f"β
 [on_connect] Connected:  { reason_code} " )if  hasattr (flags, "session_present" ):print (f"  βͺ session_present:  { flags. session_present} " )def  on_connect_fail(client, userdata):print ("β [on_connect_fail] Connection failed." )def  on_disconnect(client, userdata, reason_code, properties):print (f"β [on_disconnect] Disconnected:  { reason_code} " )def  on_message(client, userdata, msg):print (f"π© [on_message] Topic:  { msg. topic}  | Payload:  { msg. payload. decode()} " )def  on_publish(client, userdata, mid, reason_codes= None , properties= None ):print (f"π€ [on_publish] mid:  { mid}  reason:  { reason_codes} " )def  on_subscribe(client, userdata, mid, reason_codes, properties):print (f"π [on_subscribe] mid:  { mid}  reason_codes:  { reason_codes} " )def  on_unsubscribe(client, userdata, mid, reason_codes, properties):print (f"π« [on_unsubscribe] mid:  { mid}  reason_codes:  { reason_codes} " )def  on_log(client, userdata, level, buf):print (f"π [on_log]  { buf} " )def  on_socket_open(client, userdata, sock):print ("πΆ [on_socket_open] Socket opened" )def  on_socket_close(client, userdata, sock):print ("β [on_socket_close] Socket closed" )def  on_socket_register_write(client, userdata, sock):print ("ποΈ [on_socket_register_write] Socket ready for write" )def  on_socket_unregister_write(client, userdata, sock):print ("π [on_socket_unregister_write] Socket not ready for write" )# --- Client Setup --- =  mqtt.Client(client_id= CLIENT_ID, callback_api_version= mqtt.CallbackAPIVersion.VERSION2)=  on_connect=  on_connect_fail=  on_disconnect=  on_message=  on_publish=  on_subscribe=  on_unsubscribe=  on_log=  on_socket_open=  on_socket_close=  on_socket_register_write=  on_socket_unregister_write# --- Start Client --- connect (BROKER, PORT, 60 )
πΆ [on_socket_open] Socket opened
π [on_log] Sending CONNECT (u0, p0, wr0, wq0, wf0, c1, k60) client_id=b'jupyter-client-b11aed8c-5d98-4f04-a984-6a0c1d6a2978'
ποΈ [on_socket_register_write] Socket ready for write
π [on_socket_unregister_write] Socket not ready for write 
<MQTTErrorCode.MQTT_ERR_SUCCESS: 0> 
π [on_log] Received CONNACK (0, 0)
β
 [on_connect] Connected: Success
  βͺ session_present: False
π [on_log] Sending SUBSCRIBE (d0, m1) [(b'#', 0)]
ποΈ [on_socket_register_write] Socket ready for write
π [on_socket_unregister_write] Socket not ready for write
π [on_log] Received SUBACK
π [on_subscribe] mid: 1 reason_codes: [ReasonCode(Suback, 'Granted QoS 0')]
π [on_log] Received PUBLISH (d0, q0, r1, m0), '/online', ...  (1 bytes)
π© [on_message] Topic: /online | Payload: 0
π€ [on_publish] mid: 2 reason: Success
π [on_socket_unregister_write] Socket not ready for write
π [on_log] Received PUBLISH (d0, q0, r0, m0), '/led', ...  (1 bytes)
π© [on_message] Topic: /led | Payload: 0
π [on_log] Sending PINGREQ
ποΈ [on_socket_register_write] Socket ready for write
π [on_socket_unregister_write] Socket not ready for write
π [on_log] Received PINGRESP
π [on_log] Sending PINGREQ
ποΈ [on_socket_register_write] Socket ready for write
π [on_socket_unregister_write] Socket not ready for write
π [on_log] Received PINGRESP
π [on_log] Sending PINGREQ
ποΈ [on_socket_register_write] Socket ready for write
π [on_socket_unregister_write] Socket not ready for write
π [on_log] Received PINGRESP
π [on_log] Sending PINGREQ
ποΈ [on_socket_register_write] Socket ready for write
π [on_socket_unregister_write] Socket not ready for write
π [on_log] Received PINGRESP
π [on_log] Sending PINGREQ
ποΈ [on_socket_register_write] Socket ready for write
π [on_socket_unregister_write] Socket not ready for write
π [on_log] Received PINGRESP
π [on_log] Sending PINGREQ
ποΈ [on_socket_register_write] Socket ready for write
π [on_socket_unregister_write] Socket not ready for write
π [on_log] Received PINGRESP
π [on_log] Sending PINGREQ
ποΈ [on_socket_register_write] Socket ready for write
π [on_socket_unregister_write] Socket not ready for write
π [on_log] Received PINGRESP
π [on_log] Sending PINGREQ
ποΈ [on_socket_register_write] Socket ready for write
π [on_socket_unregister_write] Socket not ready for write
π [on_log] Received PINGRESP
π [on_log] Sending PINGREQ
ποΈ [on_socket_register_write] Socket ready for write
π [on_socket_unregister_write] Socket not ready for write
π [on_log] Received PINGRESP
π [on_log] Sending PINGREQ
ποΈ [on_socket_register_write] Socket ready for write
π [on_socket_unregister_write] Socket not ready for write
π [on_log] Received PINGRESP
π [on_log] Sending PINGREQ
ποΈ [on_socket_register_write] Socket ready for write
π [on_socket_unregister_write] Socket not ready for write
π [on_log] Received PINGRESP
π [on_log] Sending PINGREQ
ποΈ [on_socket_register_write] Socket ready for write
π [on_socket_unregister_write] Socket not ready for write
π [on_log] Received PINGRESP
π [on_log] Sending PINGREQ
ποΈ [on_socket_register_write] Socket ready for write
π [on_socket_unregister_write] Socket not ready for write
π [on_log] Received PINGRESP
π [on_log] Sending PINGREQ
ποΈ [on_socket_register_write] Socket ready for write
π [on_socket_unregister_write] Socket not ready for write
π [on_log] Received PINGRESP
π [on_log] Sending PINGREQ
ποΈ [on_socket_register_write] Socket ready for write
π [on_socket_unregister_write] Socket not ready for write
π [on_log] Received PINGRESP
π [on_log] Sending PINGREQ
ποΈ [on_socket_register_write] Socket ready for write
π [on_socket_unregister_write] Socket not ready for write
π [on_log] Received PINGRESP
π [on_log] Sending PINGREQ
ποΈ [on_socket_register_write] Socket ready for write
π [on_socket_unregister_write] Socket not ready for write
π [on_log] Received PINGRESP
π€ [on_publish] mid: 3 reason: Success
π [on_socket_unregister_write] Socket not ready for write
π [on_log] Received PUBLISH (d0, q0, r0, m0), '/led', ...  (1 bytes)
π© [on_message] Topic: /led | Payload: 0 
 
for  i in  range (5 ):"test/topic" , f"Message  { i} " ) 
"/led" , f"0" )
π [on_log] Sending PUBLISH (d0, q0, r0, m3), 'b'/led'', ... (1 bytes)
ποΈ [on_socket_register_write] Socket ready for write 
<paho.mqtt.client.MQTTMessageInfo> 
 
import  matplotlib.pyplot as  pltfrom  IPython.display import  clear_output=  []def  on_message(client, userdata, msg):=  float (msg.payload.decode())= True )"Live MQTT Plot" )"Message #" )"Value" )=  on_message=  client.subscribe("sensor/temp" )if  result ==  mqtt.MQTT_ERR_SUCCESS:print (f"β
 Subscribed successfully! MID =  { mid} " )else :print (f"β Failed to subscribe. Error code:  { result} " )
β
 Subscribed successfully! MID = 14