├── .gitignore ├── LICENSE ├── README.md ├── aliyun_iot_device ├── .gitignore ├── __init__.py ├── http │ ├── .gitignore │ ├── __init__.py │ └── client.py ├── mqtt │ ├── .gitignore │ ├── __init__.py │ └── client.py └── root.cer ├── examples ├── http │ ├── basic.py │ └── context.py └── mqtt │ ├── all.py │ ├── basic.py │ ├── context.py │ ├── https.py │ ├── shadow.py │ └── websockets.py ├── requirements.txt └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__/ 2 | *.pyc 3 | build/ 4 | dist/ 5 | yansongda_aliyun_iot_device.egg-info -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 yansongda 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # aliyun-iot-device-python 2 | 3 | 非官方,阿里云 IOT 套件设备端 Python 开发 SDK 4 | 5 | 6 | ## 支持的协议 7 | 8 | - [x] MQTT 9 | - [x] HTTP 10 | - [ ] CoAP 11 | 12 | 13 | ## 环境 14 | 15 | - Python3 16 | 17 | 18 | ## 安装 19 | 20 | `pip3 install yansongda-aliyun-iot-device` 21 | 22 | 23 | ## 使用 24 | 25 | ### MQTT 26 | 27 | ```Python 28 | from aliyun_iot_device.mqtt import Client as IOT 29 | import time 30 | 31 | 32 | def on_connect(client, userdata, flags, rc): 33 | print('subscribe') 34 | client.subscribe(qos=1) 35 | 36 | 37 | def on_message(client, userdata, msg): 38 | print('receive message') 39 | print(str(msg.payload)) 40 | 41 | 42 | PRODUCE_KEY = "b1VzFx30hEm" 43 | DEVICE_NAME = "iot_device_01" 44 | DEVICE_SECRET = "3TSqd6sfzjSkSwEmLmcAdZnI0oGlmRZ8" 45 | 46 | iot = IOT((PRODUCE_KEY, DEVICE_NAME, DEVICE_SECRET)) 47 | 48 | iot.on_connect = on_connect 49 | iot.on_message = on_message 50 | 51 | iot.connect() 52 | 53 | iot.loop_start() 54 | while True: 55 | iot.publish(payload="success", qos=1) 56 | time.sleep(5) 57 | ``` 58 | 59 | #### 回调 60 | 61 | - on_connect 62 | 63 | 定义连接成功后的回调函数 64 | 65 | 回调函数格式: 66 | 67 | `connect_callback(client, userdata, flags, rc)` 68 | 69 | client: the client instance for this callback 70 | 71 | userdata: the private user data as set in Client() or userdata_set() 72 | 73 | flags: response flags sent by the broker 74 | 75 | rc: the connection result 76 | 77 | - on_subscribe 78 | 79 | 定义订阅成功后的回调函数 80 | 81 | 回调函数格式: 82 | 83 | `subscribe_callback(client, userdata, mid, granted_qos)` 84 | 85 | client: the client instance for this callback 86 | 87 | userdata: the private user data as set in Client() or userdata_set() 88 | 89 | mid: matches the mid variable returned from the corresponding 90 | subscribe() call. 91 | 92 | granted_qos: list of integers that give the QoS level the broker has 93 | granted for each of the different subscription requests. 94 | 95 | - on_message 96 | 97 | 定义收到消息时的回调函数. 98 | 99 | 回调函数格式: 100 | 101 | `on_message_callback(client, userdata, message)` 102 | 103 | client: the client instance for this callback 104 | 105 | userdata: the private user data as set in Client() or userdata_set() 106 | 107 | message: an instance of MQTTMessage.This is a class with members topic, payload, qos, retain. 108 | 109 | 110 | - on_publish 111 | 112 | 定义 publish() 方法成功发送消息时的回调函数. 113 | 114 | 格式: 115 | 116 | `on_publish_callback(client, userdata, mid)` 117 | 118 | client: the client instance for this callback 119 | 120 | userdata: the private user data as set in Client() or userdata_set() 121 | 122 | mid: matches the mid variable returned from the corresponding publish() call, to allow outgoing messages to be tracked. 123 | 124 | - on_unsubscribe 125 | 126 | 定义取消订阅某条 topic 时的回调函数. 127 | 128 | 格式: 129 | 130 | `unsubscribe_callback(client, userdata, mid)` 131 | 132 | client: the client instance for this callback 133 | 134 | userdata: the private user data as set in Client() or userdata_set() 135 | 136 | mid: matches the mid variable returned from the corresponding 137 | unsubscribe() call. 138 | 139 | - on_disconnect 140 | 141 | 定义连接断开时的回调函数. 142 | 143 | 格式: 144 | 145 | `disconnect_callback(client, userdata, self)` 146 | 147 | client: the client instance for this callback 148 | 149 | userdata: the private user data as set in Client() or userdata_set() 150 | 151 | rc: the disconnection result. 152 | 153 | #### 说明 154 | 155 | - 域名直连与 HTTPS 认证 156 | 157 | SDK 默认使用域名直连同时启用 TLS 加密。 158 | 159 | 如果您不想使用 TLS 加密,可在初始化时传入 `tls=False` 参数; 160 | 161 | 如果您想使用 HTTPS 认证,可在初始化时传入 `domain_direct=False` 参数,HTTPS 认证将强制使用 TLS 认证加密 162 | 163 | - TLS 认证 CA 证书 164 | 165 | SDK 默认使用了阿里云 IOT 根证书,一般情况无需修改。 166 | 167 | 如一定要修改,请传入 `ca_certs="/path/to/cert/root.cer"` 168 | 169 | - websocket 通道 170 | 171 | SDK 默认使用 TCP 通道。 172 | 173 | 如果需要使用 websocket,请传入 `transport="websockets"`。 174 | 175 | 当使用 websocket 时,默认启用 TLS,即使用的是 wss 协议,如果不想使用 wss,请同时传入 `tls=False` 176 | 177 | ### HTTP 178 | 179 | ```Python 180 | from aliyun_iot_device.http import Client as IOT 181 | import time 182 | 183 | PRODUCE_KEY = "b1VzFx30hEm" 184 | DEVICE_NAME = "iot_device_01" 185 | DEVICE_SECRET = "3TSqd6sfzjSkSwEmLmcAdZnI0oGlmRZ8" 186 | 187 | iot = IOT((PRODUCE_KEY, DEVICE_NAME, DEVICE_SECRET)) 188 | 189 | while True: 190 | iot.publish('success') 191 | time.sleep(5) 192 | ``` 193 | 194 | #### 注意 195 | 196 | - 使用 http 协议进行通讯时,需要 token 进行认证,SDK 默认使用内存型缓存(cachetools 方案)进行 token 的保存。 197 | 198 | - 如果您需要自行进行其他方案进行保存(file/memcached/redis),可以 `iot.get_token(cache=False)` 获取 token,再 publish 消息时,请 `iot.publish(payload=payload, token=token)` 199 | 200 | 201 | ## 设备影子系统 202 | 203 | ```Python 204 | # 设备主动上报状态 205 | iot.publish(payload={"method": "update", 206 | "version": i, 207 | "reported": {"online": True}}, 208 | topic="shadow", 209 | qos=1) 210 | 211 | # 设备主动获取设备影子内容 212 | iot.publish(payload={"method": "get"}, 213 | topic="shadow", 214 | qos=1) 215 | 216 | # 设备端删除影子属性 217 | iot.publish(payload={"method": "delete", "reported": "null"}, 218 | topic="shadow", 219 | qos=1) 220 | ``` -------------------------------------------------------------------------------- /aliyun_iot_device/.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__/ 2 | *.pyc -------------------------------------------------------------------------------- /aliyun_iot_device/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | __author__ = "yansongda " 4 | 5 | __version__ = "0.6.0" 6 | -------------------------------------------------------------------------------- /aliyun_iot_device/http/.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__/ 2 | *.pyc -------------------------------------------------------------------------------- /aliyun_iot_device/http/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | __author__ = "yansongda " 4 | 5 | from .client import Client 6 | -------------------------------------------------------------------------------- /aliyun_iot_device/http/client.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | __author__ = "yansongda " 4 | 5 | from cachetools import TTLCache 6 | import requests 7 | import hashlib 8 | import json 9 | import hmac 10 | 11 | HTTP_URI = "https://iot-as-http.{region}.aliyuncs.com/{uri}" 12 | 13 | DEFAULT_PUBLISH_TOPIC = "/{product_key}/{device_name}/update" 14 | DEFAULT_SUBSCRIBE_TOPIC = "/{product_key}/{device_name}/get" 15 | 16 | SHADOW_UPDATE_TOPIC = "/shadow/update/{product_key}/{device_name}" 17 | SHADOW_GET_TOPIC = "/shadow/get/{product_key}/{device_name}" 18 | 19 | 20 | class Client(object): 21 | """阿里云 IOT 物联网套件 HTTP 客户端 22 | """ 23 | 24 | def __init__(self, product_device, client_id=None, region="cn-shanghai"): 25 | """product_device: (tuple) 阿里云规定三元组,分别为 PRODUCE_KEY, DEVICE_NAME, DEVICE_SECRET 26 | 27 | client_id: (None, str) 客户端 id,如果为 None 或者 "",则 SDK 自动设置为 DEVICE_NAME 28 | 29 | region: (str) 阿里云地域,目前有cn-shanghai,us-west-1,ap-southeast-1 30 | """ 31 | super(Client, self).__init__() 32 | if not isinstance(product_device, tuple): 33 | raise TypeError('{pd} Must Be A Tuple'.format(pd=product_device)) 34 | 35 | if client_id is None or client_id == "": 36 | client_id = product_device[1] 37 | 38 | self.client_id = client_id 39 | self.region = region 40 | self.product_key, self.device_name, self.device_secret = product_device 41 | self.cache = TTLCache(maxsize=1, ttl=48 * 60 * 60) 42 | 43 | def publish(self, payload=None, topic=None, token=None): 44 | """payload: (str/int/float/None) 负载 45 | 46 | topic: (string) 发布的主题,默认为阿里云默认主题,即:"/{product_key}/{device_name}/update" 47 | 48 | token: 发布时所携带的 token,如果为 None,则 SDK 自动缓存获取 49 | """ 50 | if topic is None: 51 | topic = DEFAULT_PUBLISH_TOPIC.format(product_key=self.product_key, device_name=self.device_name) 52 | if topic == 'shadow': 53 | topic = SHADOW_UPDATE_TOPIC.format(product_key=self.product_key, device_name=self.device_name) 54 | 55 | data = {"method": payload['method']} 56 | if 'reported' in payload: 57 | data.update({"state": {"reported": payload['reported']}}) 58 | if 'version' in payload: 59 | data.update({"version": payload['version']}) 60 | payload = json.dumps(data) 61 | 62 | if token is None: 63 | token = self.get_token() 64 | 65 | response = self._request_api(uri='topic' + topic, data=bytes(payload, 'utf-8'), 66 | headers={"password": token, "Content-Type": "application/octet-stream"}) 67 | 68 | return response 69 | 70 | def get_token(self, cache=True): 71 | """获取 token 72 | 73 | cache: 如果为 True,则使用缓存。默认使用内存型缓存 cachetools 方案 74 | """ 75 | if cache: 76 | try: 77 | return self.cache['token'] 78 | except KeyError: 79 | self.cache['token'] = self._request_api(uri='auth')['token'] 80 | return self.cache['token'] 81 | 82 | return self._request_api(uri='auth')['token'] 83 | 84 | def _request_api(self, uri, data=None, headers=None): 85 | """与阿里云服务器通讯 86 | """ 87 | if data is None: 88 | data = json.dumps({"productKey": self.product_key, 89 | "deviceName": self.device_name, 90 | "clientId": self.client_id, 91 | "signmethod": "hmacsha1", 92 | "version": "default", 93 | "sign": self._get_sign()}) 94 | if headers is None: 95 | headers = {"Content-Type": "application/json"} 96 | 97 | response = requests.post(HTTP_URI.format(region=self.region, uri=uri), 98 | data=data, headers=headers).json() 99 | if response['code'] != 0: 100 | raise ValueError("与阿里云通讯出错,uri:{uri},response:{response}".format(uri=uri, response=response)) 101 | 102 | return response['info'] 103 | 104 | def _get_sign(self): 105 | """获取签名 106 | """ 107 | content = "clientId" + self.client_id + "deviceName" + self.device_name + "productKey" + self.product_key 108 | return hmac.new(bytes(self.device_secret, 'utf-8'), bytes(content, 'utf-8'), hashlib.sha1).hexdigest() 109 | -------------------------------------------------------------------------------- /aliyun_iot_device/mqtt/.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__/ 2 | *.pyc -------------------------------------------------------------------------------- /aliyun_iot_device/mqtt/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | __author__ = "yansongda " 4 | 5 | from .client import Client 6 | -------------------------------------------------------------------------------- /aliyun_iot_device/mqtt/client.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | __author__ = "yansongda " 4 | 5 | import paho.mqtt.client as mqtt_client 6 | import hashlib 7 | import hmac 8 | import time 9 | import os 10 | 11 | DOMAIN_DIRECT_URI = "{product_key}.iot-as-mqtt.{region}.aliyuncs.com" 12 | DOMAIN_DIRECT_PORT = 1883 13 | 14 | WEBSOCKETS_URI = "{product_key}.iot-as-mqtt.{region}.aliyuncs.com" 15 | WEBSOCKETS_PORT = 443 16 | 17 | HTTPS_AUTH = "https://iot-auth.{region}.aliyuncs.com/auth/devicename" 18 | 19 | DEFAULT_PUBLISH_TOPIC = "/{product_key}/{device_name}/update" 20 | DEFAULT_SUBSCRIBE_TOPIC = "/{product_key}/{device_name}/get" 21 | 22 | SHADOW_UPDATE_TOPIC = "/shadow/update/{product_key}/{device_name}" 23 | SHADOW_GET_TOPIC = "/shadow/get/{product_key}/{device_name}" 24 | 25 | KEEPALIVE = 60 26 | CA_CERTS = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'root.cer') 27 | 28 | 29 | class Client(mqtt_client.Client): 30 | """阿里云 IOT 物联网套件设备端 MQTT 客户端 SDK 31 | 32 | 基本使用方法: 33 | 34 | from aliyun_iot_device.mqtt import Client 35 | iot = Client((PRODUCE_KEY, DEVICE_NAME, DEVICE_SECRET), CLIENT_ID) 36 | iot.connect() 37 | iot.loop_start() 38 | while True: 39 | iot.publish('success', 1) 40 | time.sleep(5) 41 | """ 42 | 43 | def __init__(self, product_device, client_id=None, clean_session=True, 44 | region="cn-shanghai", domain_direct=True, tls=True, ca_certs=CA_CERTS, transport="tcp"): 45 | """product_device: (tuple) 阿里云规定三元组,分别为 PRODUCE_KEY, DEVICE_NAME, DEVICE_SECRET 46 | 47 | client_id: (None, str) 客户端 id,如果为 None 或者 "",则 SDK 自动设置为 DEVICE_NAME 48 | 49 | region: (str) 阿里云地域,目前有cn-shanghai,us-west-1,ap-southeast-1 50 | 51 | domain_direct: (bool) 是否启用域名直连模式,默认启用 52 | 53 | tls: (bool) 是否启用 tls 加密,默认启用 54 | 55 | ca_certs: (str) ca 证书路径,SDK 已默认加载阿里云根证书,无特殊用途不需要更改 56 | 57 | transport: (str) 传输模式,默认为 tcp,支持 websockets 58 | """ 59 | if not isinstance(product_device, tuple): 60 | raise TypeError('{pd} Must Be A Tuple'.format(pd=product_device)) 61 | 62 | if client_id is None or client_id == "": 63 | client_id = product_device[1] 64 | 65 | self.client_id = client_id 66 | self.region = region 67 | self.tls = tls 68 | self.ca_certs = ca_certs 69 | self.domain_direct = domain_direct 70 | self.transport = transport 71 | self.clean_session = clean_session 72 | self.mqtt_uri = '' 73 | self.mqtt_port = 0 74 | self.product_key, self.device_name, self.device_secret = product_device 75 | 76 | self._get_mqtt_client() 77 | 78 | def connect(self, keepalive=KEEPALIVE): 79 | """连接阿里云 IOT 服务器 80 | 81 | keepalive: (int) 心跳秒数,60-300,默认 60秒 82 | """ 83 | return super(Client, self).connect(self.mqtt_uri, self.mqtt_port, keepalive) 84 | 85 | def publish(self, payload=None, qos=0, topic=None): 86 | """payload: (str/int/float/None) 负载 87 | 88 | qos: (int) 0/1,服务等级 89 | 90 | topic: (string) 发布的主题。 91 | 如果为 None 默认为阿里云默认主题,即:"/{product_key}/{device_name}/update"; 92 | 93 | 如果为'shadow',则为阿里云影子系统主题,即"/shadow/get/{product_key}/{device_name}",此时payload 应为 dict 类型,包含"version", "reported" 两个key。详情请查看阿里云物联网套件官方文档。 94 | 95 | 阿里云 IOT 套件不支持 retain 96 | """ 97 | if topic is None: 98 | topic = DEFAULT_PUBLISH_TOPIC.format(product_key=self.product_key, device_name=self.device_name) 99 | if topic == 'shadow': 100 | import json 101 | topic = SHADOW_UPDATE_TOPIC.format(product_key=self.product_key, device_name=self.device_name) 102 | 103 | data = {"method": payload['method']} 104 | if 'reported' in payload: 105 | data.update({"state": {"reported": payload['reported']}}) 106 | if 'version' in payload: 107 | data.update({"version": payload['version']}) 108 | payload = json.dumps(data) 109 | 110 | return super(Client, self).publish(topic, payload, qos, False) 111 | 112 | def subscribe(self, qos=0, topic=None): 113 | """qos: (int) 0/1,服务等级 114 | 115 | topic: (string) 订阅的主题。 116 | 如果为 None 默认为阿里云默认主题,即:"/{product_key}/{device_name}/get"; 117 | 如果为'shadow',则为阿里云影子系统主题,即"/shadow/get/{product_key}/{device_name}" 118 | """ 119 | if topic is None: 120 | topic = DEFAULT_SUBSCRIBE_TOPIC.format(product_key=self.product_key, device_name=self.device_name) 121 | if topic == 'shadow': 122 | topic = SHADOW_GET_TOPIC.format(product_key=self.product_key, device_name=self.device_name) 123 | 124 | return super(Client, self).subscribe(topic, qos) 125 | 126 | def unsubscribe(self, topic=None): 127 | """topic: (string) 订阅的主题,默认为阿里云默认主题,即:"/{product_key}/{device_name}/get" 128 | """ 129 | if topic is None: 130 | topic = DEFAULT_SUBSCRIBE_TOPIC.format(product_key=self.product_key, device_name=self.device_name) 131 | 132 | return super(Client, self).unsubscribe(topic) 133 | 134 | def _get_mqtt_client(self): 135 | """获取 MQTT 客户端实例 136 | """ 137 | if self.transport == "websockets": 138 | mqtt_client_id, mqtt_user, mqtt_passwd = self._get_websockets_mqtt_info() 139 | elif self.domain_direct: 140 | mqtt_client_id, mqtt_user, mqtt_passwd = self._get_doamin_direct_mqtt_info() 141 | else: 142 | mqtt_client_id, mqtt_user, mqtt_passwd = self._get_https_mqtt_info() 143 | 144 | super(Client, self).__init__(mqtt_client_id, transport=self.transport, clean_session=self.clean_session) 145 | self.username_pw_set(mqtt_user, mqtt_passwd) 146 | if not self.domain_direct or self.tls: 147 | self.tls_set(ca_certs=self.ca_certs) 148 | 149 | def _get_doamin_direct_mqtt_info(self): 150 | """获取域名直连 MQTT 连接信息 151 | """ 152 | mode = "3" 153 | if self.tls: 154 | mode = "2" 155 | 156 | mqtt_client_id = self.client_id + "|securemode=" + mode + ",signmethod=hmacsha1,timestamp=" + str(round(time.time())) + "|" 157 | mqtt_user = self.device_name + "&" + self.product_key 158 | mqtt_content = "clientId" + self.client_id + "deviceName" + self.device_name + "productKey" + self.product_key + "timestamp" + str(round(time.time())) 159 | mqtt_passwd = hmac.new(bytes(self.device_secret, 'utf-8'), bytes(mqtt_content, 'utf-8'), hashlib.sha1).hexdigest() 160 | 161 | self.mqtt_uri = DOMAIN_DIRECT_URI.format(product_key=self.product_key, region=self.region) 162 | self.mqtt_port = DOMAIN_DIRECT_PORT 163 | 164 | return mqtt_client_id, mqtt_user, mqtt_passwd 165 | 166 | def _get_websockets_mqtt_info(self): 167 | websockets_info = self._get_doamin_direct_mqtt_info() 168 | 169 | self.mqtt_uri = WEBSOCKETS_URI.format(product_key=self.product_key, region=self.region) 170 | self.mqtt_port = WEBSOCKETS_PORT 171 | 172 | return websockets_info 173 | 174 | def _get_https_mqtt_info(self): 175 | """获取HTTPS 连接方法 MQTT 连接信息 176 | """ 177 | import requests 178 | 179 | response = requests.post(HTTPS_AUTH.format(region=self.region), 180 | data={'productKey': self.product_key, 181 | 'deviceName': self.device_name, 182 | 'clientId': self.client_id, 183 | 'signmethod': "hmacsha1", 184 | "resources": "mqtt", 185 | "timestamp": str(round(time.time())), 186 | "sign": self._get_sign()}).json() 187 | if response['code'] != 200: 188 | raise ValueError("获取连接信息错误:{}".format(response)) 189 | 190 | self.mqtt_uri = response['data']['resources']['mqtt']['host'] 191 | self.mqtt_port = response['data']['resources']['mqtt']['port'] 192 | 193 | return self.client_id, response['data']['iotId'], response['data']['iotToken'] 194 | 195 | def _get_sign(self): 196 | content = "clientId" + self.client_id + "deviceName" + self.device_name + "productKey" + self.product_key + "timestamp" + str(round(time.time())) 197 | return hmac.new(bytes(self.device_secret, 'utf-8'), bytes(content, 'utf-8'), hashlib.sha1).hexdigest() 198 | -------------------------------------------------------------------------------- /aliyun_iot_device/root.cer: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIDdTCCAl2gAwIBAgILBAAAAAABFUtaw5QwDQYJKoZIhvcNAQEFBQAwVzELMAkG 3 | A1UEBhMCQkUxGTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExEDAOBgNVBAsTB1Jv 4 | b3QgQ0ExGzAZBgNVBAMTEkdsb2JhbFNpZ24gUm9vdCBDQTAeFw05ODA5MDExMjAw 5 | MDBaFw0yODAxMjgxMjAwMDBaMFcxCzAJBgNVBAYTAkJFMRkwFwYDVQQKExBHbG9i 6 | YWxTaWduIG52LXNhMRAwDgYDVQQLEwdSb290IENBMRswGQYDVQQDExJHbG9iYWxT 7 | aWduIFJvb3QgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDaDuaZ 8 | jc6j40+Kfvvxi4Mla+pIH/EqsLmVEQS98GPR4mdmzxzdzxtIK+6NiY6arymAZavp 9 | xy0Sy6scTHAHoT0KMM0VjU/43dSMUBUc71DuxC73/OlS8pF94G3VNTCOXkNz8kHp 10 | 1Wrjsok6Vjk4bwY8iGlbKk3Fp1S4bInMm/k8yuX9ifUSPJJ4ltbcdG6TRGHRjcdG 11 | snUOhugZitVtbNV4FpWi6cgKOOvyJBNPc1STE4U6G7weNLWLBYy5d4ux2x8gkasJ 12 | U26Qzns3dLlwR5EiUWMWea6xrkEmCMgZK9FGqkjWZCrXgzT/LCrBbBlDSgeF59N8 13 | 9iFo7+ryUp9/k5DPAgMBAAGjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMBAf8E 14 | BTADAQH/MB0GA1UdDgQWBBRge2YaRQ2XyolQL30EzTSo//z9SzANBgkqhkiG9w0B 15 | AQUFAAOCAQEA1nPnfE920I2/7LqivjTFKDK1fPxsnCwrvQmeU79rXqoRSLblCKOz 16 | yj1hTdNGCbM+w6DjY1Ub8rrvrTnhQ7k4o+YviiY776BQVvnGCv04zcQLcFGUl5gE 17 | 38NflNUVyRRBnMRddWQVDf9VMOyGj/8N7yy5Y0b2qvzfvGn9LhJIZJrglfCm7ymP 18 | AbEVtQwdpf5pLGkkeB6zpxxxYu7KyJesF12KwvhHhm4qxFYxldBniYUr+WymXUad 19 | DKqC5JlR3XC321Y9YeRq4VzW9v493kHMB65jUr9TU/Qr6cf9tveCX4XSQRjbgbME 20 | HMUfpIBvFSDJ3gyICh3WZlXi/EjJKSZp4A== 21 | -----END CERTIFICATE----- -------------------------------------------------------------------------------- /examples/http/basic.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | __author__ = "yansongda " 4 | 5 | import context 6 | from aliyun_iot_device.http import Client as IOT 7 | import time 8 | 9 | PRODUCE_KEY = "b1VzFx30hEm" 10 | DEVICE_NAME = "iot_device_01" 11 | DEVICE_SECRET = "3TSqd6sfzjSkSwEmLmcAdZnI0oGlmRZ8" 12 | 13 | iot = IOT((PRODUCE_KEY, DEVICE_NAME, DEVICE_SECRET)) 14 | 15 | while True: 16 | iot.publish('success') 17 | time.sleep(5) 18 | -------------------------------------------------------------------------------- /examples/http/context.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | try: 4 | import aliyun_iot_device 5 | 6 | except ImportError: 7 | import sys 8 | import os 9 | import inspect 10 | 11 | cmd_subfolder = os.path.realpath( 12 | os.path.abspath( 13 | os.path.join( 14 | os.path.split( 15 | inspect.getfile(inspect.currentframe()) 16 | )[0], 17 | "..", 18 | ".." 19 | ) 20 | ) 21 | ) 22 | if cmd_subfolder not in sys.path: 23 | sys.path.insert(0, cmd_subfolder) 24 | 25 | import aliyun_iot_device -------------------------------------------------------------------------------- /examples/mqtt/all.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | __author__ = "yansongda " 4 | 5 | import context 6 | from aliyun_iot_device.mqtt import Client as IOT 7 | import time 8 | 9 | 10 | def on_connect(client, userdata, flags, rc): 11 | print('subscribe') 12 | client.subscribe(qos=1) 13 | 14 | 15 | def on_message(client, userdata, msg): 16 | print('receive message') 17 | print(str(msg.payload)) 18 | 19 | 20 | PRODUCE_KEY = "b1VzFx30hEm" 21 | DEVICE_NAME = "iot_device_01" 22 | DEVICE_SECRET = "3TSqd6sfzjSkSwEmLmcAdZnI0oGlmRZ8" 23 | 24 | iot = IOT((PRODUCE_KEY, DEVICE_NAME, DEVICE_SECRET)) 25 | 26 | iot.on_connect = on_connect 27 | iot.on_message = on_message 28 | 29 | iot.connect() 30 | 31 | iot.loop_start() 32 | while True: 33 | iot.publish(payload="success", qos=1) 34 | time.sleep(5) 35 | -------------------------------------------------------------------------------- /examples/mqtt/basic.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | __author__ = "yansongda " 4 | 5 | import context 6 | from aliyun_iot_device.mqtt import Client as IOT 7 | import time 8 | 9 | 10 | PRODUCE_KEY = "b1VzFx30hEm" 11 | DEVICE_NAME = "iot_device_01" 12 | DEVICE_SECRET = "3TSqd6sfzjSkSwEmLmcAdZnI0oGlmRZ8" 13 | CLIENT_ID = "" 14 | 15 | iot = IOT((PRODUCE_KEY, DEVICE_NAME, DEVICE_SECRET)) 16 | 17 | iot.connect() 18 | 19 | iot.loop_start() 20 | while True: 21 | iot.publish('success', 1) 22 | time.sleep(5) 23 | -------------------------------------------------------------------------------- /examples/mqtt/context.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | try: 4 | import aliyun_iot_device 5 | 6 | except ImportError: 7 | import sys 8 | import os 9 | import inspect 10 | 11 | cmd_subfolder = os.path.realpath( 12 | os.path.abspath( 13 | os.path.join( 14 | os.path.split( 15 | inspect.getfile(inspect.currentframe()) 16 | )[0], 17 | "..", 18 | ".." 19 | ) 20 | ) 21 | ) 22 | if cmd_subfolder not in sys.path: 23 | sys.path.insert(0, cmd_subfolder) 24 | 25 | import aliyun_iot_device -------------------------------------------------------------------------------- /examples/mqtt/https.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | __author__ = "yansongda " 4 | 5 | import context 6 | from aliyun_iot_device.mqtt import Client as IOT 7 | import time 8 | 9 | 10 | PRODUCE_KEY = "b1VzFx30hEm" 11 | DEVICE_NAME = "iot_device_01" 12 | DEVICE_SECRET = "3TSqd6sfzjSkSwEmLmcAdZnI0oGlmRZ8" 13 | CLIENT_ID = "" 14 | 15 | iot = IOT((PRODUCE_KEY, DEVICE_NAME, DEVICE_SECRET), domain_direct=False) 16 | 17 | iot.connect() 18 | 19 | iot.loop_start() 20 | while True: 21 | iot.publish('success', 1) 22 | time.sleep(5) 23 | -------------------------------------------------------------------------------- /examples/mqtt/shadow.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | __author__ = "yansongda " 4 | 5 | import context 6 | from aliyun_iot_device.mqtt import Client as IOT 7 | import time 8 | 9 | 10 | def on_connect(client, userdata, flags, rc): 11 | print('subscribe') 12 | client.subscribe(topic='shadow', qos=1) 13 | 14 | 15 | def on_message(client, userdata, msg): 16 | print('receive message') 17 | print(str(msg.payload)) 18 | 19 | 20 | PRODUCE_KEY = "b1VzFx30hEm" 21 | DEVICE_NAME = "iot_device_01" 22 | DEVICE_SECRET = "3TSqd6sfzjSkSwEmLmcAdZnI0oGlmRZ8" 23 | 24 | iot = IOT((PRODUCE_KEY, DEVICE_NAME, DEVICE_SECRET)) 25 | 26 | iot.on_connect = on_connect 27 | iot.on_message = on_message 28 | 29 | iot.connect() 30 | 31 | i = 1 32 | iot.loop_start() 33 | while True: 34 | iot.publish(payload={"method": "update", "version": i, 35 | "reported": {"online": True}}, topic="shadow", qos=1) 36 | i += 1 37 | time.sleep(5) 38 | -------------------------------------------------------------------------------- /examples/mqtt/websockets.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | __author__ = "yansongda " 4 | 5 | import context 6 | from aliyun_iot_device.mqtt import Client as IOT 7 | import time 8 | 9 | 10 | PRODUCE_KEY = "b1VzFx30hEm" 11 | DEVICE_NAME = "iot_device_01" 12 | DEVICE_SECRET = "3TSqd6sfzjSkSwEmLmcAdZnI0oGlmRZ8" 13 | CLIENT_ID = "" 14 | 15 | iot = IOT((PRODUCE_KEY, DEVICE_NAME, DEVICE_SECRET), transport="websockets") 16 | 17 | iot.connect() 18 | 19 | iot.loop_start() 20 | while True: 21 | iot.publish('success', 1) 22 | time.sleep(5) 23 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | cachetools==2.1.0 2 | certifi==2018.4.16 3 | chardet==3.0.4 4 | idna==2.6 5 | paho-mqtt==1.3.1 6 | pkginfo==1.4.2 7 | requests==2.20.0 8 | requests-toolbelt==0.8.0 9 | tqdm==4.23.4 10 | twine==1.11.0 11 | urllib3==1.24.2 12 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import setuptools 4 | import sys 5 | import os 6 | 7 | from aliyun_iot_device import __version__ 8 | 9 | with open("README.md", "r") as fh: 10 | long_description = fh.read() 11 | 12 | requires = [ 13 | 'paho-mqtt>=1.3.1', 14 | 'requests>=2.18.4', 15 | 'cachetools>=2.1.0' 16 | ] 17 | 18 | if sys.argv[-1] == 'publish': 19 | os.system('python3 setup.py sdist bdist_wheel') 20 | os.system('twine upload dist/*') 21 | os.system("rm -rf ./dist ./build ./yansongda_aliyun_iot_device.egg-info") 22 | sys.exit() 23 | 24 | if sys.argv[-1] == 'publish-test': 25 | os.system('python3 setup.py sdist bdist_wheel') 26 | os.system('twine upload --repository-url https://test.pypi.org/legacy/ dist/*') 27 | os.system("rm -rf ./dist ./build ./yansongda_aliyun_iot_device.egg-info") 28 | sys.exit() 29 | 30 | setuptools.setup( 31 | name="yansongda-aliyun-iot-device", 32 | version=__version__, 33 | author="yansongda", 34 | author_email="me@yansongda.cn", 35 | description="aliyun iot device sdk", 36 | long_description=long_description, 37 | long_description_content_type="text/markdown", 38 | package_data={ 39 | '': ['LICENSE', 'NOTICE'], 40 | 'aliyun_iot_device': ['*.cer'] 41 | }, 42 | url="https://github.com/yansongda/aliyun-iot-device-python", 43 | project_urls={ 44 | 'Source': 'https://github.com/yansongda/aliyun-iot-device-python', 45 | 'Tracker': 'https://github.com/yansongda/aliyun-iot-device-python/issues', 46 | }, 47 | packages=setuptools.find_packages(), 48 | python_requires=">=3.0, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*", 49 | install_requires=requires, 50 | keywords='yansongda aliyun iot device', 51 | classifiers=( 52 | "Programming Language :: Python :: 3", 53 | "License :: OSI Approved :: MIT License", 54 | "Operating System :: OS Independent", 55 | ), 56 | ) 57 | --------------------------------------------------------------------------------