SkyCase MQTT Connection Guide
This guide provides instructions on how to establish an MQTT connection with the SkyCase IoT Cloud Platform and how to perform basic publish and subscribe operations.
Overview
MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol designed for IoT devices. It enables efficient communication between devices and the SkyCase platform.
MQTT Connection Setup
To connect to SkyCase using MQTT, follow these steps:
MQTT Broker Details: - Broker URL: skycase.embien.com - Port: 1883 for unencrypted connections, 8883 for encrypted connections (SSL/TLS)
Device Credentials: - Username: Use the device access token provided by SkyCase. - Password: Leave it blank (optional, based on security requirements).
MQTT Client Libraries: - You can use any standard MQTT client library. Popular choices include Paho MQTT for Python, Eclipse Paho for Java, and Mosquitto for C.
Example MQTT Connection
Below is an example of how to connect to SkyCase using the Paho MQTT client library in Python.
import paho.mqtt.client as mqtt
# Define the MQTT broker details
broker = "skycase.embien.com"
port = 1883
access_token = "YOUR_DEVICE_ACCESS_TOKEN"
# Create an MQTT client instance
client = mqtt.Client()
# Set the access token as the username for the MQTT connection
client.username_pw_set(access_token)
# Define the on_connect callback function
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to SkyCase MQTT Broker")
else:
print(f"Failed to connect, return code {rc}")
# Define the on_message callback function
def on_message(client, userdata, msg):
print(f"Received message: {msg.topic} {str(msg.payload)}")
# Set the callback functions
client.on_connect = on_connect
client.on_message = on_message
# Connect to the MQTT broker
client.connect(broker, port, 60)
# Start the loop to process network traffic and dispatch callbacks
client.loop_start()
Publishing Messages
To send data to SkyCase, publish messages to the appropriate topics. For example, to publish telemetry data:
# Define the telemetry topic
telemetry_topic = "v1/devices/me/telemetry"
# Define the telemetry payload
payload = '{"temperature": 25, "humidity": 60}'
# Publish the telemetry data
client.publish(telemetry_topic, payload)
Subscribing to Topics
To receive data or commands from SkyCase, subscribe to the relevant topics. For example, to subscribe to attribute updates:
# Define the attributes topic
attributes_topic = "v1/devices/me/attributes"
# Subscribe to the attributes topic
client.subscribe(attributes_topic)
Full Example
Here’s a complete example demonstrating both publishing telemetry data and subscribing to attribute updates:
import paho.mqtt.client as mqtt
# Define the MQTT broker details
broker = "skycase.embien.com"
port = 1883
access_token = "YOUR_DEVICE_ACCESS_TOKEN"
# Create an MQTT client instance
client = mqtt.Client()
# Set the access token as the username for the MQTT connection
client.username_pw_set(access_token)
# Define the on_connect callback function
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to SkyCase MQTT Broker")
# Publish telemetry data after connecting
publish_telemetry_data()
else:
print(f"Failed to connect, return code {rc}")
# Define the on_message callback function
def on_message(client, userdata, msg):
print(f"Received message: {msg.topic} {str(msg.payload)}")
# Function to publish telemetry data
def publish_telemetry_data():
topic = "v1/devices/me/telemetry"
payload = '{"temperature": 22.5, "humidity": 60}'
result = client.publish(topic, payload)
# Check if publish was successful
status = result[0]
if status == 0:
print(f"Sent `{payload}` to topic `{topic}`")
else:
print(f"Failed to send message to topic {topic}")
# Set the callback functions
client.on_connect = on_connect
client.on_message = on_message
# Connect to the MQTT broker
client.connect(broker, port, 60)
# Start the loop to process network traffic and dispatch callbacks
client.loop_start()
# Keep the script running to maintain the connection and allow callbacks to be processed
try:
while True:
pass
except KeyboardInterrupt:
print("Exiting")
client.loop_stop()
client.disconnect()
Usage Notes
Ensure you have the correct device access token for authentication.
Adjust the MQTT broker URL and port if using a custom SkyCase instance.
Refer to the SkyCase documentation for detailed information on available topics and message formats.
For further details and additional information, please refer to the SkyCase documentation or contact support.