我是靠谱客的博主 老迟到宝贝,最近开发中收集的这篇文章主要介绍python 物联网平台_使用 Python3 接入阿里云物联网平台(原物联网套件),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

使用 Python3 接入阿里云物联网平台(原物联网套件)

发布时间:2018-08-16 10:32,

浏览次数:733

, 标签:

Python

阿里云官方提供的 DEMO

里面没有Python接入 阿里云物联网平台(原物联网套件)

的例子,不便于我们在电脑端做虚拟终端的相关测试,本文介绍一种基于使用Python3、MQTT-TCP连接通信

接入阿里云物联网平台(原物联网套件)。

开发语言:Python3.5

开发环境:Ubutnu16.04

IDE:PyCharm Community Edition

通信协议:MQTT、TCP

参考资料:开源MQTT客户端参考 、

MQTT协议

相关筹备:阿里云物联网平台(原物联网套件)、创建产品、创建Topic(data)

1. 新建文件夹AliYun_Iot,在此文件夹下新建Get_para.py、Sub_Client.py、Pub_Client.py。

2. 编辑Get_para.py,如下

#!/usr/bin/env python3.5 # -*- coding:utf-8 -*- """ file: Get_para.py date:

20171111 author: S_L_zheng function: return client_id, username, password. """

import time import hmac import hashlib import math class Get_para(object): def

__init__(self, PK, DN, DS): self.ProductKey = PK self.DeviceName = DN

self.DeviceSecret = DS def get_para(self): ProductKey = self.ProductKey

ClientId = self.DeviceName DeviceName = self.DeviceName DeviceSecret =

self.DeviceSecret signmethod = "hmacsha1" us = math.modf(time.time())[0] ms =

int(round(us * 1000)) timestamp = str(ms) data = "".join(("clientId", ClientId,

"deviceName", DeviceName, "productKey", ProductKey, "timestamp", timestamp))

ret = hmac.new(bytes(DeviceSecret, encoding='utf-8'), bytes(data,

encoding='utf-8'), hashlib.sha1).hexdigest() sign = ret client_id =

"".join((ClientId, "|securemode=3", ",signmethod=", signmethod, ",timestamp=",

timestamp, "|")) username = "".join((DeviceName, "&", ProductKey)) password =

sign return client_id, username, password if __name__ == '__main__': instance =

Get_para(PK='YurProductKey', DN='YourDeviceName', DS='YourDeviceSecret')

client_id, username, password = instance.get_para()

print('client_id:',client_id) print('username:', username) print('password:',

password)

3. 编辑Sub_Client.py,如下

#!/usr/bin/env python3.5 # -*- coding:utf-8 -*- """ file: Sub_Client.py date:

20171111 author: S_L_zheng function: Subscribe Topic. """ import

paho.mqtt.client as mqtt import Get_para DeviceName = "YourDeviceName"

DeviceSecret = "YourDeviceSecret " ProductKey = "YourProductKey " res =

Get_para.Get_para(PK=ProductKey, DN=DeviceName, DS=DeviceSecret) paras =

res.get_para() client_id = paras[0] username = paras[1] password = paras[2]

strBroker = ProductKey + ".iot-as-mqtt.cn-shanghai.aliyuncs.com" port = 1883

topic = ProductKey+'/'+DeviceName+'/'+'data' def on_connect(mqttc, userdata,

flags, rc): print("Connected with result code " + str(rc))

mqttc.subscribe(topic) def on_message(mqttc, userdata, msg): msgpayload =

str(msg.payload, encoding='utf-8') print(msgpayload) def on_subscribe(mqttc,

userdata, mid, granted_qos): print('on_subscribe') def on_publish(mqttc,

userdata, mid): print('on_publish') if __name__ == '__main__': mqttc =

mqtt.Client(client_id) mqttc.username_pw_set(username, password)

mqttc.on_connect = on_connect mqttc.on_message = on_message mqttc.on_subscribe

= on_subscribe mqttc.on_publish = on_publish mqttc.connect(strBroker, port,

120) mqttc.loop_forever()

4. 编辑Pub_Client.py,如下

#!/usr/bin/env python3.5 # -*- coding:utf-8 -*- """ file: Pub_Client.py date:

20171111 author: S_L_zheng function: Publishi message to topic. """ import

paho.mqtt.client as mqtt import Get_para DeviceName = "YourDeviceName"

DeviceSecret = "YourDeviceSecret" ProductKey = "YourProductKey" res =

Get_para.Get_para(PK=ProductKey, DN=DeviceName, DS=DeviceSecret) paras =

res.get_para() client_id = paras[0] username = paras[1] password = paras[2]

strBroker = ProductKey + ".iot-as-mqtt.cn-shanghai.aliyuncs.com" port = 1883

topic = ProductKey+'/'+DeviceName+'/'+'data' def on_connect(mqttc, userdata,

flags, rc): print("Connected with result code " + str(rc)) message =

'{"weather":"28"}' # must json mqttc.publish(topic, message, 1) def

on_message(mqttc, userdata, msg): msgpayload = str(msg.payload,

encoding='utf-8') print(msgpayload) def on_publish(mqttc, userdata, mid):

print('on_publish') if __name__ == '__main__': mqttc = mqtt.Client(client_id)

mqttc.username_pw_set(username, password) mqttc.on_connect = on_connect

mqttc.on_message = on_message mqttc.on_publish = on_publish

mqttc.connect(strBroker, port, 120) mqttc.loop_forever()

5. 代码运行顺序

Get_para.py—Sub_Client.py—Pub_Client.py

最后

以上就是老迟到宝贝为你收集整理的python 物联网平台_使用 Python3 接入阿里云物联网平台(原物联网套件)的全部内容,希望文章能够帮你解决python 物联网平台_使用 Python3 接入阿里云物联网平台(原物联网套件)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部