酷易物联Python3SDK
酷易物联,快速接入/开发/管理物联设备 cooleiot.tech
实例
实例化IoT连接,须填入设备的DevelopKey
from CoolEIoT import CoolE
iot = CoolE("{developkey}")
iot.setDebug
# 设置debug状态
def setDebug(self, sta:bool = True)
使用:
iot.setDebug()
iot.recv
iot.recv 是 CoolE类的一个成员变量,用于存储当前从MQTT服务器接收到的消息
每当使用
iot.decode()解析成功字段后,iot.recv将会重置清零
使用:
recv = iot.recv
iot.getDeviceId
# 获取设备ID
def getDeviceId(self)
使用:
在
iot.start()之后才能调用此方法获取设备ID
device_id = iot.getDeviceId()
连接
iot.start
# 配置设备初始化并连接到MQTT服务器
def start(self, keepalive:int = 60, ssl:bool = False, host = False)
"""
keepalive: 默认60秒
ssl: 开启mqtts
host: 是否上位机(为True后,连接到mqtt服务器后不会触发上线)
"""
使用:
iot.start()
iot.start(120)
iot.start(120, True)
iot.loop
# MQTT接收循环
def loop(self)
循环接收MQTT消息,如需要在设备运行过程中接收消息,请将该方法放入主函数的
while True:中
使用:
iot.loop()
iot.stop
# 关闭MQTT连接
def stop(self)
使用:
iot.stop()
字段
iot.decode
# 字段解析
def decode(self, payload:str, type:str)
type = ["COMMAND","GET","CONTENT"] # 命令字段,GET指令,内容字段
该方法用于解析在程序运行过程中接收到的三类字段
使用示例:
def handleGet():
get = iot.decode(iot.recv,"GET")
if get is not None:
print("接收到[" + get + "]GET指令")
def handleCommand():
command = iot.decode(iot.recv,"COMMAND")
if command is not None:
print("接收到[" + command + "]命令字段")
def handleContent():
content = iot.decode(iot.recv,"CONTENT")
if content is not None:
print("接收到[" + content['n'] + "]内容字段,内容为:" + content['d'])
开发中,您需要先将字段数据更新至iot实例中的一个成员变量,然后再进行消息发布
也就是说,更新数据和发布消息是两步!
iot.updateReport
# 为通信字段添加一个字段内容
def updateReport(self, field:str, data)
该方法用于将数据更新至指定通信字段,相同字段名,后更新的数据会覆盖之前的数据
使用:
iot.updateReport("temp",23.6)
iot.updateReport("ledsta","开")
iot.getReport
# 获取通信字段内容
def getReport(self, field:str)
使用:
ledsta = iot.getReport("ledsta")
iot.clearReport
# 清除当前成员变量存储的所有通信字段信息
def clearReport(self)
使用:
iot.clearReport()
iot.updateContent
# 为通信字段添加一个字段内容
def updateContent(self, field:str, data)
该方法用于将数据更新至指定内容字段,相同字段名,后更新的数据会覆盖之前的数据
使用:
content = iot.decode(iot.recv,"CONTENT")
if content is not None:
iot.updateContent(content['n'],content['d'])
iot.getContent
# 获取通信字段内容
def getContent(self, field:str)
使用:
print(iot.getContent("line1"))
iot.clearContent
# 清除当前成员变量存储的所有内容字段信息
def clearContent(self)
使用:
iot.clearContent()
发布
为了避免代码编写不规范等导致的Broker阻塞
无论代码编写方法如何,publish方法每秒只会发送一条消息
iot.publish
# 发布消息
def publish(self, field:str = None, payload = None)
发布消息到MQTT服务器
- 如果您想发送之前使用
iot.updateReport()存储的所有通信字段内容
iot.publish()
- 如果您想发送之前使用
iot.updateReport()存储的指定通信字段内容
iot.publish("temp")
- 如果您想发送指定通信字段内容的自定义内容,而不在其前面进行
updateReport()。
也就是说您想单独发送此次消息,而不将这次发送的数据更新到通信字段成员变量中
iot.publish("temp",23.8)
iot.publish("ledsta","开")
iot.selfPublish
- 如果您想发送自定义key-value键值对
iot.selfPublish("hello", "world") # {"hello":"world"}
iot.selfPublish("ram", 400) # {"ram":400}
iot.selfPublish("ram", "400") # {"ram":"400"}
云配置
为了节省服务器资源,云配置每个DevelopKey每天仅能调用200次
iot.getConfig
# 获取云配置
def getConfig(self, field:str)
使用:
citycode = iot.getConfig("citycode")
Metadata
Release files for cooleiot 0.0.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cooleiot-0.0.7.tar.gz | 5.7 kB | Without provenance | Details for cooleiot-0.0.7.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cooleiot-0.0.7-py3-none-any.whl | Python 3 | none | any | Details for cooleiot-0.0.7-py3-none-any.whl |
Total release size: 11.0 kB
Release files / cooleiot-0.0.7.tar.gz
| Download URL | cooleiot-0.0.7.tar.gz |
|---|---|
| Size | 5.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
54403d0d33c435ba3c4b02cbc9f18166cc0d04d63bb4425dc98d2b2cb3fa5bcf
|
|
BLAKE2b-256 checksum How to use checksums |
ef4e2157eb74ebdce0daa73ebb5a9c0f3987401d2bf40d37a4f8e6ed738ad33f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
|
Release files / cooleiot-0.0.7-py3-none-any.whl
| Download URL | cooleiot-0.0.7-py3-none-any.whl |
|---|---|
| Size | 5.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bc6f490d9c25647cfa9ef847114abd223b721d758b305cbfa298cd76b9d332c8
|
|
BLAKE2b-256 checksum How to use checksums |
62820cef241218bae77846854c9bbb614f95047f5688ae199fd03a934729ba06
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
|
Security for cooleiot 0.0.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.