Coder Social home page Coder Social logo

cooleiot-python's Introduction

酷易物联Python3SDK

酷易物联,快速接入/开发/管理物联设备 cooleiot.tech

项目已迁移到 https://github.com/cooleiot/python-sdk

实例

实例化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")

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.