With no authentication

Using Terminal

# To Publish
mosquitto_pub -h hostname -p 1883 -t topic -m "message"

# To Subscribe
mosquitto_sub -h hostname -p 1883 -t topic

Using Python program

import paho.mqtt.client as mqtt

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    client.subscribe("topic")
    client.publish("topic","message")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("hostname", 1883, 60)

client.loop_forever()

With username and password authentication

Using Terminal

# To Publish
mosquitto_pub -h hostname -p 1883 -u username -P password -t topic -m "message"

# To Subscribe
mosquitto_sub -h hostname -p 1883 -u username -P password -t topic

Using Python Program

import paho.mqtt.client as mqtt

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    client.subscribe("topic")
    client.publish("topic","message")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(username="username", password="password")
client.connect("hostname", 1883, 60)

client.loop_forever()

Connecting with SSL

Using Terminal

# To Publish
mosquitto_pub -h hostname -p 8883 -i ClientID --cafile ca-cert.pem  -t topic -m "message"

#To Subscribe
mosquitto_sub -h hostname -p 8883 -i ClientID --cafile ca-cert.pem  -t topic 

Using Python Program


import paho.mqtt.client as mqtt
import ssl

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    client.subscribe("topic")
    client.publish("topic","message")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

client=mqtt.Client(client_id="ClientID") 
client.on_connect = on_connect
client.on_message = on_message

client.tls_set(ca_certs="ca-cert.pem")
client.tls_insecure_set(True)
client.connect("hostname", 8883, 60)

client.loop_forever()

Connecting with SSL and Client Certificates

Using Terminal

# To Publish
mosquitto_pub -h hostname -p 8883 -i ClientID --cafile ca-cert.pem --cert device.crt --key device.key -t topic -m "message"

# To Subscribe
mosquitto_sub -h hostname -p 8883 -i ClientID --cafile ca-cert.pem --cert device.crt --key device.key -t topic

Using Python Program

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    client.subscribe("topic")
    client.publish("topic","message")

def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

client = mqtt.Client(client_id="ClientID")
client.on_connect = on_connect
client.on_message = on_message
client.tls_set(ca_certs="ca-cert.pem", certfile="device.crt", keyfile="device.key")
client.connect("hostname", 8883, 60)

client.loop_forever()
Last modified: January 28, 2021

Author

Comments

Write a Reply or Comment

Your email address will not be published.