Examples

Getting Started with MQTT

This documentation provides MQTT examples for using Qubitro as the broker to connect, subscribe, and publish messages across programming languages for building MQTT-based apps.

Publish-only

Code snippets for publishing data to Qubitro with MQTT protocol. The code can be adapted to your specific requirements by changing the device credentials, payload, and publishing interval.

publish.py
  import ssl
  import time
  import json
  import paho.mqtt.client as mqtt
 
  # Define the MQTT broker information and device credentials
  broker_host = "broker.qubitro.com"
  broker_port = 8883
  device_id = "YOUR_DEVICE_ID"
  device_token = "YOUR_DEVICE_TOKEN"
  # Define callback functions
  def on_connect(client, userdata, flags, rc):
      if rc == 0:
          print("Connected to Qubitro!")
      else:
          print("Failed to connect, return code:", rc)
 
  def on_publish(client, userdata, mid):
      print("Published: " + str(payload))
 
  # Create the MQTT client, set the TLS context, and provide device credentials
  client = mqtt.Client(client_id=device_id)
  context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
  client.tls_set_context(context)
  client.username_pw_set(username=device_id, password=device_token)
 
  # Assign the callback functions to the client
  client.on_connect = on_connect
  client.on_publish = on_publish
 
  # Connect to the MQTT broker and start the client loop
  client.connect(broker_host, broker_port, 60)
  client.loop_start()
 
  # Define the payload to be published
  payload = {
      "temp": 64,
      "Key2": 30.5
  }
 
  # Publish the payload every 2 seconds
  while True:
      if client.is_connected():
          client.publish(device_id, json.dumps(payload))
          time.sleep(2)

Subscribe-only

Code snippets for subscribing to the specified device with MQTT protocol. The code can be adapted to your specific requirements by changing the device credentials, payload, and publishing interval.

subscribe.py
import ssl
import paho.mqtt.client as mqtt
 
# Define the MQTT broker information and device credentials
broker_host = "broker.qubitro.com"
broker_port = 8883
device_id = "YOUR_DEVICE_ID"
device_token = "YOUR_DEVICE_TOKEN"
 
# Define callback functions
def on_connect(client, userdata, flags, rc):
if rc == 0:
    print("Connected to Qubitro!")
    client.subscribe(device_id, 0)
else:
    print("Failed to connect, return code:", rc)
 
def on_subscribe(mqttc, obj, mid, granted_qos):
print("Subscribed, " + "qos: " + str(granted_qos))
 
def on_message(client, userdata, message):
print("received message =", str(message.payload.decode("utf-8")))
 
# Create the MQTT client, set the TLS context, and provide device credentials
client = mqtt.Client(client_id=device_id)
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
client.tls_set_context(context)
client.username_pw_set(username=device_id, password=device_token)
 
# Assign the callback functions to the client
client.on_connect = on_connect
client.on_subscribe = on_subscribe
client.on_message = on_message
 
# Connect to the MQTT broker and start the client loop
client.connect(broker_host, broker_port, 60)
client.loop_start()
 

Publish & Subscribe

This code connects to Qubitro with MQTT protocol and subscribes to a topic based on the provided device id. It continuously publishes JSON-formatted data to the device id topic every 2 seconds, and whenever a new message is received on that topic, it prints the received message.

subscribe.py
import ssl
import json
import time
import paho.mqtt.client as mqtt
 
# Define the MQTT broker information and device credentials
broker_host = "brokerdev.qubitro.com"
broker_port = 8883
device_id = "YOUR_DEVICE_ID"
device_token = "YOUR_DEVICE_TOKEN"
 
# Define callback functions
def on_connect(client, userdata, flags, rc):
    if rc == 0:
        print("Connected to Qubitro!")
        client.subscribe(device_id, 0)
    else:
        print("Failed to connect, return code:", rc)
 
def on_subscribe(mqttc, obj, mid, granted_qos):
    print("Subscribed, " + "qos: " + str(granted_qos))
 
def on_message(client, userdata, message):
    print("received message =", str(message.payload.decode("utf-8")))
 
# Create the MQTT client, set the TLS context, and provide device credentials
client = mqtt.Client(client_id=device_id)
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
client.tls_set_context(context)
client.username_pw_set(username=device_id, password=device_token)
 
# Assign the callback functions to the client
client.on_connect = on_connect
client.on_subscribe = on_subscribe
client.on_message = on_message
 
# Connect to the MQTT broker and start the client loop
client.connect(broker_host, broker_port, 60)
client.loop_start()
 
# Prepare the data to be sent as a JSON string
payload = {
    "temp": 64,
    "Key2": 30.5
}
 
# Continuously publish data every 2 seconds
while True:
    if client.is_connected():
        client.publish(device_id, payload=json.dumps(payload))
        time.sleep(2)
 

logo

Start building today

Collect, process, and activate device data. Scale from one device to thousands.

Qubitro Logo

Product

Decision Aids

Resources

Company

© 2024 Qubitro, Inc. All rights reserved