DEV Community

Cover image for QuecPython + Alibaba Cloud IoT: Quick Connection to Alibaba Cloud IoT Platform
QuecPython
QuecPython

Posted on

QuecPython + Alibaba Cloud IoT: Quick Connection to Alibaba Cloud IoT Platform

Overview

With the rapid development of IoT technology, cloud platforms have become the core hub connecting the physical and digital worlds. QuecPython deeply integrates with the Alibaba Cloud IoT Platform (aLiYun) to launch an integrated "device + cloud" development solution, providing IoT developers with out-of-the-box cloud connectivity capabilities.

Core Advantages

  • Minimalist Development Interface: Through Pythonic API design, complex underlying operations such as MQTT protocol stacks, security authentication, and data encoding/decoding are encapsulated into concise method calls, eliminating the need for complex low-level driver writing.
  • Comprehensive Documentation: Each API interface comes with detailed usage instructions, allowing developers to easily design and implement projects by following the documentation.

Feature Overview

This article mainly introduces how to use QuecPython series modules to connect to the Alibaba Cloud IoT Platform and perform data subscription and publishing. The general connection process is as follows:

  • Initialize the Alibaba Cloud IoT Platform
  • Configure related functions and callbacks
  • Implement subscription and publishing functions
  • Start and stop connections

Initialize Alibaba Cloud IoT Platform

This method is used to configure product and device information for the Alibaba IoT Platform IoT Suite and returns an aLiYun connection object.

aLiYun(productKey, productSecret, DeviceName, DeviceSecret, MqttServer)

Run

For detailed API information, please refer to aLiYun.

Configure Related Functions and Callbacks

MQTT Parameter Configuration

This method is used to set MQTT data channel parameters.

aLiYun.setMqtt(clientID, clean_session, keepAlive=300, reconn=True)

Run

For detailed API information, please refer to aLiYun.setMqtt.

Callback Registration

This method is used to register callback functions.

aLiYun.setCallback(callback)

Run

For detailed API information, please refer to aLiYun.setCallback.

Subscription and Publishing

Subscribe

This method is used to subscribe to MQTT topics.

aLiYun.subscribe(topic, qos)

Run

For detailed API information, please refer to aLiYun.subscribe.

Publish

This method is used to publish messages.

aLiYun.publish(topic, msg, qos=0)

Run

For detailed API information, please refer to aLiYun.publish.

Start and Stop Functions

Start Connection

This method is used to initiate the connection.

aLiYun.start()

Run

For detailed API information, please refer to aLiYun.start.

Close Connection

This method is used to terminate the connection.

aLiYun.disconnect()

Run

For detailed API information, please refer to aLiYun.disconnect.

Application Example

import log

import utime

import checkNet

from aLiYun import aLiYun

'''

The following two global variables are required. Users can modify their values according to their actual project needs

'''

PROJECT_NAME = "QuecPython_AliYun_example"

PROJECT_VERSION = "1.0.0"

checknet = checkNet.CheckNetwork(PROJECT_NAME, PROJECT_VERSION)

Set log output level

log.basicConfig(level=log.INFO)

aliYun_log = log.getLogger("ALiYun")

productKey = "" # Product identifier (refer to Alibaba IoT Platform application development guide)

productSecret = None # Product secret (pass None when using one-device-one-secret authentication, refer to Alibaba IoT Platform application development guide)

DeviceName = "" # Device name (refer to Alibaba IoT Platform application development guide)

DeviceSecret = "" # Device secret (pass None when using one-product-one-secret authentication. Pre-registration-free is not supported yet. Devices must be created on the IoT Platform first. Refer to Alibaba IoT Platform application development guide)

state = 5

Callback function

def sub_cb(topic, msg):

global state

aliYun_log.info("Subscribe Recv: Topic={}, Msg={}".format(topic.decode(), msg.decode()))

state -= 1

if name == 'main':

stagecode, subcode = checknet.wait_network_connected(30)

if stagecode == 3 and subcode == 1:

aliYun_log.info('Network connection successful!')

# Create Alibaba Cloud connection object

ali = aLiYun(productKey, productSecret, DeviceName, DeviceSecret)

# Set MQTT connection properties

clientID = "" # Custom string (no more than 64 characters)

ali.setMqtt(clientID, clean_session=False, keepAlive=300)

# Set callback function

ali.setCallback(sub_cb)

topic = "" # Custom or owned topic on the IoT Platform

# Subscribe to topic

ali.subscribe(topic)

# Publish message

ali.publish(topic, "hello world")

# Start connection

ali.start()

while 1:

if state:

pass

else:

ali.disconnect()

break

else:

aliYun_log.info('Network connection failed! stagecode = {}, subcode = {}'.format(stagecode, subcode))

Top comments (0)