Tutorials


March 28, 2024

Connect Your First Device to Qubitro with MQTT and Python

In this tutorial, you'll learn how to connect your first IoT device to the Qubitro platform using MQTT and Python. This simple guide is designed for beginners and assumes no prior experience with IoT, MQTT, or Qubitro. Prerequisites Before we begin, make sure you have the following: A Qubitro account. If you don't have one, sign up at Qubitro's website. Python installed on your computer. Python 3.6 or higher is recommended. An MQTT client library for Python. We will use paho-mqtt for this tutorial. Step 1: Install the Paho-MQTT Client Open your terminal or command prompt and install the paho-mqtt library using pip: ``bash pip install paho-mqtt ` Step 2: Create a Device on Qubitro 1. Log in to your Qubitro account and navigate to the 'Devices' section. 2. Click on 'Create Device' and select the device type that matches your project. For this tutorial, you can start with a generic device. 3. Name your device and save the credentials provided by Qubitro. You will need the Device ID and Token for MQTT authentication. Step 3: Connect and Publish to Qubitro with MQTT Create a new Python file and include the following code. Remember to replace YOUR_DEVICE_ID and YOUR_DEVICE_TOKEN with the credentials you obtained from Qubitro. `python 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) ` This script connects your device to Qubitro using MQTT. The on_connect` callback function confirms the connection status. This script sets up a secure connection to Qubitro using TLS, connects to the MQTT broker, and publishes a simple payload containing temperature data. It repeats the publishing process every 2 seconds. Step 4: Monitor Your Device Data on Qubitro After running the script, log into your Qubitro account. You should see the data being published from your device in real-time. Use Qubitro's monitoring tools to analyze the incoming data. Conclusion You've now successfully connected your device to Qubitro and started publishing data using MQTT and Python. This foundation allows you to expand your IoT project by adding more devices, sensors, and data types to your Qubitro account.

MQTT
February 29, 2024

How to integrate The Things Stack with Qubitro

Learn how to integrate The Things Stack with Qubitro for enhanced IoT data management and analytics.

LoRaWAN®
Integrations
The Things Stack
    Qubitro Logo

    Product

    Resources

    Industries

    Decision Aids

    Solutions

    Company

    © 2024 Qubitro, Inc. All rights reserved