|
连接时返回的状态码一直是2。
基于这篇文章的代码修改https://blog.csdn.net/ruoyusixian/article/details/104368894。
请问为什么会问题的原因是在什么地方。
# 获取信息
def getInfo(auth):
host = 'https://iot.diandeng.tech'
url = '/api/v1/user/device/diy/auth?authKey=' + auth
print(host + url)
data = requests.get(host + url).json()
return data
requests.get("https://iot.diandeng.tech/api/v1/user/device/diy/auth?authKey="+key+"&miType=light&version=1.2.2")
info = getInfo(auth)
# 设置设备类型
client_id = info['detail']['deviceName']
server = "public.iot-as-mqtt.cn-shanghai.aliyuncs.com"
user = info['detail']['iotId']
pwd = info['detail']['iotToken']
subtopic = "/" + info['detail']['productKey'] + "/" + info['detail']['deviceName'] + "/r"# 拉的主题
pubtopic = "/" + info['detail']['productKey'] + "/" + info['detail']['deviceName'] + "/s"# 推的主题
print(subtopic,"/r",pubtopic)
# 连接成功的回调函数
def on_connect(client, userdata, flags, rc):
print("Ne session being set .", rc)//打印rc结果为2
client = mqtt.Client(client_id=client_id)
client.username_pw_set(user, pwd)
client.on_connect = on_connect
client.on_message = on_message
client.connect(server, 1883, 60)
|
|