卡宴MQTT蟒蛇图书馆
cayenne-mqtt的Python项目详细描述
卡宴mqtt python库提供了一些函数,可以方便地连接到Cayenne IoT project builder。有了它,您可以向卡宴发送数据并从卡宴接收数据。
要求
- Python 2.7.9+ or 3.4+。
- This library。
- Eclipse Paho MQTT Python client library。这是作为卡宴库安装的一部分安装的。
开始
安装
此库可以使用pip安装:
pip install cayenne-mqtt
它也可以从存储库安装:
git clone https://github.com/myDevicesIoT/Cayenne-MQTT-Python cd Cayenne-MQTT-Python python setup.py install
卡宴设置
- 在https://mydevices.com创建您的卡宴帐户。
- 使用“自带物品”API选项添加新设备。
示例
存储库中提供了一些简单的示例:https://github.com/myDevicesIoT/Cayenne-MQTT-Python/tree/master/examples。
下面是一个简单的客户机示例,它发布一些示例数据,并在消息回调中从cayen接收数据。在卡宴中添加新设备时,必须使用收到的身份验证信息修改卡宴身份验证变量:
import cayenne.client import time # Cayenne authentication info. This should be obtained from the Cayenne Dashboard. MQTT_USERNAME = "MQTT_USERNAME" MQTT_PASSWORD = "MQTT_PASSWORD" MQTT_CLIENT_ID = "MQTT_CLIENT_ID" # The callback for when a message is received from Cayenne. def on_message(message): print("message received: " + str(message)) # If there is an error processing the message return an error string, otherwise return nothing. client = cayenne.client.CayenneMQTTClient() client.on_message = on_message client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID) # For a secure connection use port 8883 when calling client.begin: # client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=8883) i=0 timestamp = 0 while True: client.loop() if (time.time() > timestamp + 10): client.celsiusWrite(1, i) client.luxWrite(2, i*10) client.hectoPascalWrite(3, i+800) timestamp = time.time() i = i+1
文件
有关cayen客户端api的更多详细信息,可以使用pydoc。
pydoc cayenne.client
附加的卡宴MQTT库
其他库可用于用其他语言连接到卡宴。这些可以在https://github.com/myDevicesIoT找到。