1、阿里物聯網平臺html
平臺地址:https://account.aliyun.comjson
根據文檔設置平臺端,跟着文檔一步步來session
文檔地址:https://help.aliyun.com/document_detail/42648.html?spm=a2c4g.11174283.6.642.78b01668rFZaSZasync
2、Windows模擬設備端工具
1.安裝vs2017開發工具,配置好Python環境post
使用pip來安裝linkkit最新版本開發工具
pip install aliyun-iot-linkkit
2.Python實現客戶端與平臺鏈接、發送消息、接收消息阿里雲
參考文檔:https://help.aliyun.com/document_detail/98292.html?spm=a2c4g.11186623.6.617.4ca226831rShZt.net
#coding=utf-8
from linkkit import linkkit
import time
#阿里雲三元組,在平臺上建立產品、設備後取得
lk = linkkit.LinkKit(
host_name="cn-shanghai",
product_key="", #三元組
device_name="", #三元組
device_secret="") #三元組
#鏈接成功回調
def on_connect(session_flag, rc, userdata):
print("on_connect:%d,rc:%d,userdata:" % (session_flag, rc))
pass
#斷開鏈接回調
def on_disconnect(rc, userdata):
print("on_disconnect:rc:%d,userdata:" % rc)
lk.on_connect = on_connect
lk.on_disconnect = on_disconnect
lk.config_device_info("Eth|03ACDEFF0032|Eth|03ACDEFF0031")
lk.connect_async()
#訂閱雲端消息
def on_subscribe_topic(mid, granted_qos, userdata):
print("on_subscribe_topic mid:%d, granted_qos:%s" %
(mid, str(','.join('%s' % it for it in granted_qos))))
pass
#發送消息到雲端
def on_publish_topic(mid, userdata):
print("on_publish_topic mid:%d" % mid)
#接收與處理來自雲端的消息
def on_topic_message(topic, payload, qos, userdata):
print("on_topic_message:" + topic + " payload:" + str(payload) + " qos:" + str(qos))
pass
time.sleep(5)
lk.on_publish_topic = on_publish_topic
lk.on_topic_message = on_topic_message
lk.on_subscribe_topic = on_subscribe_topic
#訂閱雲端消息,系統消息
#rc, mid = lk.subscribe_topic("/sys/a1hSxOQhke5/smp001/thing/service/property/set")
#訂閱雲端消息,自定義消息
rc, mid = lk.subscribe_topic(lk.to_full_topic("user/get"))
i=1
#阿里雲系統參數上報數據,t1對應雲平臺屬性名稱
payload_json = {
'id': int(time.time()),
'params': {
't1': 88
},
'method': "thing.event.property.post"
}
#阿里雲自定義參數上報數據
payload_json2 = {'params': {'t1': 123}}
while(True):
print(str(i))
#發送消息到雲端,系統訂閱上報
#rc, mid = lk.publish_topic("/sys/a1hSxOQhke5/smp001/thing/event/property/post", str(payload_json))
#發送消息到雲端,自定義訂閱上報
#rc, mid = lk.publish_topic(lk.to_full_topic("user/update"), str(payload_json2))
i = i + 1
time.sleep(15)
運行,鏈接成功,訂閱成功!htm