Python正常进行mqtt相关开发,使用paho-mqtt模块(需要了解的可以看我另一篇Python快速实现MQTT双向订阅发布),需要参数如:name、clientId、host、port、username、password。
但在阿里云物联网网关开发时,一机一密型直接使用productKey、deviceName、deviceSecret进行开发(需要了解的可以看完另一篇博客Python对阿里云物联网MQTT设备接入端开发)。
本文从阿里云源码解析,将阿里云一机一密型中的配置解析出常规的配置信息。
用处在于如果没有设备网关,可以将数据解析后,使用MQTT桌面平台,填入常规mqtt配置信息,进而和阿里云物联网平台进行通信,进行数据发送,模拟、测试。
1. 先上代码
# -*- coding: utf-8 -*-
import sys
import time
import hmac
import hashlib
options = {
'productKey': 'your productKey',
'deviceName': 'your deviceName',
'deviceSecret': 'your deviceSecret',
'port': 1883,
'host': 'iot-as-mqtt.cn-shanghai.aliyuncs.com'
}
if sys.version > '3':
PY3 = True
else:
PY3 = False
def get_client_id(client_id, secure_mode=2, sign_method='hmacsha1'):
"""
获取加密后id
:param client_id:
:param secure_mode:
:param sign_method:
:return:
"""
ts = str(int