|
1 | 1 | Cayenne MQTT Python Library
|
2 |
| -=========================== |
3 |
| -The Cayenne MQTT Python Library provides functions to easily connect to the Cayenne IoT project builder. It supports Python 2.7 or 3.x. |
| 2 | +*************************** |
| 3 | +The Cayenne MQTT Python Library provides functions to easily connect to the `Cayenne IoT project builder <https://www.cayenne-mydevices.com>`_. With it you can send data to and receive data from Cayenne. |
4 | 4 |
|
5 |
| -This library installs the `Eclipse Paho MQTT Python client <https://github.com/eclipse/paho.mqtt.python>`_. |
| 5 | +Requirements |
| 6 | +============ |
| 7 | +* `Python 2.7 or 3.x <https://www.python.org/downloads/>`_. |
| 8 | +* `This library <https://github.com/myDevicesIoT/Cayenne-MQTT-Python/archive/master.zip>`_. |
| 9 | +* `Eclipse Paho MQTT Python client library <https://github.com/eclipse/paho.mqtt.python>`_. This is installed as part of the Cayenne library installation. |
6 | 10 |
|
| 11 | +Getting Started |
| 12 | +=============== |
| 13 | +Installation |
| 14 | +------------ |
| 15 | +This library can be installed using pip: |
| 16 | +:: |
| 17 | + |
| 18 | + pip install cayenne-mqtt |
| 19 | + |
| 20 | +It can also be installed from the repository: |
| 21 | +:: |
| 22 | + |
| 23 | + git clone https://github.com/myDevicesIoT/Cayenne-MQTT-Python |
| 24 | + cd Cayenne-MQTT-Python |
| 25 | + python setup.py install |
| 26 | + |
| 27 | +Cayenne Setup |
| 28 | +------------- |
| 29 | +1. Create your Cayenne account at https://www.cayenne-mydevices.com. |
| 30 | +2. Add a new device using the Bring Your Own Thing API selection. |
| 31 | + |
| 32 | +Examples |
| 33 | +-------- |
| 34 | +Simple examples are available in the repository here: https://github.com/myDevicesIoT/Cayenne-MQTT-Python/tree/master/examples. |
| 35 | + |
| 36 | +Below is an example of a simple client that publishes some sample data and receives data from Cayenne in a message callback. The Cayenne authentication variables must be modified with the authentication info you received when adding a new device in Cayenne: |
| 37 | +:: |
| 38 | + |
| 39 | + import cayenne.client |
| 40 | + import time |
| 41 | + |
| 42 | + # Cayenne authentication info. This should be obtained from the Cayenne Dashboard. |
| 43 | + MQTT_USERNAME = "MQTT_USERNAME" |
| 44 | + MQTT_PASSWORD = "MQTT_PASSWORD" |
| 45 | + MQTT_CLIENT_ID = "MQTT_CLIENT_ID" |
| 46 | + |
| 47 | + # The callback for when a message is received from Cayenne. |
| 48 | + def on_message(message): |
| 49 | + print("message received: " + str(message)) |
| 50 | + # If there is an error processing the message return an error string, otherwise return nothing. |
| 51 | + |
| 52 | + client = cayenne.client.CayenneMQTTClient() |
| 53 | + client.on_message = on_message |
| 54 | + client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID) |
| 55 | + |
| 56 | + i=0 |
| 57 | + timestamp = 0 |
| 58 | + |
| 59 | + while True: |
| 60 | + client.loop() |
| 61 | + |
| 62 | + if (time.time() > timestamp + 10): |
| 63 | + client.celsiusWrite(1, i) |
| 64 | + client.luxWrite(2, i*10) |
| 65 | + client.hectoPascalWrite(3, i+800) |
| 66 | + timestamp = time.time() |
| 67 | + i = i+1 |
| 68 | + |
| 69 | +Documentation |
| 70 | +------------- |
| 71 | +For more detailed info about the Cayenne client API you can use **pydoc**. |
| 72 | +:: |
| 73 | + |
| 74 | + pydoc cayenne.client |
| 75 | + |
7 | 76 |
|
8 | 77 | Additional Cayenne MQTT Libraries
|
9 | 78 | =================================
|
|
0 commit comments