阿里物联网平台(一)Windows系统+VS2017 模拟设备端接入
https://blog.csdn.net/panwen1111/article/details/88365636
一、阿里物联网平台
平台地址:https://account.aliyun.com
根据文档设置平台端,跟着文档一步步来
文档地址:https://help.aliyun.com/document_detail/42648.html?spm=a2c4g.11174283.6.642.78b01668rFZaSZ
二、Windows模拟设备端
1.安装vs2017开发工具,配置好Python环境
使用pip来安装linkkit最新版本
pip install aliyun-iot-linkkit
2.Python实现客户端与平台连接、发送消息、接收消息
参考文档:https://help.aliyun.com/document_detail/98292.html?spm=a2c4g.11186623.6.617.4ca226831rShZt
#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)
运行,连接成功,订阅成功!
---------------------
作者:weixin_pwtank1983
来源:CSDN
原文:https://blog.csdn.net/panwen1111/article/details/88365636
版权声明:本文为博主原创文章,转载请附上博文链接!